Error is wave file processing - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error is wave file processing (/thread-15460.html) |
Error is wave file processing - vipinv23 - Jan-18-2019 Hello, I am executing the following program for SE32_LE wave file format: import wave import matplotlib.pyplot as plt import numpy as np f = wave.open("M1F132.wav", "rb") #rb = read only # (nchannels, sampwidth, framerate, nframes, comptype, compname) params = f.getparams() nchannels, sampwidth, framerate, nframes = params[:4] print("Channels =", nchannels) print("Sample width =", sampwidth) print("Frame rate =", framerate) print("Frames =", nframes) str_data = f.readframes(nframes) #Reads and returns at most n frames of audio, as a bytes object. f.close() #length = f.getnframes() #for i in range(0,length): # waveData = f.readframes(1) # data = struct.unpack("<h", waveData) # print(int(data[0])) wave_data = np.fromstring(str_data, dtype=np.short) #A new 1-D array initialized from text data in a string # str_data is string containing the data #dtype= The data type of the array; default: float print ("Audio data =", wave_data) wave_data.shape = -1, 2 #formats array in rows and columns #-1. In this case, the value is taken from the length of the array and remaining dimensions. wave_data = wave_data.T #.T is self transpose the array i.e. rows to columns time = np.arange(0, nframes) * (1.0 / framerate) #for creating time axis for plot, arrange for no. of frames, (1.0 / framerate) will create in seconds #arange = Returns evenly spaced values within a given interval. figure = plt.gcf() # get current figure figure.set_size_inches(8*20, 6) duration = nframes/float(framerate) xticks = np.arange(0, duration, 2) plt.subplot(211).set_xticks(xticks) plt.plot(time, wave_data[0]) plt.title('Audio channel 1', loc='left') plt.subplot(212).set_xticks(xticks) plt.plot(time, wave_data[1], c="g") plt.xlabel("time (seconds)") plt.title('Audio channel 2', loc='left') plt.savefig('Audio.png', dpi=100, bbox_inches='tight', pad_inches=0.1) plt.show() #plt.close(figure)but i am getting the following error: Channels = 2 Sample width = 4 Frame rate = 8000 Frames = 23493 Audio data = [ 0 0 0 ... 0 0 -6] PLease guideHello Buran, I shall take care henceforth Than you RE: Error is wave file processing - Larz60+ - Jan-18-2019 Line 45 of your code: plt.plot(time, wave_data[0])ValueError: x and y must have same first dimension, but have shapes (23493,) and (46986,) It's in the error traceback, there are among the best constructed error tracebacks in the programming world. start with the last posted line number from your module, this is where the error initiated. Code listed after your scripts last line shows what's executed after leaving your module, and code Code listed before your scripts last line shows what's executed prior to the error. |