aiXander / Realtime_PyAudio_FFT

Realtime audio analysis in Python, using PyAudio and Numpy to extract and visualize FFT features from streaming audio.
MIT License
948 stars 186 forks source link

Add functionality to read wav file #20

Open kootenpv opened 2 years ago

kootenpv commented 2 years ago

Unfortunately doesn't work with:

wf = wave.open("my.wav", "rb")

self.stream = p.open(
    format=p.get_format_from_width(wf.getsampwidth()),
    channels=wf.getnchannels(),
    rate=wf.getframerate(),
    output=True,
    stream_callback=self.callback,
)
EdoardoMor commented 9 months ago

I think it should work like this, i will try it next weekend: (pseudo code):

import pyaudio import wave import numpy as np from Realtime_PyAudio_FFT import Visualization

Read audio file

audio_file = "path_to_audiofile.mp3" wf = wave.open(audio_file, 'rb')

Initialization of the PyAudio library and the Visualization object

p = pyaudio.pyAudio() visualization = visualization()

Set parameters for audio playback

stream = p.open( format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True, stream_callback=visualization.callback, # Callback callback function callback for animation )

Audio creation

data = wf.readframes(1024) whiledata: stream.write(data) data = wf.readframes(1024)

End the thread and clean up the resources

stream.stop_stream() stream.close() p.terminate()

Did you manage to write the full code and does it work?

nordost8 commented 9 months ago

I think it should work like this, i will try it next weekend: (pseudo code): import pyaudio import wave import numpy as np from Realtime_PyAudio_FFT import Visualization

Read audio file

audio_file = "path_to_audiofile.mp3" wf = wave.open(audio_file, 'rb')

Initialization of the PyAudio library and the Visualization object

p = pyaudio.pyAudio() visualization = visualization()

Set parameters for audio playback

stream = p.open( format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True, stream_callback=visualization.callback, # Callback callback function callback for animation )

Audio creation

data = wf.readframes(1024) whiledata: stream.write(data) data = wf.readframes(1024)

End the thread and clean up the resources

stream.stop_stream() stream.close() p.terminate()

Did you manage to write the full code and does it work?

do you want to specify the path to the file and see everything just like with the microphone? I could make a fork and add such functionality there, if someone really needs it :)