Playing a sound during onscreen button press


Author
Message
Nejc900
Nejc900
Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)Associate Member (51 reputation)
Group: Forum Members
Posts: 3, Visits: 22
Hi,

First of all, thank you for actively running this forum! It has been an incredible help in getting to grips with Inquisit and trying to solve issues that have arisen. I am still a complete novice when it comes to any kind of coding so apologies if any of the bellow does not make sense or has a very simple solution.

tl;dr
I want to have a sound play while someone is holding down an onscreen button, with the sound starting when they press down and ending when they release the button.

Longer explanation:
For context, this is a redeveloped version of the BART script that is available through the library. What I have done is added a third button named the valve button which the participants need to press and hold for a specific duration.
The first issue that I needed to resolve (which I managed to do with significant help from this forum) was to actually record someone pressing and holding down the button. I did so by having validresponses be lbuttondown and lbuttonup. I added shapes behind actual buttons and defined clickable areas based on them. i.e.
<expressions>
/ isovervalvebutton = (mouse.x > shape.valve.left && mouse.x < shape.valve.right && mouse.y > shape.valve.top && mouse.y < shape.valve.bottom)
</expressions>

I then added isvalidresponse as seen below with a time being recorded for lbutton down and then deducted for lbuttonup, giving is the duration of the button hold.
/ isvalidresponse = [
if(trial.choiceaftervalve.response == "lbuttondown" && expressions.isovervalvebutton) {
  values.time_of_button_down = trial.choiceaftervalve.latency;
  values.n_clicks += 1;
 return false;
};
  else if (trial.choice.response == "lbuttonup" && expressions.isovervalvebutton) {
  values.time_of_button_up = trial.choice.latency;
 if (values.time_of_button_down > 0) {
    values.diff = values.time_of_button_up - values.time_of_button_down;
 values.n_clicks += 1
   };
 return true; 
  };
else if(trial.choiceaftervalve.response == "lbuttondown" && expressions.isoverpumpbutton) {
 return true;
};
else if (trial.choiceaftervalve.response == "lbuttondown" && expressions.isovercollectbutton){
 return true;
};
]

What I want to add now is that a sound (valvesound) should play for the duration of the button being pressed. There's a few ways in which I have tried to accomplish this, but none have yielded any useful results. Below is the full trial within which this would be implemented, with the full script also attached if useful.

Thank you!

<trial choice>
/ ontrialbegin = [
  values.countchoice += 1;
  values.pumpresult = list.pumpresult.nextvalue;
  if (values.pumpsound == 1)
   trial.choice.insertstimulustime(sound.inflatesound,0);
  values.pumpsound = 0;
  values.time_of_button_down = -1;
values.time_of_button_up = -1;
values.n_clicks = 0;
values.diff = 0;
  clock.stopwatch.start();
]
/ stimulustimes = [1=balloon, pump, collect, collectbutton, valve, pumpbutton, totalearnings, pumpcount, potentialearnings, ballooncount, redsquare, whitesquare, valvebutton, valveinstruct, stopwatch]
/ inputdevice = mouse
/ validresponse = (lbuttondown, lbuttonup)
/ isvalidresponse = [
if(trial.choice.response == "lbuttondown" && expressions.isovervalvebutton) {
  values.time_of_button_down = trial.choice.latency;
  values.n_clicks += 1;
 return false;
};
  else if (trial.choice.response == "lbuttonup" && expressions.isovervalvebutton) {
  values.time_of_button_up = trial.choice.latency;
 if (values.time_of_button_down > 0) {
    values.diff = values.time_of_button_up - values.time_of_button_down;
 values.n_clicks += 1;
   };
 return true; 
  };
else if(trial.choice.response == "lbuttondown" && expressions.isoverpumpbutton) {
 return true;
};
else if (trial.choice.response == "lbuttondown" && expressions.isovercollectbutton){
 return true;
};
]
/ ontrialend = [
  trial.choice.resetstimulusframes();
  if (trial.choice.response == "lbuttondown" && expressions.isoverpumpbutton) {
   values.scalingFactor += parameters.balloonsizeincrement;
   values.pumpcount += 1;
   if (values.pumpcount > 1) {
    values.mean_timebtwpumps = values.sum_timebtwpumps / (values.pumpcount-1);
   };
   values.totalpumpcount += 1
   if (values.countchoice == 1) {
    values.timebefore1stpump = trial.choice.latency;
   } else {
    values.timebtwpumps = trial.choice.latency;
    values.sum_timebtwpumps += values.timebtwpumps;
   };
  } else if (trial.choice.response == "lbuttondown" && expressions.isovercollectbutton) {
   if (values.countchoice == 1) {
    values.timebeforecollectwithoutpump = trial.choice.latency;
   } else {
    values.timebtwlastpumpandcollect = trial.choice.latency;
   };
  };

/ branch = [
  if (trial.choice.response == "lbuttondown" && expressions.isoverpumpbutton) {
   if (values.pumpresult == 1) {
    return trial.pop; // If you select pump button and the value that is picked out is 1 then go to the pop trial.
   } else {
    values.pumpsound = 1;
    return trial.choice; // Otherwise continue with the trial
   }
  }
  else if (trial.choice.response == "lbuttondown" && expressions.isovercollectbutton) {
   return trial.collect; // If you select the collect button, go to the collect trial.
  };
  else if (trial.choice.response == "lbuttonup" && expressions.isovervalvebutton && values.n_clicks == 1) {
   return trial.choiceaftervalve;
  };

]
/ recorddata = true
</trial>
Attachments
BASA clock pause.iqx (114 views, 67.00 KB)
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Reading This Topic

Explore
Messages
Mentions
Search