Python Forum
Error is wave file processing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error is wave file processing
#1
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]
Error:
Traceback (most recent call last): File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\trywav.py", line 45, in <module> plt.plot(time, wave_data[0]) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\pyplot.py", line 2813, in plot is not None else {}), **kwargs) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\__init__.py", line 1810, in inner return func(ax, *args, **kwargs) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_axes.py", line 1611, in plot for line in self._get_lines(*args, **kwargs): File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_base.py", line 393, in _grab_next_args yield from self._plot_args(this, kwargs) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_base.py", line 370, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_base.py", line 231, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (23493,) and (46986,)
PLease guide

Hello Buran,

I shall take care henceforth

Than you
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Logical error processing List of strings dmc8300 3 1,030 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  python one line file processing har 4 3,202 Dec-09-2019, 06:10 AM
Last Post: har
  pdf file processing: how to "Enable Editing" Pavel_47 4 3,130 Dec-04-2019, 10:00 AM
Last Post: Pavel_47
  Trouble processing file villumanati 1 1,611 Jul-30-2019, 04:17 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020