Demo playwave Demo Experiment Run Experiment

Demo playwave

Jaap Murre

Introduces and defines a new function ‘playwave()’, which can be used to play a simple sound of a certain frequency, duration, loudness, and type (e.g., sine or square wave form) using only the HTML5 audio ‘context’.


The playwave() function is an example of how to package the fairly complex AudioContext functions in an easy to use NeuroTask Scripting function. We intend to make this function available as a ‘standard’ (built-in) function in the future. 

If you are a registered user and signed in, you can here copy this script and its stimuli to your own account, where you can edit it and change it in any way you want.

It is absolutely free to register (no credit card info asked!). You can then instantly copy this experiment with one click and edit it, change its accompanying texts, its landing page, stimuli, etc. Invite your colleagues, friends, or students to check out your experiment. There is no limit on how many people can do your experiment, even with a free account.

The catch? There is no catch! Just keep in mind that with a free account, you cannot collect data. For teaching that is usually not a problem. For research, prepaid data collection (unlimited subjects) starts as low as €10.00 for a 10-day period.

AudioContext = window.AudioContext || window.webkitAudioContext;
//This code with eval() is a work-around for problems with SJS, new and DOM classes:
var audioCtx; eval("audioCtx = new AudioContext();");

// Plays a sound wave of a certain frequency, duration, loudness and
// type (sine [default], square, sawtooth, or triangle)
function playwave(/* Hz */frequency,/* ms */duration,/* % */loudness,type) {
    frequency = frequency || 440; // Hz, default is the note a. 
    duration = duration || 500; // ms
    loudness = loudness/100 || 1; // Default is 100%
    type = type || 'sine'; // Other options: "square", "sawtooth", "triangle"
    var oscillator = audioCtx.createOscillator();
    oscillator.type = type;
    oscillator.frequency.value = frequency;
    var gainNode = audioCtx.createGain();
    oscillator.connect(gainNode);
    gainNode.connect(audioCtx.destination);  
    gainNode.gain.value = loudness;
    oscillator.start();
    // await(duration);
    await(duration);
    oscillator.stop();
}

notes = [
  [659, 4],
  [659, 4],
  [659, 4],
  [523, 8],
  [0, 16],
  [783, 16],
  [659, 4],
  [523, 8],
  [0, 16],
  [783, 16],
  [659, 4],
  [0, 4],
  [987, 4],
  [987, 4],
  [987, 4],
  [1046, 8],
  [0, 16],
  [783, 16],
  [622, 4],
  [523, 8],
  [0, 16],
  [783, 16],
  [659, 4]
];

for (var i = 0, tempo = 100; i < notes.length; i++) {
    playwave(notes[i][0],1000*256/(notes[i][1]*tempo),1,'square');
}
This experiment has no files of this type
This experiment has no files of this type
This experiment has no files of this type
This experiment has no YouTube links
This experiment has no files of this type