@@ -93,14 +93,30 @@ AudioRecorderRtAudio::~AudioRecorderRtAudio()
9393
9494uint32_t AudioRecorderRtAudio::initializeSoundStream (uint32_t deviceId, uint32_t channelCount, uint32_t sampleRate, uint32_t bufferSize)
9595{
96- RtAudio::StreamParameters iParams;
97- iParams.deviceId = deviceId;
98- iParams.nChannels = channelCount;
99- uint32_t bufferFrames = bufferSize;
100- m_rtAudio.openStream (nullptr , &iParams, RTAUDIO_SINT32,
101- sampleRate, &bufferFrames,
102- &AudioRecorderRtAudio::recordCallback, this );
103- m_rtAudio.startStream ();
96+ RtAudio::StreamParameters streamParameters;
97+ streamParameters.deviceId = deviceId;
98+ streamParameters.nChannels = channelCount;
99+ streamParameters.firstChannel = 0 ;
100+
101+ RtAudio::StreamOptions streamOptions;
102+ // Minimize latency for the input stream and request realtime scheduling
103+ streamOptions.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_SCHEDULE_REALTIME;
104+ // Set to 2 buffers to minimize the time between hardware capture and your callback
105+ streamOptions.numberOfBuffers = 2 ;
106+ streamOptions.streamName = " NoteaheadRecorder" ;
107+
108+ try {
109+ uint32_t bufferFrames = bufferSize;
110+ m_rtAudio.openStream (nullptr , &streamParameters, RTAUDIO_SINT32,
111+ sampleRate, &bufferFrames,
112+ &AudioRecorderRtAudio::recordCallback, this , &streamOptions);
113+ m_rtAudio.startStream ();
114+ } catch (RtAudioError & e) {
115+ // In recording, if 'hw:' is busy, this will catch the 'Device Busy' error
116+ e.printMessage ();
117+ return 0 ;
118+ }
119+
104120 return m_rtAudio.getStreamSampleRate ();
105121}
106122
0 commit comments