Reputation: 53
With PyAudio it is fairly simple to open an input stream comming from a physical sound input device.
I wonder if there is a way of open an existing sound file as a stream, which has the same properties as the sound-device-stream?
Instead of
self.p = pyaudio.PyAudio()
self.inStream = self.p.open(format=pyaudio.paInt16,
channels=1,
rate=self.RATE,
input=True,
frames_per_buffer=self.BUFFERSIZE)
I'd like to do
self.p = pyaudio.PyAudio()
self.inStream = self.p.open('testfile.wav',
input=True,
frames_per_buffer=self.BUFFERSIZE)
Regards, Torsten
Upvotes: 0
Views: 1914
Reputation: 118
As far as I know you can't use PyAudio to open file directly, however it's possible to combine pyaudio and wave modules in case your app reroutes the processed sound to SoundFlower for example.
import pyaudio, wave
def open_stream(self):
self.p_audio = pyaudio.PyAudio()
self.file_source = wave.open('input.wav', 'rb')
s_width = self.file_source.getsampwidth()
format = self.p_audio.get_format_from_width(s_width)
channels = self.file_source.getnchannels()
rate = self.file_source.getframerate()
self.stream = self.p_audio.open(format=format,
channels=channels,
rate=rate,
frames_per_buffer=1024,
input=False,
output=True,
output_device_index=detect_build_in_output_device_idx(self.p_audio),
stream_callback=self.__recording_callback)
def __recording_callback(self, in_data, frame_count, time_info, status):
# read frames
in_data = self.file_source.readframes(frame_count)
# decode frames
left, right = audio_decode(in_data, self.file_source.getnchannels())
# process frames
left, right = self.core.process(left, right)
# encode back
processed_data = audio_encode((left, right))
return processed_data, pyaudio.paContinue
Upvotes: 0