music.write_wav_mono

music.write_wav_mono(sonic_vector=None, filename='asound.wav', sample_rate=None, fades_ms=0, bit_depth=16, remove_bias=True, normalize=True, **renamed)[source]

Write a mono sound to a WAV or FLAC file.

To hear a sound without writing it, play_audio() plays it.

Parameters:
sonic_vectorarray_like, optional

The PCM samples to be written: (nsamples,), or a single row, (1, nsamples). Unless normalize is False they are normalized by normalize_mono(sonic_vector) to have samples between -1 and 1. Defaults to about two seconds of uniform noise.

filenamestr

The path to write. .wav and .flac are understood.

sample_ratescalar or None

The sample frequency. None, the default, is the rate music.get_sample_rate() returns.

fades_msiterable or scalar

Milliseconds of fade in and fade out (to avoid clicks), either as a pair or as a single value applied to both ends.

bit_depthinteger

The number of bits in each sample of the final file: 8, 16, 24 or 32 for WAV, and 8, 16 or 24 for FLAC, which has no 32-bit form. Any other value raises ValueError.

remove_biasbool

Whether to remove or not the bias (or offset). Part of normalizing, so it has no effect when normalize is False.

normalizebool

Normalize to full scale, as by default, or write the samples as they are. Normalizing makes every file peak at full scale, so a piece written a phrase to a file loses its dynamics; False keeps them, and refuses a sound reaching past [-1, 1].

**renamed

fades, the name fades_ms had before it said its unit. Still accepted, with a DeprecationWarning.

Raises:
ValueError

If the extension or bit depth is not one this package writes, the sound is empty or has more than one channel, or, with normalize=False, it reaches past [-1, 1]; or if fades_ms is neither one value nor two, or is negative.

Parameters:
  • sonic_vector (ArrayLike | None)

  • filename (str)

  • sample_rate (int | None)

  • fades_ms (Sequence[int] | int)

  • bit_depth (int)

  • remove_bias (bool)

  • normalize (bool)

  • renamed (Any)

Return type:

None

See also

normalize_mono

Normalizes an array to [-1,1]

write_wav_stereo

Write a stereo file.

write_audio

Write mono or stereo, WAV or FLAC, as the array and the extension say.

play_audio

Play a sound rather than write it.

Examples

>>> write_wav_mono(note(freq=440, duration=0.5), "tone.wav")
>>> read_audio("tone.wav").shape
(22050,)
>>> write_wav_mono(note(), "faded.wav", fades_ms=(50, 200))  # in, out
>>> write_wav_mono(0.1 * note(), "quiet.wav", normalize=False)