-
Notifications
You must be signed in to change notification settings - Fork 6
IO Guide
Every Signal has the methods play
and export
(only supports WAV).
The latter has the positional argument filename
,
and both have the following keyword arguments:
-
sample_rate
- defaults to 44100. Valid values are8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 96000
. Note that a Signal object doesn't know/care what sample rate it is supposed to be played in, even when it was directly loaded from a Wave file, so you will have to be explicit about sample rates other than 44.1 kHz. -
byte_width
- defaults to 2 (16-bit). Supported values are1,2,4
(TODO I think? there was something about floating points) -
max_amplitude
- defaults to 1. This means that regardless of the actual volume of the audio, amplitudes will always be shrunk/stretched to fit between -1 and 1. This behaviour isn't really desired and can cause unexpected results, but we're not sure what's the best way to deal with it yet. However, specifyingmax_amplitude=None
will keep all amplitudes exactly as they are.
Gensound signals are theoretical constructs that have no knowledge of the sample rate and byte width they are intended to have. Depending on whether we specify durations as milliseconds or number of samples, we may get very different results when exporting.
For a WAV or Raw Signal, use the resample
method which recomputes the associated audio stream so it can be played using the desired sample rate without affecting pitch.
from gensound import WAV, test_wav
WAV(test_wav).play(sample_rate=32000) # will sound lower in pitch, since test_wav is in 44.1 kHz
WAV(test_wav).resample(32000).play(sample_rate=32000) # playback will now be at proper pitch
Or in order to save the file using the new sample rate:
WAV(test_wav).resample(32000).export("new_file.wav", sample_rate=32000)
Since WAV/Raw data is cached, calling
resample
once will affect all other objects contained the same WAV file. We may choose to implement an option to keep both the original as well as the resampled copies, but if that bothers you, you're most likely looking for the Stretch transform instead. Either way, one workaround is to load several unique copies of the original wave file, and another one is usingRaw(WAV(filename).mixdown(original_sample_rate))
which effectively makes a unique copy in memory.
resample
is a method of Raw, inherited by WAV. Note that it is not a Transform, for good reasons. If you thought about using it to stretch the audio while altering the pitch, you are probably looking for the Stretch transform, which works very much the same.