Crackling at the end of sound when playing certain frequencies

853 views
Skip to first unread message

Benjamin Christensen

unread,
Jun 14, 2013, 7:07:06 AM6/14/13
to psychop...@googlegroups.com
We are designing a adaptive staircase experiment, trying to establish the pitch detection threshold of the participants. We just recently migrated the code from matlab, so we are pretty new to PsychoPy.

We are using the Coder view and we have based our scripts on the existing examples in PsychoPy, JND_staircase_exp.py and soundStimuli.py.

The issue relates to the quality of the sound generated with the sound-function. We are experiencing an annoying crackling sound at the end of each sound played.

The issue is not so bad in the soundStimuli.py example where an A is played. But for the frequencies we are using the problem is aggravated. We tried tweaking the sound parameters without any luck. We also tried the fading function, but it only makes matters worse with repeated crackling while fading the sound - maybe we are using it wrong?

This script generates the crackling sound, on our system at least:

import sys
from psychopy import logging, prefs
logging
.console.setLevel(logging.DEBUG)#get messages about the sound lib as it loads


from psychopy import sound,core, visual
if prefs.general['audioLib'][0] == 'pyo':
   
#if pyo is the first lib in the list of preferred libs then we could use small buffer
   
#pygame sound is very bad with a small buffer though
    sound
.init(48000,buffer=128)
print 'Using %s(with %s) for sounds' %(sound.audioLib, sound.audioDriver)


highA
= sound.Sound(523, sampleRate=44100, secs=0.8, bits=8)
highA
.setVolume(0.5)


highA
.play()
core
.wait(2)

highA
.play().fadeOut(800) #a lot of crackling here as well, maybe we are not using the fadeOut() function correctly?
core
.wait(2)


print 'done'
core
.quit()

Console output:
##### Running: C:\Users\Benjamin\workspace\CillephD\nyt_script\soundStimuli2.py #####
Using pygame(with None) for sounds
done
0.6719 ERROR avbin.dll failed to load. Try importing psychopy.visual as the first
    library
(before anything that uses scipy) and make sure that avbin is installed.
0.9309 EXP Set  sound=523.0
0.9309 EXP Set Sound  volume=0.500
0.9309 EXP Sound  started
2.9310 EXP Sound  started

Some specs:

PsychoPy v. 1.76.00
Windows 7, 64 bit
Dell XPS 15z

Hope someone can give us ideas on how to improve the sound quality!

Thanks for making this open-source alternative to matlab and e-prime!

Best regards, 
Benjamin Christensen

Jonathan Peirce

unread,
Jun 14, 2013, 7:58:47 AM6/14/13
to psychop...@googlegroups.com
Thanks for nice clear report :-)

Not exactly clear why you'd be having these issues but the two general probs with sound are a) incompatible bit-rates (a 44kHz sound playing on a 22010 stream has to be interpolated) or b) sounds not filling the buffer as fast as the sound is played

Things to try:
    - have a go with the pyo library instead of pygame - go to your preferences and change the order of them
    - different audio rates/buffer sizes.
        With pyo selected you can control the rates/buffers using the code you can see below
        With pygame you can use
            sound.initPygame(rate=22050, bits=16, stereo=True, buffer=1024)

let us know how you get on
Jon
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/e61a784b-7361-4463-b7f3-72337ef37207%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.


Jonas Lindeløv

unread,
Jun 14, 2013, 5:34:18 PM6/14/13
to psychop...@googlegroups.com
Just wanted to add that I have always heard the clicks at tone onset and offset too, much worsened by the fading. And this counts for all computers I've tried it on (at least 5). I just figured that it was due to an abrupt onset and offset of a sine-wave, which actually decomposes to a lot of high frequencies. If this is the case, the solution would be to add an option to superpose the sound with a hamming-window-like fade-in-out at onset and offset?

I just tested some of the suggestion Jon offered and I still hear identical clicks on all sounds played here (i.e. with all parameters).

Using pyo, 
psychopy 1.76, 
windows 8, 
some realtek audio card

from psychopy import core, sound

octaves = [3, 6]

notes = ['A','B']

sampleRates = [22050, 44100]

bits = [4, 16]

buffers = [64, 1024]


for buffer in buffers:

   for sampleRate in sampleRates:

       sound.init(sampleRate, buffer)

       for bit in bits:

           for octave in octaves:

               for note in notes:

                   stim = sound.Sound(value=note, octave=octave, bits=bit, secs=0.2)

                   stim.play()

                   print 'playing params:', buffer, sampleRate, bit

                   core.wait(0.4)


Best,
Jonas

Jonathan Peirce

unread,
Jun 17, 2013, 6:01:29 AM6/17/13
to psychop...@googlegroups.com
Oh, I see. We need to add something to the creation of the sound to give it a smooth onset/offset. Hmm. OK. Anyone fancy contributing that? Would be relatively easy. Needs a slight change to the sound.py file in the _SoundBase._fromFreq() method to add a window to the start/end of the array that gets created. Each of the sound library-specific sound classes (PygameSound, PyoSound etc) will then use that appropriately.

cheers,
Jon

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jeremy Gray

unread,
Jun 17, 2013, 8:08:49 AM6/17/13
to psychop...@googlegroups.com
I hear ugly clicking too, good to fix it. Is it only from generated (_fromFreq) sounds, or potentially all sounds (_fromFile, _fromArray)?

This code attenuates the clicking quite a bit but the offset still sounds like there's a "thud" or something:
    def _fromFreq(self, thisFreq, secs):
        nSamples = int(secs*self.sampleRate)
        outArr = numpy.arange(0.0,1.0, 1.0/nSamples)
        outArr *= 2*numpy.pi*thisFreq*secs
        outArr = numpy.sin(outArr)

        # apodize the sound:
        samp = min(200, nSamples // 15)
        hamm = numpy.hamming(2*samp + 1)
        outArr[:samp] *= hamm[:samp]
        outArr[-samp:] *= hamm[samp+1:]

        self._fromArray(outArr)

test:
>>> from psychopy import sound; sound.Sound().play()

Is there room for improvement in the sound quality? I am not completely sure what to listen for.

Jeremy Gray

unread,
Jun 17, 2013, 12:38:53 PM6/17/13
to psychop...@googlegroups.com
Hi Benjamin,

With essential input from Jonas and Jon, I just added some code that helps with this crackling. It will be available in version 1.78 (later this summer). In the meantime, the key bit of the code is here, line ~221 in https://github.com/jeremygray/psychopy/commit/03a40c684141bb4d8f69976d8485fd9737ae21ee

You could edit your copy of sound.py to do the same thing. (No need to have the parameter 'hamming'). Or you could copy that code, and use it to generate your own array, and then pass the array to Sound(), to avoid needing to edit sound.py.

--Jeremy



Benjamin Christensen

--

Tudor

unread,
Dec 2, 2015, 9:16:42 AM12/2/15
to psychopy-users
Dear all,

I also hear crackling noises while my auditory stimuli are being played. It seems to be randomly occurring, i.e. some trials are fine, while in others the crackling is quite noticeable. Am running v1.83.01 on Windows 7, using Intel high definition HDMI sound drivers.

Cheers,
T

Tudor Popescu

unread,
Dec 2, 2015, 9:44:33 AM12/2/15
to psychop...@googlegroups.com
Also (asking this in the same thread as might have a common source), when a sound finishes playing, it sounds like a very abrupt cut-off. This is probably due to the audio "channel" closing at the end of playback, rather than due to the stimulus itself, which in fact has a smooth fade-out. Is there a way to keep this channel open for the entire length ofthe experiment, such that the starts and ends of various audio stimuli don't themselves cause an abrupt click to be heard?

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/zn6Gqp1ehvM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

Jeremy Gray

unread,
Dec 2, 2015, 9:52:09 AM12/2/15
to psychop...@googlegroups.com
I am not sure about the audio channel, but thats a great idea. It may or may not be the cause of all of the crackling, and I am not sure how long channels are kept open by default (which might vary across OSes, etc).

To try to reducing clicking / crackling, by default, a hamming window of 5ms duration is applied to sounds that are requested as tones or notes. It is not applied to sounds given as arrays or from a filename, in case you don't want that -- if you do want it, you need to apply it yourself.. (A hamming window basically smooths the onset and offset, to allow it to ramp up / down over 5ms instead of slamming right into whatever the first sample is.)

--Jeremy

Tudor Popescu

unread,
Dec 2, 2015, 10:04:29 AM12/2/15
to psychop...@googlegroups.com
Many thanks Jeremy. My stimuli are all WAV files, and so I guess PsychoPy would not hamming-window them. Each stimulus has a smoothfade-in&-out built into the waveform itself, so the on/offset crackling is most likely just due to that master audio channel (or whatever it's called) getting switched on and off, and it probably makes no sense to add additional windowing to the WAVs. I understand there's nothing to do at the moment to prevent that? And same for the general crackling superimposed upon playback?

Jeremy Gray

unread,
Dec 2, 2015, 10:09:51 AM12/2/15
to psychop...@googlegroups.com

Many thanks Jeremy. My stimuli are all WAV files, and so I guess PsychoPy would not hamming-window them.

Yes
 
Each stimulus has a smoothfade-in&-out built into the waveform itself, so the on/offset crackling is most likely just due to that master audio channel (or whatever it's called) getting switched on and off, and it probably makes no sense to add additional windowing to the WAVs. I understand there's nothing to do at the moment to prevent that?

You could try it and see if it helped. You'd have to read each wav file into memory, apply the hamming window, and then create a sound.Sound() instance from that array.
 
And same for the general crackling superimposed upon playback?

Not sure exactly what you mean about general crackling. Throughout the sound, not just onset and offset?

Also, not sure if its acceptable to your experiment but you could also try playing very very quite white noise continuously to see if that helps keep the speakers responsive, etc

--Jeremy


 

Tudor Popescu

unread,
Dec 2, 2015, 10:12:42 AM12/2/15
to psychop...@googlegroups.com
thanks will consider the idea of adding some baseline noise. With the crackling I did indeed mean throughout the sound, not just onset and offset. A bit like listening to a scratched vinyl/CD.

Jeremy Gray

unread,
Dec 2, 2015, 10:15:10 AM12/2/15
to psychop...@googlegroups.com
If its throughout, I don't think that is something that PsychoPy can help with. (I could be wrong of course.) Do you also hear that crackling when using headphone, etc?

--Jeremy

Tudor Popescu

unread,
Dec 2, 2015, 10:37:13 AM12/2/15
to psychop...@googlegroups.com
By 'throughout' I mean that the crackling occurs at random points throughout the length of any one stimulus, AND only sometimes (not always the same WAV file).
Yes it is heard regardless if I have speakers or headphones on, i.e. it probably has to do with how the sound is rendered inside psychopy. When listening to these WAVs in a media player, there is no crackling.
I realise this problem may be v difficult to debug, as I can't really provide conditions to reproduce it...

Jeremy Gray

unread,
Dec 2, 2015, 10:39:29 AM12/2/15
to psychop...@googlegroups.com
Hmm, ok. Try using 'pyo' instead of 'pygame' if you are not already doing so.

--Jeremy

Tudor Popescu

unread,
Dec 2, 2015, 11:40:26 AM12/2/15
to psychop...@googlegroups.com
Not sure what those are (sorry) - I don't have 'pygame' in the source code that Builder generates for my expt..

Jeremy Gray

unread,
Dec 2, 2015, 11:50:24 AM12/2/15
to psychop...@googlegroups.com
'pyo' is a preference setting for which sound library to use. Set it under General > audio lib, make sure 'pyo' comes before 'pygame' in the list, like
['pyo', 'pygame']

--Jeremy

Tudor Popescu

unread,
Dec 2, 2015, 12:38:40 PM12/2/15
to psychop...@googlegroups.com
Oh, right. It was already set up like that..

Richard Höchenberger

unread,
Dec 6, 2015, 10:06:04 AM12/6/15
to psychop...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages