| 1 |
Saving list of floats to audio file on disk |
| 2 |
============================================================================================================================================ |
| 3 |
|
| 4 |
|
| 5 |
**01-list-to-audio-file.py** |
| 6 |
|
| 7 |
This program generates 5 seconds of white noise with pure python function |
| 8 |
and saves the samples in a file called noise.aif on the Desktop. |
| 9 |
|
| 10 |
.. code-block:: python |
| 11 |
|
| 12 |
from pyo import savefile |
| 13 |
from random import uniform |
| 14 |
import os |
| 15 |
|
| 16 |
# Create the path for the audio file. |
| 17 |
home = os.path.expanduser("~") |
| 18 |
path = os.path.join(home, "Desktop", "noise.aif") |
| 19 |
|
| 20 |
# Audio file properties (sampling rate, duration and number of channels). |
| 21 |
sr = 44100 |
| 22 |
dur = 5 |
| 23 |
chnls = 2 |
| 24 |
|
| 25 |
# Generate a list of `chnls` sub-lists with `sr * dur` floats in each of them. |
| 26 |
samples = [[uniform(-0.5, 0.5) for i in range(sr * dur)] for i in range(chnls)] |
| 27 |
|
| 28 |
# Save the list of floats in an audio file on disk. |
| 29 |
savefile(samples=samples, path=path, sr=sr, channels=chnls, fileformat=1, sampletype=1) |
| 30 |
|