In my code I'm reading a list that stores 44100 samples of a one second wave audio file like this:
with open("./test.csv", "r") as csv_file:
reader = csv.reader(csv_file, quoting=csv.QUOTE_NONE)
rows = list(reader)
audiofile = rows[12]
If I print the file:
print(audiofile)
I get this result:
' 0.06585693', ' 0.06663513', ' 0.066467285', ' 0.06716919', ' 0.06765747', ' 0.06770325', ' 0.06803894', ' 0.068481445', ' 0.06854248', ' 0.06918335', ' 0.06915283', ' 0.0693512', ' 0.07003784', ' 0.07008362', ' 0.06993103', ' 0.070632935', ' 0.07072449', ' 0.07104492', ' 0.07159424', ' 0.071395874', ...
and so on. Meaning the list 'audiofile' contains all the 44100 floating point samples of the wave file just like desired.
Now before I can display the signal with librosa waveshow I need to put the data inside a numpy.ndarray.
I found this solution Convert list elements into array here on stack overflow that suggests the use of asarray() but that did not work out somehow ...
I tried it like this:
with open("./test.csv", "r") as csv_file:
reader = csv.reader(csv_file, quoting=csv.QUOTE_NONE)
rows = list(reader)
audiofile = rows[12]
temp_list = audiofile
myarray = np.asarray(temp_list)
When I print the data type like this
print(type(myarray))
I get:
<class 'numpy.ndarray'>
So that seems to be right. When I print the array like this:
print(myarray)
I get:
[' 0.06585693' ' 0.06663513' ' 0.066467285' ... ' 0.010253906' ' 0.011354681' ' 0.011835733']
So np.asarray() seemed to be the right fit. However if I now run:
librosa.display.waveshow(myarray)
I somehow get this error:
Traceback (most recent call last): File "/Users/abc/Desktop/Project Python Audio/Datenbankzugriff.py", line 76, in librosa.display.waveshow(myarray) File "/Users/abc/Desktop/Project Python Audio/.venv/lib/python3.11/site-packages/librosa/display.py", line 2029, in waveshow util.valid_audio(y, mono=False) File "/Users/abc/Desktop/Project Python Audio/.venv/lib/python3.11/site-packages/librosa/util/utils.py", line 300, in valid_audio raise ParameterError("Audio data must be floating-point") librosa.util.exceptions.ParameterError: Audio data must be floating-point
This surprises me, as the data is floating-point ... How can I preprocess my wave samples correctly to pass them to librosa.display.waveshow() afterwards?
So it turned out that because of a mistake in my code I was filling the list with multiple arrays that had the symbols like this
] " [
inside of them, thus provoking that the list got mistaken as a character list instead of floating point. Fixed this issue like suggest in this thread:
ValueError: could not convert string to float: id
Got the librosa.display.waveshow() function working with this code:
with open("./test.csv", "r") as csv_file:
reader = csv.reader(csv_file, quoting=csv.QUOTE_NONE)
rows = list(reader)
audiofile = rows[12]
temp_list = audiofile
temp_list = [(str(i).replace('"', '')) for i in temp_list]
temp_list = [(str(i).replace("]", "")) for i in temp_list]
temp_list = [float(str(i).replace("[", "")) for i in temp_list]
myarray = np.asarray(temp_list, dtype=np.float32)
librosa.display.waveshow(myarray)
plt.show()