def output(partIdx): """Uses the student code to compute the output for test cases.""" outputString = '' if partIdx == 0: # This is ScaledFFTdB from assignment1 import scaled_fft_db r,x = wavfile.read('data/a1_submissionInput.wav') X = scaled_fft_db(x) for val in X: outputString += '%.5f ' % (val) elif partIdx == 1: # This is PrototypeFilter from assignment2 import prototype_filter h = prototype_filter() # test signal s = np.loadtxt('data/a2_submissionInput.txt') r = np.convolve(h, s)[4*512:5*512]/2 for val in r: outputString += '%.5f ' % val elif partIdx == 2: # This is SubbandFiltering from assignment3 import subband_filtering r,x = wavfile.read('data/a3_submissionInput.wav') h = np.hanning(512) X = subband_filtering(x, h) for val in X: outputString += '%.5f ' % (val) elif partIdx == 3: # This is Quantization from assignment4 import quantization from parameters import EncoderParameters params = EncoderParameters(44100, 2, 64) val_in = np.loadtxt('data/a4_submissionInput.txt') for r,row in enumerate(val_in): val = row[0] scf = row[1] ba = int(row[2]) QCa = params.table.qca[ba-2] QCb = params.table.qcb[ba-2] val = quantization(val, scf, ba, QCa, QCb) outputString += '%d ' % (val) return outputString.strip()
def output(partIdx): """Uses the student code to compute the output for test cases.""" outputString = '' if partIdx == 0: # This is ScaledFFTdB from assignment1 import scaled_fft_db r, x = wavfile.read('data/a1_submissionInput.wav') X = scaled_fft_db(x) for val in X: outputString += '%.5f ' % (val) elif partIdx == 1: # This is PrototypeFilter from assignment2 import prototype_filter h = prototype_filter() # test signal s = np.loadtxt('data/a2_submissionInput.txt') r = np.convolve(h, s)[4 * 512:5 * 512] / 2 for val in r: outputString += '%.5f ' % val elif partIdx == 2: # This is SubbandFiltering from assignment3 import subband_filtering r, x = wavfile.read('data/a3_submissionInput.wav') h = np.hanning(512) X = subband_filtering(x, h) for val in X: outputString += '%.5f ' % (val) elif partIdx == 3: # This is Quantization from assignment4 import quantization from parameters import EncoderParameters params = EncoderParameters(44100, 2, 64) val_in = np.loadtxt('data/a4_submissionInput.txt') for r, row in enumerate(val_in): val = row[0] scf = row[1] ba = int(row[2]) QCa = params.table.qca[ba - 2] QCb = params.table.qcb[ba - 2] val = quantization(val, scf, ba, QCa, QCb) outputString += '%d ' % (val) return outputString.strip()
def output(partIdx): outputString = "" if partIdx == "1": # This is ScaledFFTdB from assignment1 import scaled_fft_db r, x = wavfile.read("data/a1_submissionInput.wav") X = scaled_fft_db(x) for val in X: outputString += "%.5f " % (val) elif partIdx == "2": # This is PrototypeFilter from assignment2 import prototype_filter h = prototype_filter() # test signal s = np.loadtxt("data/a2_submissionInput.txt") r = np.convolve(h, s)[4 * 512:5 * 512] / 2 for val in r: outputString += "%.5f " % val elif partIdx == "3": # This is SubbandFiltering from assignment3 import subband_filtering r, x = wavfile.read("data/a3_submissionInput.wav") h = np.hanning(512) X = subband_filtering(x, h) for val in X: outputString += "%.5f " % (val) elif partIdx == "4": # This is Quantization from assignment4 import quantization from parameters import EncoderParameters params = EncoderParameters(44100, 2, 64) val_in = np.loadtxt("data/a4_submissionInput.txt") for r, row in enumerate(val_in): val = row[0] scf = row[1] ba = int(row[2]) QCa = params.table.qca[ba - 2] QCb = params.table.qcb[ba - 2] val = quantization(val, scf, ba, QCa, QCb) outputString += "%d " % (val) else: print("Unknown assigment part number") if len(outputString) > 0: fileName = "res%s.txt" % partIdx with open(fileName, "w") as f: f.write(outputString.strip()) print("You can now submit the file " + fileName) else: print( "there was an error with the computation. Please check your code")
def output(partIdx): outputString = '' if partIdx == '1': # This is ScaledFFTdB from assignment1 import scaled_fft_db r,x = wavfile.read('data/a1_submissionInput.wav') X = scaled_fft_db(x) for val in X: outputString += '%.5f ' % (val) elif partIdx == '2': # This is PrototypeFilter from assignment2 import prototype_filter h = prototype_filter() # test signal s = np.loadtxt('data/a2_submissionInput.txt') r = np.convolve(h, s)[4*512:5*512]/2 for val in r: outputString += '%.5f ' % val elif partIdx == '3': # This is SubbandFiltering from assignment3 import subband_filtering r,x = wavfile.read('data/a3_submissionInput.wav') h = np.hanning(512) X = subband_filtering(x, h) for val in X: outputString += '%.5f ' % (val) elif partIdx == '4': # This is Quantization from assignment4 import quantization from parameters import EncoderParameters params = EncoderParameters(44100, 2, 64) val_in = np.loadtxt('data/a4_submissionInput.txt') for r,row in enumerate(val_in): val = row[0] scf = row[1] ba = int(row[2]) QCa = params.table.qca[ba-2] QCb = params.table.qcb[ba-2] val = quantization(val, scf, ba, QCa, QCb) outputString += '%d ' % (val) else: print "Unknown assigment part number" if len(outputString) > 0: fileName = "res%s.txt" % partIdx; with open(fileName, "w") as f: f.write(outputString.strip()) print "You can now submit the file " + fileName else: print "there was an error with the computation. Please check your code"
def main(inwavfile, outmp3file, bitrate): """Encoder main function.""" #inwavfile = "../samples/sinestereo.wav" #outmp3file = "../samples/sinestereo.mp3" #bitrate = 320 # Read WAVE file and set MPEG encoder parameters. input_buffer = WavRead(inwavfile) params = EncoderParameters(input_buffer.fs, input_buffer.nch, bitrate) # Subband filter calculation from baseband prototype. # Very detailed analysis of MP3 subband filtering available at # http://cnx.org/content/m32148/latest/?collection=col11121/latest # Read baseband filter samples """ ASSIGNMENT 2 """ baseband_filter = assignment2.prototype_filter().astype('float32') subband_samples = np.zeros((params.nch, N_SUBBANDS, FRAMES_PER_BLOCK), dtype='float32') # Main loop, executing until all samples have been processed. while input_buffer.nprocessed_samples < input_buffer.nsamples: # In each block 12 frames are processed, which equals 12x32=384 new samples per block. for frm in range(FRAMES_PER_BLOCK): samples_read = input_buffer.read_samples(SHIFT_SIZE) # If all samples have been read, perform zero padding. if samples_read < SHIFT_SIZE: for ch in range(params.nch): input_buffer.audio[ch].insert( np.zeros(SHIFT_SIZE - samples_read)) # Filtering = dot product with reversed buffer. """ ASSIGNMENT 3 : Subband filtering """ for ch in range(params.nch): subband_samples[ch, :, frm] = assignment3.subband_filtering( input_buffer.audio[ch].reversed(), baseband_filter) # Declaring arrays for keeping table indices of calculated scalefactors and bits allocated in subbands. # Number of bits allocated in subband is either 0 or in range [2,15]. scfindices = np.zeros((params.nch, N_SUBBANDS), dtype='uint8') subband_bit_allocation = np.zeros((params.nch, N_SUBBANDS), dtype='uint8') smr = np.zeros((params.nch, N_SUBBANDS), dtype='float32') # Finding scale factors, psychoacoustic model and bit allocation calculation for subbands. Although # scaling is done later, its result is necessary for the psychoacoustic model and calculation of # sound pressure levels. for ch in range(params.nch): scfindices[ch, :] = get_scalefactors(subband_samples[ch, :, :], params.table.scalefactor) subband_bit_allocation[ch, :] = psycho.model1( input_buffer.audio[ch].ordered(), params, scfindices) """ ASSIGNMENT 4 : Quantization """ subband_samples_quantized = np.zeros(subband_samples.shape, dtype='uint32') for ch in range(params.nch): for sb in range(N_SUBBANDS): QCa = params.table.qca[subband_bit_allocation[ch, sb] - 2] QCb = params.table.qcb[subband_bit_allocation[ch, sb] - 2] scf = params.table.scalefactor[scfindices[ch, sb]] ba = subband_bit_allocation[ch, sb] for ind in range(FRAMES_PER_BLOCK): subband_samples_quantized[ch, sb, ind] = assignment4.quantization( subband_samples[ch, sb, ind], scf, ba, QCa, QCb) # Forming output bitsream and appending it to the output file. bitstream_formatting(outmp3file, params, subband_bit_allocation, scfindices, subband_samples_quantized)
def main(inwavfile, outmp3file, bitrate): """Encoder main function.""" #inwavfile = "../samples/sinestereo.wav" #outmp3file = "../samples/sinestereo.mp3" #bitrate = 320 # Read WAVE file and set MPEG encoder parameters. input_buffer = WavRead(inwavfile) params = EncoderParameters(input_buffer.fs, input_buffer.nch, bitrate) # Subband filter calculation from baseband prototype. # Very detailed analysis of MP3 subband filtering available at # http://cnx.org/content/m32148/latest/?collection=col11121/latest # Read baseband filter samples """ ASSIGNMENT 2 """ baseband_filter = assignment2.prototype_filter().astype('float32') subband_samples = np.zeros((params.nch, N_SUBBANDS, FRAMES_PER_BLOCK), dtype='float32') # Main loop, executing until all samples have been processed. while input_buffer.nprocessed_samples < input_buffer.nsamples: # In each block 12 frames are processed, which equals 12x32=384 new samples per block. for frm in range(FRAMES_PER_BLOCK): samples_read = input_buffer.read_samples(SHIFT_SIZE) # If all samples have been read, perform zero padding. if samples_read < SHIFT_SIZE: for ch in range(params.nch): input_buffer.audio[ch].insert(np.zeros(SHIFT_SIZE - samples_read)) # Filtering = dot product with reversed buffer. """ ASSIGNMENT 3 : Subband filtering """ for ch in range(params.nch): subband_samples[ch,:,frm] = assignment3.subband_filtering(input_buffer.audio[ch].reversed(), baseband_filter) # Declaring arrays for keeping table indices of calculated scalefactors and bits allocated in subbands. # Number of bits allocated in subband is either 0 or in range [2,15]. scfindices = np.zeros((params.nch, N_SUBBANDS), dtype='uint8') subband_bit_allocation = np.zeros((params.nch, N_SUBBANDS), dtype='uint8') smr = np.zeros((params.nch, N_SUBBANDS), dtype='float32') # Finding scale factors, psychoacoustic model and bit allocation calculation for subbands. Although # scaling is done later, its result is necessary for the psychoacoustic model and calculation of # sound pressure levels. for ch in range(params.nch): scfindices[ch,:] = get_scalefactors(subband_samples[ch,:,:], params.table.scalefactor) subband_bit_allocation[ch,:] = psycho.model1(input_buffer.audio[ch].ordered(), params,scfindices) """ ASSIGNMENT 4 : Quantization """ subband_samples_quantized = np.zeros(subband_samples.shape, dtype='uint32') for ch in range(params.nch): for sb in range(N_SUBBANDS): QCa = params.table.qca[subband_bit_allocation[ch,sb]-2] QCb = params.table.qcb[subband_bit_allocation[ch,sb]-2] scf = params.table.scalefactor[scfindices[ch,sb]] ba = subband_bit_allocation[ch,sb] for ind in range(FRAMES_PER_BLOCK): subband_samples_quantized[ch,sb,ind] = assignment4.quantization(subband_samples[ch,sb,ind], scf, ba, QCa, QCb) # Forming output bitsream and appending it to the output file. bitstream_formatting(outmp3file, params, subband_bit_allocation, scfindices, subband_samples_quantized)