Skip to content

Commit 1f56d51

Browse files
committed
PortAudio fixed buffer size
1 parent 924a85f commit 1f56d51

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/AudioTools/PortAudioStream.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class PortAudioConfig : public AudioBaseInfo {
2727
*/
2828
class PortAudioStream : public BufferedStream {
2929
public:
30-
PortAudioStream():BufferedStream(DEFAULT_BUFFER_SIZE) {
30+
PortAudioStream(int buffer_size=DEFAULT_BUFFER_SIZE):BufferedStream(buffer_size) {
3131
LOGD(__FUNCTION__);
32+
this->buffer_size = buffer_size;
3233
}
3334

3435
~PortAudioStream(){
@@ -57,20 +58,20 @@ class PortAudioStream : public BufferedStream {
5758
return;
5859
}
5960

60-
/* Open an audio I/O stream. */
61+
// calculate frames
62+
int bytes = info.bits_per_sample / 8;
63+
int buffer_frames = buffer_size / bytes / info.channels;
64+
65+
// Open an audio I/O stream.
6166
LOGD("Pa_OpenDefaultStream");
6267
err = Pa_OpenDefaultStream( &stream,
63-
info.is_input ? info.channels : 0, /* no input channels */
64-
info.is_output ? info.channels : 0, /* stereo output */
65-
getFormat(info.bits_per_sample),
66-
info.sample_rate,
67-
paFramesPerBufferUnspecified, /* frames per buffer, i.e. the number
68-
of sample frames that PortAudio will
69-
request from the callback. Many apps
70-
may want to use*/
71-
nullptr, /* this is your callback function */
72-
nullptr ); /*This is a pointer that will be passed to
73-
your callback*/
68+
info.is_input ? info.channels : 0, // no input channels
69+
info.is_output ? info.channels : 0, // stereo output
70+
getFormat(info.bits_per_sample), // format
71+
info.sample_rate, // sample rate
72+
buffer_frames, // frames per buffer
73+
nullptr,
74+
nullptr );
7475
LOGD("Pa_OpenDefaultStream - done");
7576
if( err != paNoError ) {
7677
LOGE( "PortAudio error: %s\n", Pa_GetErrorText( err ) );
@@ -101,6 +102,7 @@ class PortAudioStream : public BufferedStream {
101102
PaError err = paNoError;
102103
PortAudioConfig info;
103104
bool stream_started = false;
105+
int buffer_size;
104106

105107
virtual size_t writeExt(const uint8_t* data, size_t len) {
106108
LOGD("writeExt: %zu", len);

0 commit comments

Comments
 (0)