171 lines
6.1 KiB
C++
171 lines
6.1 KiB
C++
|
|
#include "PlayWaveSound.h"
|
||
|
|
#include "portaudio.h"
|
||
|
|
#include <QtCore>
|
||
|
|
|
||
|
|
//#define NUM_SECONDS (11)
|
||
|
|
//#define SAMPLE_RATE (65536)
|
||
|
|
//#define FRAMES_PER_BUFFER (1024)
|
||
|
|
|
||
|
|
//#ifndef M_PI
|
||
|
|
//#define M_PI (3.14159265)
|
||
|
|
//#endif
|
||
|
|
|
||
|
|
//#define TABLE_SIZE (200)
|
||
|
|
|
||
|
|
//typedef struct {
|
||
|
|
// QString channelId;
|
||
|
|
// QVector<float> wavedata;
|
||
|
|
//} WAVE_CSVDATA1;
|
||
|
|
|
||
|
|
//WAVE_CSVDATA1 m_vecCsvData[32];
|
||
|
|
|
||
|
|
|
||
|
|
PlayWaveSound::PlayWaveSound(QObject *parent) : QObject(parent)
|
||
|
|
{
|
||
|
|
Init();
|
||
|
|
}
|
||
|
|
|
||
|
|
PlayWaveSound::~PlayWaveSound()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
void PlayWaveSound::Init()
|
||
|
|
{
|
||
|
|
// PaStreamParameters outputParameters;
|
||
|
|
// PaStream *stream;
|
||
|
|
// PaError err;
|
||
|
|
// float buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
|
||
|
|
// float sine[TABLE_SIZE]; /* sine wavetable */
|
||
|
|
// float *Data = NULL;
|
||
|
|
// Data = (float*)malloc(4);
|
||
|
|
// int left_phase = 0;
|
||
|
|
// int right_phase = 0;
|
||
|
|
// int left_inc = 1;
|
||
|
|
// int right_inc = 1; /* higher pitch so we can distinguish left and right. */
|
||
|
|
// int i, j, k;
|
||
|
|
// int bufferCount;
|
||
|
|
|
||
|
|
// printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
|
||
|
|
|
||
|
|
// QFile file("e:\\22.csv");
|
||
|
|
// if (!file.open(QIODevice::ReadOnly))
|
||
|
|
// {
|
||
|
|
// qDebug() << "::MainWidget::ReadDatData() cannot open file for read";
|
||
|
|
// }
|
||
|
|
// QTextStream * out = new QTextStream(&file);//文本流
|
||
|
|
// QStringList tempOption = out->readAll().split("\n");//每行以\n区分
|
||
|
|
// qDebug() << "tempOption.count " << tempOption.count() << endl;
|
||
|
|
// QStringList tempbar;
|
||
|
|
// for(int i = 1 ; i < tempOption.count()-1 ; i++)
|
||
|
|
// {
|
||
|
|
// tempbar = tempOption.at(i).split(",");//一行中的单元格以,区分
|
||
|
|
// for(int j = 0; j < tempbar.size();j++)
|
||
|
|
// {
|
||
|
|
// m_vecCsvData[j].wavedata.push_back(tempbar.at(j).toFloat()/1000);
|
||
|
|
// m_vecCsvData[j].channelId = QString(j+1);
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// qDebug() << "tempbar " << m_vecCsvData[1].wavedata.size() << endl;
|
||
|
|
// file.close();//
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// for( i=0; i<TABLE_SIZE; i++ )
|
||
|
|
// {
|
||
|
|
// //sine[i] =
|
||
|
|
// float a = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ) ;
|
||
|
|
// //printf("data = %f\n", m_vecCsvData[1].wavedata[i]);
|
||
|
|
// m_vecCsvData[4].wavedata.push_back(a);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// err = Pa_Initialize();
|
||
|
|
// if( err != paNoError ) goto error;
|
||
|
|
|
||
|
|
// outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
|
||
|
|
// if (outputParameters.device == paNoDevice) {
|
||
|
|
// fprintf(stderr,"Error: No default output device.\n");
|
||
|
|
// goto error;
|
||
|
|
// }
|
||
|
|
// outputParameters.channelCount = 2; /* stereo output */
|
||
|
|
// outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
|
||
|
|
// outputParameters.suggestedLatency =Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency; // Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
|
||
|
|
// outputParameters.hostApiSpecificStreamInfo = NULL;
|
||
|
|
|
||
|
|
// 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 */
|
||
|
|
// NULL, /* no callback, use blocking API */
|
||
|
|
// NULL ); /* no callback, so no callback userData */
|
||
|
|
// if( err != paNoError ) goto error;
|
||
|
|
|
||
|
|
|
||
|
|
// printf( "Play 3 times, higher each time.\n" );
|
||
|
|
|
||
|
|
// //for( k=0; k < 3; ++k )
|
||
|
|
// {
|
||
|
|
// err = Pa_StartStream( stream );
|
||
|
|
// if( err != paNoError ) goto error;
|
||
|
|
|
||
|
|
// printf("Play for %d seconds.\n", NUM_SECONDS );
|
||
|
|
|
||
|
|
// bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
|
||
|
|
|
||
|
|
// int count = 0;
|
||
|
|
// printf("bufferCount %d .\n", bufferCount );
|
||
|
|
// for( i=0; i < bufferCount; i++ )
|
||
|
|
// {
|
||
|
|
// for( j=0; j < FRAMES_PER_BUFFER; j++ )
|
||
|
|
// {
|
||
|
|
// count = j + i*FRAMES_PER_BUFFER;
|
||
|
|
// buffer[j][0] = m_vecCsvData[0].wavedata[count]; /* left */
|
||
|
|
// buffer[j][1] = m_vecCsvData[0].wavedata[count]; /* right */
|
||
|
|
// //buffer[j][0] = m_vecCsvData[4].wavedata[left_phase]; /* left */
|
||
|
|
// //buffer[j][1] = m_vecCsvData[4].wavedata[right_phase]; /* right */
|
||
|
|
// left_phase += left_inc;
|
||
|
|
// if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
|
||
|
|
// right_phase += right_inc;
|
||
|
|
// if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
|
||
|
|
|
||
|
|
// }
|
||
|
|
|
||
|
|
// err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
|
||
|
|
// if( err != paNoError ) goto error;
|
||
|
|
// }
|
||
|
|
// printf("count = %d\n",count);
|
||
|
|
// err = Pa_StopStream( stream );
|
||
|
|
// if( err != paNoError ) goto error;
|
||
|
|
|
||
|
|
// // ++left_inc;
|
||
|
|
// // ++right_inc;
|
||
|
|
|
||
|
|
// Pa_Sleep( 1000 );
|
||
|
|
// }
|
||
|
|
|
||
|
|
// err = Pa_CloseStream( stream );
|
||
|
|
// if( err != paNoError ) goto error;
|
||
|
|
|
||
|
|
// Pa_Terminate();
|
||
|
|
// printf("Test finished.\n");
|
||
|
|
// qDebug() << "PlaySound end" << endl;
|
||
|
|
// return ;
|
||
|
|
|
||
|
|
// error:
|
||
|
|
// fprintf( stderr, "An error occurred while using the portaudio stream\n" );
|
||
|
|
// fprintf( stderr, "Error number: %d\n", err );
|
||
|
|
// fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
|
||
|
|
// // Print more information about the error.
|
||
|
|
// if( err == paUnanticipatedHostError )
|
||
|
|
// {
|
||
|
|
// const PaHostErrorInfo *hostErrorInfo = Pa_GetLastHostErrorInfo();
|
||
|
|
// fprintf( stderr, "Host API error = #%ld, hostApiType = %d\n", hostErrorInfo->errorCode, hostErrorInfo->hostApiType );
|
||
|
|
// fprintf( stderr, "Host API error = %s\n", hostErrorInfo->errorText );
|
||
|
|
// }
|
||
|
|
// Pa_Terminate();
|
||
|
|
|
||
|
|
// return ;
|
||
|
|
}
|