Configuring the audio encoding

Several changes are necessary to make these settings configurable. The first one is declaring a new sink item:

Configure = namedtuple('Configure', ['samplerate', 'bitdepth'])

This item contains the values of the two settings that can be configured. Then the mp3_to_flac function must take these values as parameters instead of hardcoding them. The following are the changes in it:

def mp3_to_flac(data, samplerate, bitdepth):
...
transformer.convert(samplerate=samplerate, n_channels=2, bitdepth=bitdepth)
...

The two samplerate and bitdepth parameters are forwarded to the convert function of the transformer. One last modification is needed to configure the encoding. It consists of saving the information of the Configure item, and using it when an audio file is transcoded:

def on_subscribe(observer):
samplerate = None
bitdepth = None

def on_next(item):
nonlocal samplerate
nonlocal bitdepth
if type(item) is Configure:
samplerate = item.samplerate
bitdepth = item.bitdepth
elif type(item) is EncodeMp3:
encoded_data = mp3_to_flac(
item.data, samplerate, bitdepth)
observer.on_next(
EncodeResult(id=item.id, key=item.key,
data=encoded_data))
else:
observer.on_error("unknown item: {}".format(type(item)))

The samplerate and bitdepth variables are defined in the scope of the on_subscribe function. They are declared as non-local in the on_next function so that they can be modified and used. When the Configure item is received, their value is updated, and when an EncodeMp3 item is received, these values are provided to the mp3_to_flac function.

Finally, the initialization of the encode driver must be updated to send a Configure item when the configuration file has been parsed:

    encode_init = (
config
.map(lambda i: encoder.Configure(
samplerate=i.encode.samplerate,
bitdepth=i.encode.bitdepth))
)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.67.16