Answered step by step
Verified Expert Solution
Question
1 Approved Answer
*** PYTHON LANGUAGE PLEASE *** Supplement content: Assignment 3: Working with Sound Files Read Section 5.3 of the Background document. Start a new cell following
*** PYTHON LANGUAGE PLEASE ***
Assignment 3: Working with Sound Files Read Section 5.3 of the Background document. Start a new cell following the guidelines in the Lab 1 template, dividing it into Parts A-D. A. Read in the files "train32.wav" and "tuball.wav" using either the scipy wavfile package or simpleaudio, saving the audio vectors and sampling frequencies in variables y1, y2, fsi, fs2, respectively. Print the number of channels to confirm that the train file has 1 channel and the tuba file has 2. B. Play the two sounds in their correct form and then with the sampling frequencies swapped. C. Determine the length of the two signals, and extract a section of the longer one that is the same length as the shorter one, called y3. Add two equal-length, single-channel signals to create y4. Play and write out y4 using sampling frequency fs1. D. Concatenate the signals in yl and y2, adding a 4 second pause between them. Call the new signal y5. Play the new signal and write it out to a file using sampling frequency fs1. 5.3 Reading, Writing and Playing Wav Files There are multiple packages available for working with audio files. We will use both simpleaudio and scipy.io. Whatever package you use, there are two important issues to be aware of. First, the digital file must always be associated with a sampling frequency fs-1/Ts. This means that when you read an audio file, you will get fs with it. Conversely, you need to specify fs when you save a file, or play the audio. If you play the file with a different fs than it was saved with, you will change the sound of the signal. (We'll try this in the lab.) Second, audio files may be mono (1 channel) or stereo (2 channels). If the file is mono, then the audio data you read in will come in the form of a vector or 1-dimensional array. If it is stereo, then the data will have a pair of such vectors (one for each speaker), which together form a 2- dimensional array. LL 2 LOUI It is important to keep track of this information. For example, in creating new sounds, we might want to concatenate different sounds. In order to get the desired result, you need all concatenated components to have the same sample rate and the same number of channels. You can read and write .wav files using the wavfile module from scipy.io, and you can read and play.wav files using the simpleaudio module. To do all three, we'll use both. With simpleaudio, the sampling frequency, channel and other information is bundled together with the data in a special WaveObject. With scipy.io, you have to keep track of the sampling frequency separately. Let's start with simpleaudio. The easy way to read a file, which we gave you earlier, creates an object that has the data, the sampling frequency, number of channels, and bytes per sample as attributes. (The signal is digitized in amplitude as well as time, characterized by bytes per sample.) Assuming that you used import simpleaudio as sa, you can extract this information and play the file as follows: Test the audio read and play functions wav_obj - sa. WaveObject. From_wave_file('train32.wav") fs - wav_obj.sample_rate channels - wav_obj.num_channels print('Sampling rate -', fs) print('Number of channels - channels play_obj - wav_obj.play() play_obj.wait_done() If you have raw data that you created (or read the file using scipy.io), you can first create a WaveObject, or just use play_obj sa .play_buffer(data, channels, nbytes, fs) where nbytes is 2 or 4, depending on data.dtype (int16' is 2). In either case, you should follow the play command with: play_obj.wait_done() particularly if you are playing more than one file. If you read .wav files using the wavfile module from scipy.io, then the sample rate is returned separately, as shown below for a stereo audio file. (Note that data.shape won't work for mono.) we acipy.to to read audio files from seipy. lo import wavfile as way Esl, datal - way.read('train32.wav) print('Train whistle has: sampling rate', fal,, of samples, lendatal), 'type', datal.dtype) 152, data2 a v.read(tuball.wav") len2, ch2 - data2.shape print("Taba has: sampling rate', f82,', of samples', len2, , of channels', cha, ', type', data2.dtype) If you have two-channel audio, you can extract the separate channels using data[:,ch) where ch is 0 or 1. Now let's create a new file that plays the two audio channels separately in sequence with a pause in between, and save the result as a new audio file. pause - np.zeros(int(2*fs)) # create a 2-sec pause data-data[:,0] datal - data[:,1] ptuba_datanp.concatenate([data, pause, data]) outfile='ptuba.wav way.write(outfile,fs,ptuba_data.astype('int16)) #extract channel 0 #extract channel 1 # insert pause between tubas #write wav file Supplement content:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started