Real time audio processing with Portaudio C++ - c++

I'm trying to make a C++ application to transmit audio via a VoIP protocol between 2 clients (using UDP).
I'm working with Portaudio C library and I have issues to encapsulate this lib. In order to send the recorded audio to another client, I'd like to get sound samples as it is recorded (real time).
For the moment I can only record sound, and then, play what I recorded.
I'm not comfortable at all with this library and any help would be appreciated.
Here's what I've done so far.
Audio.cpp -> Callback methods:
static int PaRecordCallback(const void *input, void *output, unsigned long frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
Audio *audio = reinterpret_cast<Audio *>(userData);
return audio->RecordCallback(input, output, frameCount, timeInfo, statusFlags);
}
static int PaPlayCallback(const void *input, void *output, unsigned long frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
Audio *audio = reinterpret_cast<Audio *>(userData);
return audio->PlayCallback(input, output, frameCount, timeInfo, statusFlags);
}
int Audio::RecordCallback(const void *input, void *output, unsigned long &frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags &statusFlags)
{
std::cout << "Frame index:\t\t" << _recordedFrameIndex << std::endl << "Max frame index:\t" << _maxFrameIndex << std::endl << "--------------" << std::endl;
const SAMPLE *rptr = static_cast<const SAMPLE *>(input);
SAMPLE *wptr = &_recordedSamples[_recordedFrameIndex * NUM_CHANNELS];
unsigned long framesLeft = _maxFrameIndex - _recordedFrameIndex;
unsigned long framesToCalc;
int finished;
if (framesLeft < frameCount) {
framesToCalc = framesLeft;
finished = paComplete;
} else {
framesToCalc = frameCount;
finished = paContinue;
}
for (unsigned long i = 0; i < framesToCalc; i++) {
*wptr++ = *rptr++;
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++;
}
_recordedFrameIndex += framesToCalc;
return finished;
}
int Audio::PlayCallback(const void *input, void *output, unsigned long &frameCount, \
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags &statusFlags)
{
SAMPLE *rptr = &_recordedSamples[_playedFrameIndex * NUM_CHANNELS];
SAMPLE *wptr = static_cast<SAMPLE *>(output);
unsigned long framesLeft = _maxFrameIndex - _playedFrameIndex;
unsigned int i;
int finished;
if (framesLeft < frameCount) {
for (i = 0; i < framesLeft; i++) {
*wptr++ = *rptr++;
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++;
}
for (; i < frameCount; i++) {
*wptr++ = 0;
if (NUM_CHANNELS == 2)
*wptr++ = 0;
}
_playedFrameIndex += framesLeft;
finished = paComplete;
} else {
for (i = 0; i < frameCount; i++) {
*wptr++ = *rptr++;
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++;
}
_playedFrameIndex += frameCount;
finished = paContinue;
}
return finished;
}
Audio.cpp -> Record and Play methods:
void Audio::Record()
{
if (!_recordStream) {
OpenRecordStream();
_recordedFrameIndex = 0;
_err = Pa_StartStream(_recordStream);
if (_err != paNoError)
AudioError("Audio::Record -> Pa_StartStream()");
std::cout << "Audio record stream started." << std::endl;
std::cout << "Recording ..." << std::endl;
_recording = true;
fflush(stdout);
} else if (_recording)
Pa_Sleep(1000);
}
void Audio::Play()
{
if (!_playStream) {
OpenPlayStream();
_playedFrameIndex = 0;
_err = Pa_StartStream(_playStream);
if (_err != paNoError)
AudioError("Audio::Play -> Pa_StartStream()");
std::cout << "Audio play stream started." << std::endl;
std::cout << "Playing ..." << std::endl;
_playing = true;
fflush(stdout);
} else if (_playing)
Pa_Sleep(500);
}
Audio.hpp -> Class audio:
#include <portaudio.h>
typedef int16_t SAMPLE;
#define PA_SAMPLE_TYPE paInt16
#define PRINTF_S_FORMAT "%.8f"
#define SAMPLE_RATE 44100
#define SAMPLE_SILENCE 0.0f
#define FRAMES_PER_BUFFER 1
#define NUM_SECONDS 5
#define NUM_CHANNELS 2
#define DITHER_FLAG 0
#define WRITE_TO_FILE 0
#define SAMPLE_SIZE NUM_SECONDS * SAMPLE_RATE * NUM_CHANNELS
class Audio
{
public:
Audio();
~Audio();
void Record();
void Play();
void OpenRecordStream();
void OpenPlayStream();
void CloseRecordStream();
void ClosePlayStream();
const bool &isRecording() const;
const bool &isPlaying() const;
const PaStream *GetRecordStream() const;
const PaStream *GetPlayStream() const;
void GetSamples(SAMPLE *);
void SetSamples(SAMPLE *);
int RecordCallback(const void *, void *, unsigned long &, \
const PaStreamCallbackTimeInfo *, PaStreamCallbackFlags &);
int PlayCallback(const void *, void *, unsigned long &, \
const PaStreamCallbackTimeInfo *, PaStreamCallbackFlags &);
bool _recording;
bool _playing;
protected:
private:
// Functions:
void AudioError(const std::string &);
// Variables:
PaError _err;
PaStream *_playStream;
PaStream *_recordStream;
SAMPLE *_samplesToPlay;
SAMPLE *_recordedSamples;
unsigned long _recordedFrameIndex;
unsigned long _playedFrameIndex;
unsigned long _maxFrameIndex;
PaStreamParameters _inputParameters;
PaStreamParameters _outputParameters;
};
I apologize if it's very long, but I want you to have all the necessary informations to understand my problem.
I don't ask questions often, so I really need some help here.
Thank you.

You need to use a full duplex callback to record and play in real time so you catch the input buffer in chunks for your recordings (when you want to record) and send the recordings (or even the incoming sound) to the output buffer, also in chunks.
The chunksize normally would be from 64 to 4096 frames, and each frame normally would contain 2 samples (channel L and channel R)
The full duplex callback is a kind of circular buffer that brings you x frames when the input buffer is filled by the ADC and where you fill the output buffer with x frames for being ready for when the DAC asks for it.

Related

Why does copying to portaudio input buffer with memcpy is creating noise?

I'm just trying to copy the input buffer of portaudio in a callback function. Thats my callback:
static int recordCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
memcpy(&circularBuffer[data->write], inputBuffer, 1024); // Assuming samplesPerFrame = FRAME_SIZE
data->write = data->write + 1024;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
if( framesLeft < framesPerBuffer )
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
data->frameIndex += framesToCalc;
return finished;
}
And this is the original callback from portaudio examples:
static int recordCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
if( framesLeft < framesPerBuffer )
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if( inputBuffer == NULL )
{
for( i=0; i<framesToCalc; i++ )
{
*wptr++ = SAMPLE_SILENCE; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
for( i=0; i<framesToCalc; i++ )
{
*wptr++ = *rptr++; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
}
}
data->frameIndex += framesToCalc;
return finished;
}
And I'm just using memcpy instead of to iterate pointer. But the sound I record has so much noise. I can't figure out the problem. Maybe, I'm missing the channels(2 channels) but I'm not sure. Do you have any idea?
You can access the full code from here
The approach is true but I couldn`t specify the frame size correctly
Instead of this:
memcpy(&circularBuffer[data->write], inputBuffer, 1024); // 1024 = FRAME_SIZE
This will be better:
memcpy(&circularBuffer[data->write], inputBuffer, 1024*sizeof(float)); // Assuming 1024 = FRAME_SIZE
because of:
typedef float SAMPLE; is 4 bytes.

portaudio only taking one sample?

I am currently working with portaudio with an application that records, and I seem to have some issues collecting the samples. From what I can see is only one sample being stored, and the callback is only being called once, and that is it, even though the variable NUM_OF_SECONDS is set to 30 seconds.
I currently running out of ideas of what I can test, and how I can debug this, so I've come here, any suggestions to how I can debug my problem?
Here is the code:
main.cpp:
#include <record.h>
int main()
{
record somethis;
somethis.start_record();
return 0;
}
record.h
#pragma once
#include <iostream> // Functionality: COUT
#include "portaudio.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono> //Functionality: Sleep
#include <thread> //Functionality: Sleep
#include <algorithm> //Functionality: fill_n
#define SAMPLE_RATE (44100)
typedef float SAMPLE;
#define NUM_SECONDS 30
#define NUM_CHANNELS 2
#define SAMPLE_SILENCE 0.0f
#define PA_SAMPLE_TYPE paFloat32
#define FRAMES_PER_BUFFER (512)
#define TRUE (1==1)
#define FALSE (!TRUE)
#define WRITE_TO_FILE TRUE
typedef struct
{
int frameIndex;
int maxFrameindex;
SAMPLE *recordedSamples;
}
paTestData;
class record {
public:
record();
void start_record();
private:
PaStreamParameters inputParameters,
outputParameters;
PaStream* stream;
PaError err = paNoError;
paTestData data;
int totalFrames;
int numSamples;
int numBytes;
SAMPLE max, val;
double average;
int recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData);
static int recordCallbackSub(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData)
{
auto pThis = reinterpret_cast<record*>(userData); // get back the this pointer.
return pThis->recordCallback( inputBuffer, outputBuffer,framesPerBuffer, timeInfo,statusFlags, nullptr);
}
};
record.cpp
#include "record.h"
record::record()
{
std::cout << "Record object made" << std::endl;
std::cout << "Portaudio Version: " << Pa_GetVersion() << std::endl;
this->data.maxFrameindex = this->totalFrames = NUM_SECONDS * SAMPLE_RATE;
this->data.frameIndex = 0;
this->numSamples = this->totalFrames * NUM_CHANNELS;
numBytes = numSamples * sizeof(SAMPLE);
this->data.recordedSamples = new SAMPLE[numSamples]; /* From now on, recordedSamples is initialised. */
if( this->data.recordedSamples == NULL )
{
std::cout << "Could not allocate record array" << std::endl;
exit(1);
}
for(int i=0; i<numSamples; i++ )
{
this->data.recordedSamples[i] = 0;
}
int err = Pa_Initialize();
if( err == paNoError )
{
std::cout << "No error in init" << std::endl;
std::cout << "PortAudio init: "<< Pa_GetErrorText( err ) << std::endl;
}
else
{
printf( "PortAudio error: %s\n", Pa_GetErrorText( err ) );
exit(1);
}
this->inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
if (this->inputParameters.device == paNoDevice) {
std::cout << "Error: No default input device" << std::endl;
exit(1);
}
this->inputParameters.channelCount = 1; /* stereo input */
this->inputParameters.sampleFormat = PA_SAMPLE_TYPE;
this->inputParameters.suggestedLatency = Pa_GetDeviceInfo( this->inputParameters.device )->defaultLowInputLatency;
this->inputParameters.hostApiSpecificStreamInfo = NULL;
std::cout << "Device name: " <<Pa_GetDeviceInfo(this->inputParameters.device)->name << std::endl;
std::cout << "Max inputChannels: " <<Pa_GetDeviceInfo(this->inputParameters.device)->maxInputChannels << std::endl;
}
int record::recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData)
{
std::cout << "Callback called" << std::endl;
this->data = (paTestData&) userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &this->data.recordedSamples[this->data.frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = this->data.maxFrameindex - this->data.frameIndex;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
//(void) userData;
if( framesLeft < framesPerBuffer )
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if( inputBuffer == NULL )
{
for(int i=0; i<framesToCalc; i++ )
{
*wptr++ = SAMPLE_SILENCE; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
for(int i=0; i<framesToCalc; i++ )
{
*wptr++ = *rptr++; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
}
}
this->data.frameIndex += framesToCalc;
return finished;
}
void record::start_record()
{
err = Pa_OpenStream(
&this->stream,
&this->inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&record::recordCallbackSub,
this );
if( err != paNoError )
{
std::cout << "Something wrong - open_stream check" << std::endl;
std::cout << "PortAudio error: "<< Pa_GetErrorText( err ) << std::endl;
exit(1);
}
this->err = Pa_StartStream( this->stream );
if( err != paNoError )
{
std::cout << "Something wrong in stream check" << std::endl;
std::cout << "PortAudio error: "<< Pa_GetErrorText( err ) << std::endl;
exit(1);
}
std::cout << "Waiting for playback to finish" << std::endl;
while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
{
Pa_Sleep(1000);
printf("index = %d\n", this->data.frameIndex ); fflush(stdout);
}
if( err < 0 )
{
std::cout << "error check with isStreamActive - something wrong" << std::endl;
std::cout << "PortAudio error: "<< Pa_GetErrorText( err ) << std::endl;
exit(1);
}
err = Pa_CloseStream( stream );
if( err != paNoError )
{
std::cout << "error check with close_stream- something wrong" << std::endl;
std::cout << "PortAudio error: "<< Pa_GetErrorText( err ) << std::endl;
exit(1);
}
std::cout << "Number of entries: " << sizeof(this->data.recordedSamples)/sizeof(this->data.recordedSamples[0]) << std::endl;
/* Measure maximum peak amplitude. */
max = 0;
average = 0.0;
for(int i=0; i<numSamples; i++ )
{
val = this->data.recordedSamples[i];
std::cout << "i: " << i << " : "<< val << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if( val < 0 ) val = -val; /* ABS */
if( val > max )
{
max = val;
}
average += val;
}
average = average / (double)numSamples;
std::cout<<"sample max amplitude = " << max << std::endl;
std::cout<<"sample average = " << average << std::endl;
if (WRITE_TO_FILE)
{
FILE *fid;
fid = fopen("recorded.wav", "wb");
if( fid == NULL )
{
printf("Could not open file.");
}
else
{
fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid );
fclose( fid );
printf("Wrote data to 'recorded.raw'\n");
}
}
std::cout << "Everythin done!" << std::endl;
}
Update:
I've from some debugging notices that the callback is only being called once, and after it returns, becomes the stream inactive, hence making the it impossible to make calls to the callback function. Why does the stream become inactive?
Your first callback
static int recordCallbackSub(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData)
{
auto pThis = reinterpret_cast<record*>(userData); // get back the this pointer.
return pThis->recordCallback( inputBuffer, outputBuffer,framesPerBuffer, timeInfo,statusFlags, nullptr);
}
calls your second callback
int record::recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData)
{
std::cout << "Callback called" << std::endl;
this->data = (paTestData&) userData;
....
}
with the userData parameter set to nullptr. You then cast the nullptr into a paTestData& and set your data member variable to the result, which I suspect you didn't intend to do.
Delete the this->data = (paTestData&) userData; line.

PortAudio: Device unavailable

What do I do wrong?
I have been trying to run this code but every time I run it, it says:
Error: Device unavailable
My Linux: Ubuntu 17.04.
Any help?
#include <portaudio.h>
#include <iostream>
#define SAMPLE_RATE (48000)
static float *data;
PaStream *stream;
int callback(const void *pVoid, void *pVoid1, unsigned long i, const PaStreamCallbackTimeInfo *pInfo,
PaStreamCallbackFlags i1, void *pVoid2);
int main() {
PaError err;
/* Open an audio I/O stream. */
err = Pa_OpenDefaultStream(&stream,
0, /* no input channels */
2, /* stereo output */
paFloat32, /* 32 bit floating point output */
SAMPLE_RATE,
paFramesPerBufferUnspecified, /* frames per buffer */
callback, /* this is your callback function */
&data); /*This is a pointer that will be passed to
your callback*/
if (err != paNoError) {
std::cout << "Error: " << Pa_GetErrorText(err) << std::endl;
auto a = Pa_GetLastHostErrorInfo();
if (a->errorCode != 0) {
std::cout << "Host error: " << a->errorText << std::endl;
}
return 1;
}
return 0;
}
int callback(const void *pVoid, void *pVoid1, unsigned long i, const PaStreamCallbackTimeInfo *pInfo,
PaStreamCallbackFlags i1, void *pVoid2) {
// Do something with the stream...
return 0;
}
I do not run any programs that use audio in any way.
You should properly init before openening a stream, and start by debugging from there..
Here is an example you can work with.

Generating specific tones from user input with portaudio

I am struggling with outputting user selected audio tones with portaudio. I have based a function on the paex_sine.cpp and modified it somewhat based on this post multi-audio-tones-to-sound-card-using-portaudio
I am able to generate a sine wave on the fly by placing the sin(n * FREQ * 2 * PI / SAMPLE_RATE) calculation in paCallBackMethod() but the frequency isn't correct, and doesn't change based on user input.
Here is my function code.
void CDeepWaveDlg::generateSound(float freq)
{
pitch = tone;
offset = freq;
class Sine
{
public:
Sine() : stream(0), left_phase(0), right_phase(0)
{
}
bool open(PaDeviceIndex index)
{
PaStreamParameters outputParameters;
outputParameters.device = index;
if (outputParameters.device == paNoDevice) {
return false;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaError err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&Sine::paCallback,
this /* Using 'this' for userData so we can cast to Sine* in paCallback method */
);
if (err != paNoError)
{
/* Failed to open stream to device !!! */
return false;
}
err = Pa_SetStreamFinishedCallback(stream, &Sine::paStreamFinished);
if (err != paNoError)
{
Pa_CloseStream(stream);
stream = 0;
return false;
}
return true;
}
bool close()
{
if (stream == 0)
return false;
PaError err = Pa_CloseStream(stream);
stream = 0;
return (err == paNoError);
}
bool start()
{
if (stream == 0)
return false;
PaError err = Pa_StartStream(stream);
return (err == paNoError);
}
bool stop()
{
if (stream == 0)
return false;
PaError err = Pa_StopStream(stream);
return (err == paNoError);
}
private:
/* The instance callback, where we have access to every method/variable in object of class Sine */
int paCallbackMethod(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags)
{
float *out = (float*)outputBuffer;
unsigned long i;
(void)timeInfo; /* Prevent unused variable warnings. */
(void)statusFlags;
(void)inputBuffer;
for (i = 0; i<framesPerBuffer; i++)
{
float v = sin(i * pitch * 2.0 * M_PI /(float)SAMPLE_RATE);
float v2 = sin(i * (pitch + (float)offset) * 2.0 * M_PI /(float)SAMPLE_RATE);
*out++ = v;
*out++ = v2;
}
return paContinue;
}
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int paCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
/* Here we cast userData to Sine* type so we can call the instance method paCallbackMethod, we can do that since
we called Pa_OpenStream with 'this' for userData */
return ((Sine*)userData)->paCallbackMethod(inputBuffer, outputBuffer,
framesPerBuffer,
timeInfo,
statusFlags);
}
void paStreamFinishedMethod()
{
printf("Stream Completed: %s\n", message);
}
/*
* This routine is called by portaudio when playback is done.
*/
static void paStreamFinished(void* userData)
{
return ((Sine*)userData)->paStreamFinishedMethod();
}
PaStream *stream;
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
char message[20];
};
PaError err;
Sine sine;
err = Pa_Initialize();
if (err != paNoError) goto error;
if (sine.open(Pa_GetDefaultOutputDevice()))
{
if (sine.start())
{
Pa_Sleep(NUM_SECONDS * 1000);
sine.stop();
}
sine.close();
}
Pa_Terminate();
The only way I am able to get close to the desired pitch is to increase frames per buffer, which is currently 64, but I still run into the problem of not changing frequency based on user selection.
This problem is definitely beyond my skill level, but I am hoping someone can help me understand what's going on here.Thanks in advance for any help.

Using Opus with PortAudio

I'm having trouble for using opus with Port audio.
I need to read data audio from a stream using PortAudio, encoding data, decoding data and writing data. If I just read and write, everything works well. But when encoding and decoding, all I can hear is snow with my voice in background.
Here a part of my code:
I/O stream header:
#define NUM_CHANNELS (2)
#define PA_SAMPLE_TYPE paInt24
#define SAMPLE_RATE (48000)
#define FRAMES_PER_BUFFER (1024)
#define SAMPLE_SIZE (3)
#define FRAME_SIZE (960)
class SoundSystem
{
private:
PaStream *_stream;
int _readBufferSize;
PaStreamParameters _inputParam;
PaStreamParameters _outputParam;
unsigned char *_readBuffer;
public:
SoundSystem();
~SoundSystem();
// Init Stream
bool initPa();
bool openStream();
bool startStream();
bool initStream();
// Init params stream
bool initParams() const;
bool initInputParams();
bool initOutputParams();
bool initParams();
// I/O
bool writeOnStream(unsigned cha\
r *buff);
bool readFromStream();
// Utils
void cleanReadBuffer();
int getReadBufferSize() const;
unsigned char *getReadBuffer() const;
};
I/O stream .cpp:
SoundSystem::SoundSystem()
{
_stream = NULL;
_readBufferSize = FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE;
_readBuffer= new unsigned char [_readBufferSize];
}
SoundSystem::~SoundSystem()
{
}
bool SoundSystem::initPa()
{
if ((Pa_Initialize()) != paNoError)
return (false);
return (true);
}
bool SoundSystem::openStream()
{
if ((Pa_OpenStream(&_stream, &_inputParam, &_outputParam, SAMPLE_RATE,
FRAMES_PER_BUFFER, paClipOff, NULL, NULL)) != paNoError)
return (false);
return (true);
}
bool SoundSystem::startStream()
{
if ((Pa_StartStream(_stream)) != paNoError)
return (false);
return (true);
}
bool SoundSystem::initStream()
{
if ((openStream()) == false)
std::cerr << "can not open stream" << std::endl;
if ((startStream()) == false)
std::cerr << "cannot start stream" <<std::endl;
return (true);
}
bool SoundSystem::initParams()
{
if ((initPa()) == false)
std::cerr << "can not ijnit PA" << std::endl;
initInputParams();
initOutputParams();
return (true);
}
bool SoundSystem::initInputParams()
{
if ((_inputParam.device = Pa_GetDefaultInputDevice()) == paNoDevice)
return (false);
_inputParam.channelCount = 2;
_inputParam.sampleFormat = PA_SAMPLE_TYPE;
_inputParam.suggestedLatency = Pa_GetDeviceInfo(_inputParam.device)->defaultLowInputLatency;
_inputParam.hostApiSpecificStreamInfo = NULL;
return (true);
}
bool SoundSystem::initOutputParams()
{
if ((_outputParam.device = Pa_GetDefaultInputDevice()) == paNoDevice)
return (false);
_outputParam.channelCount = 2;
_outputParam.sampleFormat = PA_SAMPLE_TYPE;
_outputParam.suggestedLatency = Pa_GetDeviceInfo(_outputParam.device)->defaultLowInputLatency;
_outputParam.hostApiSpecificStreamInfo = NULL;
return (true);
}
bool SoundSystem::writeOnStream(unsigned char *buff)
{
if ((Pa_WriteStream(_stream, buff, FRAMES_PER_BUFFER)) != paNoError)
{
std::cout << "FAIL WRITE" <<std::endl;
return (false);
}
return (true);
}
bool SoundSystem::readFromStream()
{
if ((Pa_ReadStream(_stream, _readBuffer, FRAMES_PER_BUFFER)) != paNoError)
return (false);
return (true);
}
void SoundSystem::cleanReadBuffer()
{
for (int i = 0; i != _readBufferSize; i++)
_readBuffer[i] = 0;
}
int SoundSystem::getReadBufferSize() const
{enter code here
return (_readBufferSize);
}
unsigned char* SoundSystem::getReadBuffer() const { return (_readBuffer); }
Encode header:
#define FRAME_SIZE (960)
#define SAMPLE_RATE (48000)
#define CHANNELS (2)
#define APPLICATION OPUS_APPLICATION_VOIP
#define MAX_FRAME_SIZE (6*960)
class EncoderSystem
{
private:
OpusEncoder *_encode;
OpusDecoder *_decode;
opus_int16 _in[FRAME_SIZE*CHANNELS];
opus_int16 _out[MAX_FRAME_SIZE*CHANNELS];
int _nbBytes;
public:
EncoderSystem();
~EncoderSystem();
bool encoderCreate();
bool decoderCreate();
unsigned char* encode(unsigned char *, int);
unsigned char* decode(unsigned char *, int);
int getEncodeLen() const;
};
Encode .cpp:
EncoderSystem::EncoderSystem()
{
}
EncoderSystem::~EncoderSystem()
{
}
bool EncoderSystem::encoderCreate()
{
int error;
if ((_encode = opus_encoder_create(SAMPLE_RATE, CHANNELS, OPUS_APPLICATION_VOIP, &error)) == NU\
LL)
{
std::cerr << "Can not create encode" <<std::endl;
return (false);
}
return (true);
}
bool EncoderSystem::decoderCreate()
{
int error;
if ((_decode = opus_decoder_create(SAMPLE_RATE, CHANNELS, &error)) == NULL)
{
std::cerr << "Can not create decoder" <<std::endl;
return (false);
}
return (true);
}
unsigned char* EncoderSystem::encode(unsigned char *data, int size)
{
unsigned char *c_bits = new unsigned char [size];
memcpy(_in, data, size);
/* Encode the frame. */
_nbBytes = opus_encode(_encode, _in, FRAME_SIZE, c_bits, size);
if (_nbBytes<0)
{
std::cerr << "cannot decode" << std::endl;
return NULL;
}
return (c_bits);
}
unsigned char* EncoderSystem::decode(unsigned char *data, int size)
{
int frame_size = opus_decode(_decode, data, size, _out,
MAX_FRAME_SIZE * CHANNELS * 2, 0);
unsigned char *pcm_bytes = new unsigned char [MAX_FRAME_SIZE * CHANNELS * 2];
if (frame_size<0)
{
std::cerr << "cannot decode" << std::endl;
return (NULL);
}
memcpy(pcm_bytes, _out, size);
return (pcm_bytes);
}
int EncoderSystem::getEncodeLen() const { return (this->_nbBytes); }
I really need you, thanks a lot to take your time to help me.
#define PA_SAMPLE_TYPE paInt24
That's probably your problem. As far as I know the standard OPUS codecs take 16-bit integers or 32-bit floating point samples. These correspond to the PortAudio sample types paInt16 and paFloat32.
I recommend getting the types of all your sample buffers correct. Using unsigned char* for formatted sample data is asking for trouble. You need to understand what data types are expected by PortAudio functions and by the OPUS codec functions.