@@ -26,43 +26,41 @@ namespace noteahead {
2626static const auto TAG = " AudioPlayerRtAudio" ;
2727
2828int AudioPlayerRtAudio::playCallback (void * outputBuffer, void *,
29- uint32_t nFrames ,
29+ uint32_t frameCount ,
3030 double , RtAudioStreamStatus status,
3131 void * userData)
3232{
3333 const auto self = static_cast <AudioPlayerRtAudio *>(userData);
3434
3535 if (status) {
36- std::cerr << " Stream under/overflow detected!" << std::endl ;
36+ juzzlin::L (TAG). error () << " Stream under/overflow detected!" ;
3737 }
3838
3939 if (!outputBuffer) {
4040 return 0 ;
4141 }
4242
4343 const auto out = static_cast <int32_t *>(outputBuffer);
44- const int channels = self->m_streamer .channels () > 0 ? self->m_streamer .channels () : 2 ; // Default to 2 if no file
45- const size_t totalSamples = nFrames * channels;
44+ const size_t channels = self->m_streamer .channels () > 0 ? static_cast < size_t >( self->m_streamer .channels () ) : 2 ; // Default to 2 if no file
45+ const size_t totalSamples = frameCount * channels;
4646
47- const size_t read = self->m_streamer .pop (out, totalSamples);
48- if (read < totalSamples) {
47+ if (const size_t read = self->m_streamer .pop (out, totalSamples); read < totalSamples) {
4948 // Zero out the rest of the buffer
5049 std::fill_n (out + read, totalSamples - read, 0 );
5150 }
5251
5352 if (self->m_audioEngine ) {
54- std::vector<float > interleaved (nFrames * 2 , 0 .0f );
55- self->m_audioEngine ->process (interleaved.data (), nFrames, self->m_rtAudio .getStreamSampleRate ());
56-
57- for (uint32_t i = 0 ; i < nFrames; ++i) {
53+ std::vector<float > interleaved (frameCount * 2 , 0 .0f );
54+ self->m_audioEngine ->process (interleaved.data (), frameCount, self->m_rtAudio .getStreamSampleRate ());
55+ for (uint32_t frame = 0 ; frame < frameCount; frame++) {
5856 // Mix with existing buffer (converted to float)
5957 // assuming int32_t full scale
60- for (int ch = 0 ; ch < channels; ++ch ) {
61- const int outIdx = i * channels + ch ;
62- float currentVal = static_cast < float >(out[outIdx]) / 2147483647 .0f ;
58+ for (size_t channel = 0 ; channel < channels; channel++ ) {
59+ const auto outIndex = frame * channels + channel ;
60+ const auto maxVal = 2'147'483'647 .0f ;
6361 // Mix in engine data (interleaved is always 2 channels)
64- currentVal += interleaved[i * 2 + (ch % 2 )];
65- out[outIdx ] = static_cast <int32_t >(std::clamp (currentVal, -1 .0f , 1 .0f ) * 2147483647 . 0f );
62+ const auto currentVal = static_cast < float >(out[outIndex]) / maxVal + interleaved[frame * 2 + (channel % 2 )];
63+ out[outIndex ] = static_cast <int32_t >(std::clamp (currentVal, -1 .0f , 1 .0f ) * maxVal );
6664 }
6765 }
6866 }
@@ -88,10 +86,10 @@ std::vector<AudioDevice> AudioPlayerRtAudio::getOutputDevices()
8886{
8987 std::vector<AudioDevice> devices;
9088 const unsigned int deviceCount = m_rtAudio.getDeviceCount ();
91- for (unsigned int i = 0 ; i < deviceCount; ++i ) {
92- const auto info = m_rtAudio.getDeviceInfo (i );
89+ for (unsigned int deviceIndex = 0 ; deviceIndex < deviceCount; deviceIndex++ ) {
90+ const auto info = m_rtAudio.getDeviceInfo (deviceIndex );
9391 if (info.outputChannels > 0 ) {
94- devices.push_back ({ i , info.name });
92+ devices.push_back ({ deviceIndex , info.name });
9593 }
9694 }
9795 return devices;
@@ -146,8 +144,8 @@ void AudioPlayerRtAudio::start(const std::string & fileName, uint32_t bufferSize
146144
147145 if (!fileName.empty ()) {
148146 m_streamer.start (fileName, 0 ); // Temporary size, will resize below
149- sampleRate = m_streamer.sampleRate ();
150- channelCount = m_streamer.channels ();
147+ sampleRate = static_cast < uint32_t >( m_streamer.sampleRate () );
148+ channelCount = static_cast < uint32_t >( m_streamer.channels () );
151149 }
152150
153151 uint32_t deviceId = 0 ;
0 commit comments