audiostream
audiostream copied to clipboard
Issue recording audio after outputting audio - iOS
There appears to be an issue whereby mic.poll() does not call the callback function of 'get_input(callback = callbackFunction, rate = rate, buffersize = buffersize) after using the speaker's on my iPhone to output audio.
My code works fine recording audio with no previous activity, but if I use the audiostream's AudioOutput module or Kivy's SoundLoader module and try to record audio again using get_input, then mic.poll() fails and no buffer data is sent to the callback function of get_input. I imagine this issue is to do with how the phone is re-configured when it's speakers are used, as, if I do not output any audio, I can record audio using audiostream an infinite number of times.
Here is my code to record the audio:
def __init__(self, **kw):
super().__init__(**kw)
self.samples_per_second = 60 # variables which stores the number of audio samples recorded per second
self.audioData = [] # creates a list to store the audio bytes recorded
import sys
importlib.reload(sys.modules['audiostream']) # reloads the audiostream module - thought this might solve the problem; it doesn't!!
self.mic = get_input(callback=self.micCallback, rate=8000, source='default', buffersize=2048) # initialises the method get_input from the module 'audiostream' with the properties required to ensure the audio is recorded correctly
def micCallback(self, buffer):
# method which is called by the method 'get_input' to store recorded audio data (each buffer of audio samples)
self.audioData.append(buffer) # appends each buffer (chunk of audio data) to variable 'self.audioData'
def start(self):
# method which begins the process of recording the audio data
self.mic.start() # starts the method 'self.mic' recording audio data
Clock.schedule_interval(self.readChunk, 1 / self.samples_per_second) # calls the method 'self.readChunk' to read and store each audio buffer (2048 samples) 60 times per second
def readChunk(self, sampleRate):
# method which coordinates the reading and storing of the bytes from each buffer of audio data (which is a chunk of 2048 samples)
self.mic.poll() # calls 'get_input(callback=self.mic_callback, source='mic', buffersize=2048)' to read the byte content. This byte content is then dispatched to the callback method 'self.micCallback'
def stop(self):
# method which terminates and saves the audio recording when the recording has been successful
Clock.unschedule(self.readChunk) # un-schedules the Clock's rythmic execution of the 'self.readChunk' callback
self.mic.stop() # stops recording audio
return self.audioData
Here is the code I have used to output audio after using the above code to record audio:
messageFile_voice = SoundLoader.load("filename.wav")
messageFile_voice.play()
- iOS add this at the very top
import os os.environ['KIVY_AUDIO'] = 'avplayer'
👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.
Let us know if this comment was made in error, and we'll be happy to reopen the issue.