psychopy.org | Reference | Downloads | Github

Behavior of sound.setVolume() seems inconsistent?

I am trying to play two sounds at the same time. A pink noise that onsets about 1.5 seconds before a word. PsychoPy is handling the simultaneous playing exactly as expected. However, the experimental manipulation is that while the pink noise always plays at full volume (1.0), the word should play at a lower volume (e.g. 0.5).

This is where the behavior gets weird. In the minimal working example below, I play the pink noise at full volume and the word at 0.5 volume for three trials. While .getVolume() returns the appropriate stimulus volume - that is, whatever I set in .setVolume() - the true volume that gets played is not consistent, as seen in this waveform recording of the 3 trials.

At the onset of the word (~1.5 seconds after the noise), the volume samples down for both the noise stimulus AND the word in the first trial. In the second trial, the initial volume of the pink noise is much lower. In the third trial, the behavior is different still (seems like the volume of the word is higher?)

I know that you can’t change the volume of a given stimulus once it’s started, but It seems like for simultenous sounds, volume is handled kind of like .draw() order for visual stims? I’m guessing it just plays both sounds at the same time out of the same channels, and then adjusts the volume of the channels whenever it gets any volume adjustment? (thus all sounds are effected?)

I’m hoping someone can confirm there is no way for psychopy to handle this before I try to find a satisfactory audio package to pull that can do precise loudness/gain manipulations and mixing (e.g. sox, pydub, pyaudio).

Thanks in advance!
Katie

    
    # psychopy version 1.84.1
    # OSX 10.11.5 (El Capitan)
    from psychopy import prefs, visual, sound, core

    # set the preferred audio drivers / libs 
    prefs.general['audioLib'] = ['pyo']
    prefs.general['audioDriver']= ['portaudio']


    # initialize the window and sounds
    exp_window = visual.Window(screen = 0, units = 'pix', color = 'gray',fullscr = False)
    pink_noise = sound.Sound(value = 'stims/pinknoise.wav', sampleRate = 48000)
    word = sound.Sound(value = 'stims/audio_easy/bead.wav', sampleRate = 48000)

    for trial in range(3):

        # set the volume of the pink noise to full volume
        pink_noise.setVolume(1.0)

        # flip the window
        exp_window.flip()

        # start a static period to play just the pink noise
        thisISI = core.StaticPeriod(screenHz = 60.0, name = "ISI")
        thisISI.start(1.0)
        pink_noise.play()

        # while you are doing that, set the volume of the word
        word.setVolume(0.5)

        # end the static period and start the word
        ISI_accurate = thisISI.complete()
        exp_window.flip()
        word.play()

        # when you start playing the word, return the volume 
        # of both stimuli and wait 2.5 seconds 
        print pink_noise.getVolume(), word.getVolume()
        core.wait(2.5)

Some hypothesis testing could be a good idea here. I have experienced audio problems as well, mostly in jittered timing. But if I recall correctly, also in sound duration, which was really strange! That was using the psychopy.sound.Sound function.

I think that you can do a more minimal script that would also allow us to debug the problem. After creating window, sound etc.:

def play():
    """ Play pink noise, superpose word after 1 sec, make it all last 4 secs"""
    pink_noise.play()
    core.wait(1)
    word.play()
    core.wait(3)

# Is it simply not able to replicate?
play()
play()  # same or different waveform?

# Is it setVolume()?
pink_noise.setVolume(1)
word.setVolume(1)
play()  # same or different waveform compared to last?

Post the recorded waveforms in a reply. Also, what sound card are you using? It would be helpful if you could try it out on another computer also, to see whether it’s specific to one audio card.

This was posted 3 years ago, but I’m wondering if anyone found a solution or the cause of the volume change? I’m having exactly the same problem - when my target tone plays each trial the volume of the pink background noise decreases. I’m using PsychoPy v3.2.3 and Mac OS Mojave v10.14.5 on Macbook Pro.
Thanks!