Complete the code to read an audio file using the soundfile library.
import soundfile as sf data, samplerate = sf.read([1])
The sf.read() function requires the filename as a string, so it must be enclosed in quotes.
Complete the code to write audio data to a file with a given sample rate.
import soundfile as sf sf.write('output.wav', [1], 44100)
The sf.write() function needs the audio data array as the second argument.
Fix the error in the code to read a stereo audio file and print its shape.
import soundfile as sf data, samplerate = sf.read('stereo.wav') print(data[1])
shape like a function.The shape attribute of a NumPy array is accessed without parentheses.
Fill in the blank to create a dictionary with filenames as keys and their sample rates as values.
files = ['a.wav', 'b.wav', 'c.wav'] sample_rates = {file: [1] for file in files}
The sf.read(file) returns a tuple (data, samplerate). We want to store the sample rate as the value, which is the second item at index 1.
Fill all three blanks to filter audio files longer than 5 seconds and create a dictionary with filename and duration.
files = ['x.wav', 'y.wav', 'z.wav'] durations = { [1] : [2] / [3] for [1] in files if (data, samplerate) := sf.read([1]) and len(data) / samplerate > 5 }
The dictionary comprehension uses the filename as key, and duration is calculated by dividing the length of data by the sample rate.
