Debian Codegraph @

docs/_sources/examples/21-utilities/05-batch-processing.rst.txt

raw
1 Offline processing of multiple audio files in batch
2 ============================================================================================================================================
3
4
5 **05-batch-processing.py**
6
7 This program demonstrates how to use pyo to do offline batch processing
8 given a folder of sounds.
9
10 .. code-block:: python
11
12 import os
13 from pyo import *
14
15 # Initialize the Server in offline mode.
16 s = Server(audio="offline")
17
18 # Path to your sound folder (SNDS_PATH is a folder containing a few sounds installed with pyo,
19 # you can change this path to try with your own sounds).
20 folder_path = SNDS_PATH
21
22 # Path to the processed sounds folder (user's home directory/pyo_batch_fx/).
23 output_folder = os.path.join(os.path.expanduser("~"), "pyo_batch_fx")
24
25 # create the folder if it does not exist.
26 if not os.path.isdir(output_folder):
27 os.mkdir(output_folder)
28
29 # Get the list of audio files to process.
30 sounds = [f for f in os.listdir(folder_path) if sndinfo(os.path.join(folder_path, f)) is not None]
31
32 # Enter the batch processing loop.
33 for sound in sounds:
34 # Retrieve info about the sound from its header.
35 path = os.path.join(folder_path, sound)
36 info = sndinfo(path)
37 dur, sr, chnls = info[1], info[2], info[3]
38 fformat = ["WAVE", "AIFF", "AU", "RAW", "SD2", "FLAC", "CAF", "OGG"].index(info[4])
39 samptype = [
40 "16 bit int",
41 "24 bit int",
42 "32 bit int",
43 "32 bit float",
44 "64 bits float",
45 "U-Law encoded",
46 "A-Law encoded",
47 ].index(info[5])
48
49 # Set server parameters according to the current sound info.
50 s.setSamplingRate(sr)
51 s.setNchnls(chnls)
52 s.boot()
53 s.recordOptions(
54 dur=dur + 1, # give some room for the reverb trail!
55 filename=os.path.join(output_folder, sound),
56 fileformat=fformat,
57 sampletype=samptype,
58 )
59
60 # Simple processing applied to the sound.
61 source = SfPlayer(path)
62 bandpass = ButBP(source, 1000, 5)
63 disto = Disto(bandpass, drive=0.9, slope=0.8)
64 output = WGVerb(source + disto, feedback=0.8, cutoff=5000, bal=0.25, mul=0.5).out()
65
66 # Start the rendering.
67 s.start()
68
69 # Cleanup for the next pass.
70 s.shutdown()
71