Audio volume does not change

I am new to psychtoolbox and love it! It’s by far the best software for experiments I’ve used… I’ve managed to code a task with visual stimulation and that all works perfectly. I am struggling a bit adding an auditory stimulus that is meant to change volume. However, it seems to always play the same volume. Here is minimal code that has this problem (my actual code also changes pitch and number of beeps, but I’ve deleted these parts as the problem does not depend on them). There are no error messages that come up.

I’d be very grateful for any advice!

% Perform basic initialization of the sound driver:
InitializePsychSound(1);

% Number of channels and Frequency of the sound
nrchannels = 2;
freq = 48000;

% hould we wait for the device to really start(1=yes), info:see help
% psychportaudio
waitForDeviceStart=1;

% Open Psych-Audio port, with the follow arguements
% (1) [] = default sound device
% (2) 1 = sound playback only
% (3) 1 = default level of latency
% (4) Requested frequency in samples per second
% (5) 2 = stereo putput
pahandle = PsychPortAudio('Open', [], 1, 1, freq, nrchannels);


% Settings for the sound
sound_pause= 0.01; % pause between beeps
total_sound_duration =0.3; % durations of the total train of beeps

% We want a loop to play sounds that differ in volume
vals = 0:0.1:1;
for iv = 1:length(vals)
    % Set the volume
     PsychPortAudio('Volume', sound_settings.pahandle, vals(iv));
    
    % Make a beep 
    cur_freq         = 500;
    cur_beepDuration = total_sound_duration - sound_settings.sound_pause;
    myBeep = MakeBeep(cur_freq, cur_beepDuration, 48000); 
    
    % Fill the audio playback buffer with the audio data, doubled for stereo presentation
    PsychPortAudio('FillBuffer', pahandle, [myBeep; myBeep]);
    
    % Set start delay for first beep, others will be computed in loop
    start_delay=0;  %'start immediately' - will later be replaced by start_delay specified above so that we get pauses between the beeps
   
    % Play the sound (with a pause before to get a series of beeps,
    % rather than just one long sound)
    WaitSecs(2)
    

    PsychPortAudio('Start',pahandle,1,start_delay,waitForDeviceStart)
    % get exact time when the beep stopped
    [actualStartTime, ~, ~, estStopTime] = PsychPortAudio('Stop',pahandle,1,1);

end

Additional info:
3.0.17 - Flavor: beta - Corresponds to SVN Revision 12153
Mac OS 10.15 (task will later be run on a windows computer; data collection behaviour + MEG), but to program I have a Mac… Matlab 2018a

========================================================================================

H77ZC-NL-202192413322:cc231de4e3094ca12b931ef0ced579a366890ed8ba2031cf6f784136bb7fa954

========================================================================================

Glad to hear that you like it! I can’t see anything wrong with your sample code, apart from that it specifies pahandle everywhere, except for the ‘Volume’ call, where sound_settings.pahandle is used? Similar for the sound_pause and total_sound_duration variables?

Has this code snippet been tested to work? Or are these actual copy & paste errors? if your script truly would use pahandle sometimes and sound_settings.pahandle other times, then the explanation could be that you are changing the volume of a different sound device sound_settings.pahandle than the one actually used for playing the beep tones (pahandle)?

The relevant driver code has not been changed in almost a decade and works here, so a PTB bug is very unlikely.

[Time spent so far: 40 Minutes.]

Thanks for looking at this. I’m glad that there is nothing obvious that is wrong with how I’m using the call to PsychPortAudio(‘Volume’). (Sorry, the “sound_settings” should have gone - I had forgotten to put clear all on top of script when testing this shorter version I’ve tried it with sound_settings removed now and it is still the same.)

Though one thing I am noticing is that - while the volume does not change - there is a low frequency hum for what is meant to be low volume levels that gets added to the pure tone. I have tried this also looping from length(vals):-1:1 to make sure it’s nothing to do with later sounds versus earlier ones. And it is really the case that when Volume is meant to be low there is a low frequency hum.

I’m wondering now whether it’s something to do about using a Mac. I’ll try to find a colleague with a windows computer to try it.

I tested your script now on Linux, Windows-10 and macOS 10.15.7.
Your script works just fine on Linux and Windows.

It’s yet another macOS screwup. I get the same, essentially almost no perceptible change in loudness. I think macOS is normalizing whatever signal amplitude it gets from our driver to some standard amplitude, rescaling the signal accordingly, thereby undoing the volume change. You can hear it if you set Volume to zero, where it indeed turns silent.

Or if you use BasicAMAndMixScheduleDemo(), at the end you hear a mix of AM modulates beep tones and some white-noise. If you use the cursor keys to crank up the amplitude of the beep tone, you won’t hear a louder beep tone, but instead attenuation of the white-noise whenever the beep tone happens, because the signal as a whole is scaled down by the operating system to keep peak amplitude (== amplitude of the beep tone) constant.

So i guess auditory stimulation is another area where modern macOS fails us by default. Maybe the signal processing is good for something else, like regular music playback, or maybe using something else (external speakes? Headphones? External soundcard?) but the internal MacBookPro speakers would avoid the auto-rescaling.

Not sure with which OS version or edition of Macs this behaviour was introduced or changed for the worse, but not a bug in PTB or your script.

Edit: Related thread of other people noticing this effect:

Also, if you change the sound vector to white noise, you’ll hear quite different behaviour, hinting that the dynamic amplification/dampening by the OS may be frequency specific.

Best,
-mario

[Time spent so far: 73 Minutes, priority support used up]

As a followup: UpdatePsychtoolbox to PTB 3.0.18 is advised. It had some improvements to the PsychPortAudio sound driver, which may be able to work around some of the macOS audio volume deficiencies. Seemed to work on my Catalina MBP 2017, maybe it also helps on yours.

Of course, upgrading to Linux would be still the better/safer course of action…

-mario

Thanks! I’ve changed my task now to not rely on volume - it’s only for training the participant that I’ll have to use a Mac; luckily for the MEG experiment we have a windows computer (where so far everything seems to run fine).