Play .Wav file c++ with PortAudio and libsndfile get errors for ALSA

  alsa, c++, cross-platform, libsndfile, portaudio

I am working on a Game Library for C++, now I am on audio part.
After a lots of topic read, I obtained for using PortAudio with libsndfile.

Header:

#ifndef AUDIO_H
#define AUDIO_H


#include "sndfile.h"
#include "portaudio.h"
#include <iostream>


#define FRAMES_PER_BUFFER   (512)

typedef struct
{
    SNDFILE     *file;
    SF_INFO      info;
} callback_data_s;

static int callback(    const void *input, void *output,
                    unsigned long frameCount,
                    const PaStreamCallbackTimeInfo* timeInfo,
                    PaStreamCallbackFlags statusFlags,
                    void *userData );


/**
 * Initialize Audio library
 */ 
void initAudio();
    
/**
 * Close Audio Library
 */ 
void termAudio();


class Audio{

private:
    SNDFILE *file;
    PaStream *stream;
    callback_data_s data;

public:

    Audio(const char* path);

    void play();

    void sleep(long millis);

    void stop();

    void free();
};

#endif

Cpp:

#include "Audio.h"

static int callback(const void *input, void *output, unsigned long frameCount, 
    const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void         
*userData)
{
float           *out;
callback_data_s *p_data = (callback_data_s*)userData;
sf_count_t       num_read;

out = (float*)output;
p_data = (callback_data_s*)userData;

delete out;

/* read directly into output buffer */
num_read = sf_read_float(p_data->file, out, frameCount * p_data->info.channels);

/*  If we couldn't read a full frameCount of samples we've reached EOF */
if (num_read < frameCount)
{
    return paComplete;
}

return paContinue;
}


Audio::Audio(const char* path){

this->data.file = sf_open(path, SFM_READ, &this->data.info);
if(sf_error(this->data.file) != SF_ERR_NO_ERROR){
    std::cout<<"Error loading audio: "<<std::string(path)<<" err: " 
<<sf_strerror(data.file);
}else{
    PaError err = Pa_OpenDefaultStream(&stream, 0, data.info.channels, 
                                        paFloat32, data.info.samplerate,         
FRAMES_PER_BUFFER,
                                        callback, &data);

    if(err != paNoError){
        std::cout<<"Error PortAudio: "<<Pa_GetErrorText(err)<<"n";
    }


}
}

void Audio::play(){

PaError err = Pa_StartStream(stream);

while(Pa_IsStreamActive(stream)){
    
}

if(err != paNoError){
    std::cout<<"Error PortAudio: "<<Pa_GetErrorText(err)<<std::endl;
}
}

void Audio::stop(){
PaError err = Pa_StopStream(stream);
if(err != paNoError){
    std::cout<<"Error PortAudio: "<<Pa_GetErrorText(err)<<std::endl;
}
}

void Audio::sleep(long millis){
Pa_Sleep(millis);
}

void Audio::free(){
PaError err = Pa_CloseStream(stream);
if(err != paNoError){
    std::cout<<"Error PortAudio: "<<Pa_GetErrorText(err)<<std::endl;
}
}

void initAudio(){

//Initializing PortAudio
std::cout<<"Initializing PortAudio...n";
PaError err = Pa_Initialize();
if(err != paNoError){
    std::cout<<"Could not initialize PortAudio: "<<Pa_GetErrorText(err)<<std::endl<<std::endl;
    exit(-1);
}
std::cout<<"PortAudio Initializednn";
}

void termAudio(){
std::cout<<"Terminating PortAudio...n";
PaError err = Pa_Terminate();
if(err != paNoError){
    std::cout<<"Error PortAudio: "<<Pa_GetErrorText(err)<<std::endl;
}else{
    std::cout<<"PortAudio terminatednn";
}
}

I’ve already installed ALSA and Jack (I am on Ubuntu).
I am tryna making cross-platform for W10, so I installed mingw.
Compiling and Executing the Window task, everything is ok (I run .exe with wine).
When executing linux one, I get these errors below:

Initializing PortAudio...

Expression 'alsa_snd_pcm_hw_params_set_buffer_size_near( pcm, hwParams, &alsaBufferFrames )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 923

Expression 'alsa_snd_pcm_hw_params_set_buffer_size_near( pcm, hwParams,&alsaBufferFrames)' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 923

ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card

PortAudio Initialized

Source: Windows Questions C++

LEAVE A COMMENT