Skip to content

Commit b834a24

Browse files
committed
Improve ALSA recording latency
1 parent 7596259 commit b834a24

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Other:
1313

1414
* Improve ALSA playback latency
1515

16+
* Improve ALSA recording latency
17+
1618
2.1.0
1719
=====
1820

src/infra/audio/implementation/librtaudio/audio_recorder_rt_audio.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,30 @@ AudioRecorderRtAudio::~AudioRecorderRtAudio()
9393

9494
uint32_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

Comments
 (0)