Explanations of warnings and how to get rid of them#

Sometimes when you’re working with thebeat you’ll encounter warnings. While useful, they can of course become annoying at some point. Even though it’s possible to simply disable all these warnings (see at the bottom of this page), we believe it’s better practice to look into the origin of the warning, and solve it there.

Below you’ll find a number of common warnings and an explanation of where they originate.

Frame rounding#

What does the warning look like?#

For one or more of the used sounds, the exact start or end positions in frames (i.e. samples) were rounded off to the neirest integer ceiling. This shouldn’t be a problem. To get rid of this warning, try rounding off the onsets in the passed Sequence object by calling Sequence.round_onsets() before passing the object to the SoundSequence constructor.

When can you encounter this warning?#

You can encounter this warning when creating a SoundSequence object.

What is the reason for this warning?#

Digital sounds contain a certain number of ‘samples’. The samples are the points at which the amplitude of the sound is specified. There is a fixed number of samples per second (the default in thebeat is 48000, another common value is 44100). When creating a SoundSequence object, thebeat tries to place each individual sound at its respective onset (i.e. t value). The onset is then calculated in terms of the specific sample at which the sound should start, but if the onsets specified in the passed Sequence object do not correspond to a round-number sample (i.e. it falls in between two samples), the exact sample onset is rounded off.

What is the solution?#

As the warning specifies, try rounding off the Sequence object’s onsets before constructing the SoundSequence object.

Example with warning#

[8]:
from thebeat import Sequence, SoundStimulus, SoundSequence

sound = SoundStimulus.generate()
sequence = Sequence.generate_random_normal(n_events=10, mu=500, sigma=25)
print(sequence)
ss = SoundSequence(sound, sequence)
Object of type Sequence (ends with event)
10 events
IOIs: [467.45162561 518.69810293 505.72554864 465.24612211 509.7137021
 507.3505338  469.09078447 547.6717761  465.732822  ]
Onsets: [   0.          467.45162561  986.14972854 1491.87527718 1957.12139929
 2466.83510139 2974.18563519 3443.27641966 3990.94819576 4456.68101776]
Sequence name: Not provided

/Users/jellevanderwerff/thebeat/thebeat/core/soundsequence.py:511: UserWarning: thebeat: For one or more of the used sounds, the exact start or end positions in frames (i.e. samples) were rounded off to the neirest integer ceiling. This shouldn't be a problem. To get rid of this warning, try rounding off the onsets in the passed Sequence object by calling Sequence.round_onsets() before passing the object to the SoundSequence constructor.
  warnings.warn(thebeat._warnings.framerounding_soundseq)

Example without warning#

[9]:
from thebeat import Sequence, SoundStimulus, SoundSequence

sound = SoundStimulus.generate()
sequence = Sequence.generate_random_normal(n_events=10, mu=500, sigma=25)
sequence.round_onsets(decimals=2)
print(sequence)
ss = SoundSequence(sound, sequence)
Object of type Sequence (ends with event)
10 events
IOIs: [482.52 494.21 493.3  482.16 476.   560.04 470.57 454.92 452.81]
Onsets: [   0.    482.52  976.73 1470.03 1952.19 2428.19 2988.23 3458.8  3913.72
 4366.53]
Sequence name: Not provided

Normalization#

What does the warning look like?#

Sound was normalized to avoid distortion. If undesirable, change the amplitude of the sounds.

When can you encounter this warning?#

You can encounter this warning when creating a SoundSequence, Melody, or SoundStimulus object. The most common is when overlaying sounds. Consider the following code:

[10]:
from thebeat import SoundStimulus

stim_0 = SoundStimulus.generate()
stim_0.plot_waveform(title="stim_0")
stim_1 = SoundStimulus.generate()
stim_1.plot_waveform(title="stim_1")

stim_overlayed = stim_0.merge(stim_1)
/Users/jellevanderwerff/thebeat/thebeat/helpers.py:292: UserWarning: thebeat: Sound was normalized to avoid distortion. If undesirable, change the amplitude of the sounds.
  warnings.warn(thebeat._warnings.normalization)
../../_images/examples_tipstricks_warnings_17_1.png
../../_images/examples_tipstricks_warnings_17_2.png

As you can see the amplitudes of the two stimuli run from -1 to 1. When these sounds are overlayed both sounds’ amplitudes are summed. The overlayed sound would therefore have amplitudes that run from -2 to 2, resulting in distorted sound. thebeat automatically normalizes the sound so that the amplitudes run from -1 to 1 again.

What is the solution?#

As the warning specifies, you can change the amplitude of the sounds before overlaying them. This also ensures that sounds with a different frequency will be equally loud, for instance:

[11]:
from thebeat import SoundStimulus
stim_0 = SoundStimulus.generate(freq=220)
stim_0.change_amplitude(factor=0.7)
stim_1 = SoundStimulus.generate(freq=440)
stim_1.change_amplitude(factor=0.3)

stim_overlayed = stim_0.merge(stim_1)

Getting rid of a specific warning#

While it is better to find the cause of the warning and to solve it there, during development it might be frustrating to get these warnings, especially if you they are not relevant to your situation. To disable a specific warning use the message argument in `warnings.filterwarnings() <https://docs.python.org/3/library/warnings.html#warnings.filterwarnings>`__. You don’t have to type the whole message, just part of it, for instance:

[12]:
import warnings
warnings.filterwarnings('ignore', message="thebeat: For one or more of the used sounds")

Getting rid of all of thebeat’s warnings#

All of thebeat’s warnings start with the message ‘thebeat’. You can thus filter all thebeat’s warning like so:

[13]:
import warnings
warnings.filterwarnings('ignore', message="thebeat: ")

Resetting warnings#

To reset the warnings back to their original behaviour, use:

[14]:
warnings.resetwarnings()