tag:C,音頻播放,libsndfile,PortAudio
/* Create by zyzx
* Created 2008-08-09
* Modified 2008-08-09
*/
一、準備
?????? 1、依賴庫PortAudio(
http://www.portaudio.com/)
???????????? Win32平臺編譯見《
Win32環境PortAudio庫編譯簡介:音頻播放》
?????? 2、依賴庫libsndfile (
http://www.mega-nerd.com/libsndfile/)
???????????? 簡介:是一個開源跨平臺C庫,用于解碼一系列的音頻文件格式如WAV,AIFF,SF等等。初期支持Mp3格式的,后來由于種種原因取消了對Mp3格式的支持。
???????????? 編譯環境:
MinGW+MSYS《
MinGW+MSYS的C\C++編程環境安裝與升級》
???????????? 1)從libsndfile上下載libsndfile-1.0.17.tar.gz(當前最高版本)
???????????? 2)將libsndfile-1.0.17.tar.gz復制到MinGW/Msys安裝目錄Msys/home目錄下
???????????? 3)打開Msys模擬環境
?????????????????? $ cd /home
?????????????????? $ tar jxf libsndfile-1.0.17.tar.gz && cd libsndfile-1.0.17
?????????????????? $ ./configure
?????????????????? $ make && make install
???????????? 4)如果懶點,直接使用源碼包中發布的libsndfile-1.dll和sndfile.h文件得了。
二、編碼試驗
??????? 嘿,,經過層層碰壁后,,小弟終于初窺了點門道,,成功的摸索出了一條小道。。不過不要緊,萬事開頭難,,接下來嗎。。距離自己的音頻播放器又近了一步。。
??????? 值得注意的地方:
??????? 如下源碼參照 PortAudio庫中示例patest_read_write_wire.c 和 libsndfile 庫中示例sndfile-play.c 文件進行的修改。。所以還有很多地方懶得去理會,代碼難看點。。
??????? 1)其中 while(1) { ......} 語句,可以提取到一個線程中去。此線程專門負責給聲卡喂數據,使用雙緩存或三緩存,當某個緩存塊數據全部喂到聲卡后,通知數據讀取線程開工了。
??????? 2)為了節省空間咱不能一下申請WIN32_AUDIO_BUFF_LEN這大的內存區域,使用多線程來解決。添加數據讀取線程。。
??????? 3)為了可以播放,暫停,... 咱得提供一個GUI界面,所以就涉及到線程通訊了哦。。
??????? 4)為了保證跨平臺性,還得去找個跨平臺的線程庫啊。。Boost庫中包含線程相關的,就先學習ing...
下面是源碼:(還要在vs里設置庫環境)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "portaudio.h"
#include "windows.h"
#include "sndfile.h"
#define SF_DLL??? ??? "libsndfile-1.dll"
typedef int??? ??? ?? ?????? (*P_sf_command)(SNDFILE *sndfile, int command, void *data, int datasize);
typedef SNDFILE*??? (*P_sf_open)(const char *path, int mode, SF_INFO *sfinfo);
typedef sf_count_t???? (*P_sf_read_short)(SNDFILE *sndfile, short *ptr, sf_count_t items);
typedef int??? ??? ??????? ?? (*P_sf_close)(SNDFILE *sndfile);
typedef int??? ??? ????????? (*P_sf_perror)(SNDFILE *sndfile);
typedef const char*?? (*P_sf_strerror)(SNDFILE *sndfile);
typedef sf_count_t??? (*P_sf_read_float)(SNDFILE *sndfile, float *ptr, sf_count_t items);
typedef sf_count_t??? (*P_sf_read_int)(SNDFILE *sndfile, int *ptr, sf_count_t items);
#define sf_command( hInstan, p )??? ??? P_sf_command p = (P_sf_command)GetProcAddress( hInstan, "sf_command")
#define sf_open( hInstan, p )??? ??? ??? P_sf_open p = (P_sf_open)GetProcAddress( hInstan, "sf_open" )
#define sf_close( hInstan, p )??? ??? ??? P_sf_close p = (P_sf_close)GetProcAddress( hInstan, "sf_close" )
#define sf_perror( hInstan, p )??? ??? ??? P_sf_perror p = (P_sf_perror)GetProcAddress( hInstan, "sf_perror" )
#define sf_read_short( hInstan, p )??? ??? P_sf_read_short p = (P_sf_read_short)GetProcAddress( hInstan, "sf_read_short" )
#define sf_read_int( hInstan, p )??? ??? P_sf_read_int p = (P_sf_read_int)GetProcAddress( hInstan, "sf_read_int" )
#define sf_read_float( hInstan, p )??? ??? P_sf_read_float p = (P_sf_read_float)GetProcAddress( hInstan, "sf_read_float" )
#define sf_strerror( hInstan, p )??? ??? P_sf_strerror p = (P_sf_strerror)GetProcAddress( hInstan, "sf_strerror" )
//* - 整上面這些也是沒辦法啊,在VS里只有*.dll和頭文件,咱只有動態加載了。
//* - 保存動態庫句柄
HINSTANCE?? ghIns;??
/* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
#define SAMPLE_RATE (44100)?????????????????????????????????????? //* - 采樣率
#define FRAMES_PER_BUFFER ( 64 * 1024 )???????????????? //* - 緩存大小 - 向聲卡驅動寫數據時候的
#define NUM_CHANNELS??? (2)?????????????????????????????????????????? //* - 聲道數
/* #define DITHER_FLAG???? (paDitherOff) */
#define DITHER_FLAG???? (0) /**/
/* Select sample format. */
#define PA_SAMPLE_TYPE paFloat32???????????????????????????? //* - 采樣點占用大小32位的float型
#define SAMPLE_SIZE (4)?????????????????????????????????????????????????? //* - 采樣點占用4字節
#define SAMPLE_SILENCE (0.0f)
#define CLEAR(a) bzero( (a), FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
#define PRINTF_S_FORMAT "%.8f"
#define WIN32_AUDIO_BUFF_LEN??? 32 * 1024 * 1024 //* - 音頻數據(解碼后)緩沖區域大小
typedef struct
{
??? float*??? ??? buffer;????????????????????? //* - 音頻數據(解碼后)緩沖區域
??? long??? ??? current, bufferlen ;??? //* - 當前播放到達的數據點,實際全部音頻數據長度
??? SNDFILE ??? *sndfile ;???????????? //* - libsndfile 庫 基本 結構 -- 類似句柄或結構體指針
??? SF_INFO ??? sfinfo ;??????????????? //* - libsndfile 庫 讀取的音頻基本信息(如采樣率,聲道,等等)
??? sf_count_t??? remaining ;???????? //* - 好像沒用到
} Win32_Audio_Data ;
/*******************************************************************/
int main(int argc, char *argv [] );
int main(int argc, char *argv [] )
{
??? HINSTANCE?? hIns;
??? hIns = LoadLibrary( SF_DLL );????????? //* 加載 libsndfile-1.dll
??? Win32_Audio_Data audio_data;
??? audio_data.buffer = new float[ WIN32_AUDIO_BUFF_LEN ] ;
??? audio_data.current = 0;
??? ghIns = hIns;
??? PaStreamParameters inputParameters, outputParameters;
??? PaStream *stream = NULL;
??? PaError err;
??? int i;
??? int numBytes;
??? printf("patest_read_write_wire.c\n"); fflush(stdout);
??? err = Pa_Initialize();
??? if( err != paNoError ) goto error;
??? //* - 打開****.***音頻文件
??? sf_open( ghIns, Open );
??? if (! (audio_data.sndfile = Open (argv[1], SFM_READ, &(audio_data.sfinfo))))
??? {???
??? ??? sf_strerror( ghIns, Strerror );
??? ??? puts ( Strerror(NULL) ) ;
??? ??? goto error;
??? }
??? //* 以float格式讀取并解析音頻格式 長度標為WIN32_AUDIO_BUFF_LEN,基本上能全部讀取文件
??? sf_read_float( ghIns, ReadFloat );
??? audio_data.bufferlen += (long) ReadFloat (audio_data.sndfile, (float*)(audio_data.buffer ), WIN32_AUDIO_BUFF_LEN/*audio_data.sfinfo.frames */) ;
??? printf( "-------------------------------\n" );
??? printf( "Output samplerate %d\n", audio_data.sfinfo.samplerate );
??? printf( "Output frames %d\n", audio_data.sfinfo.frames );
??? printf( "Output channels %d\n", audio_data.sfinfo.channels );
??? printf( "seekable %d\n ", audio_data.sfinfo.seekable );
??? printf( "format is %d\n ", audio_data.sfinfo.format );
??? printf( "Sections is %d\n ", audio_data.sfinfo.sections );
??? printf( "Read buffer len %d\n" , audio_data.bufferlen );
??? printf( "-------------------------------\n" );
??? //inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
??? //printf( "Input device # %d.\n", inputParameters.device );
??? //printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency );
??? //printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency );
??? //inputParameters.channelCount = audio_data.sfinfo.channels;//NUM_CHANNELS;
??? //inputParameters.sampleFormat = PA_SAMPLE_TYPE;
??? //inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ;
??? //inputParameters.hostApiSpecificStreamInfo = NULL;
??? outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
??? printf( "Output device # %d.\n", outputParameters.device );
??? printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency );
??? printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency );
??? outputParameters.channelCount = audio_data.sfinfo.channels;//NUM_CHANNELS;
??? outputParameters.sampleFormat = PA_SAMPLE_TYPE;
??? outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
??? outputParameters.hostApiSpecificStreamInfo = NULL;
??? /* -- setup -- */
??? err = Pa_OpenStream(
??? ??? &stream,
??? ??? NULL,//&inputParameters,???????????????????????????????????????? //* - 麥克風
??? ??? &outputParameters,?????????????????????????????????????????????????? //* - 揚聲器
??? ??? audio_data.sfinfo.samplerate,//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;
??? //err = Pa_StartStream( stream );
??? //if( err != paNoError ) goto error;
??? //printf("Wire on. Will run one minute.\n"); fflush(stdout);
??? //for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
??? //{
??? //??? err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
??? //??? if( err ) goto xrun;
??? //??? err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
??? //??? if( err ) goto xrun;
??? //}
??? //err = Pa_StopStream( stream );
??? //if( err != paNoError ) goto error;
??? //CLEAR( sampleBlock );
??? err = Pa_StartStream( stream );
??? if( err != paNoError ) goto error;
??? printf("Wire on. Interrupt to stop.\n"); fflush(stdout);
??? while( 1 )
??? {
??????? //* - 此函數為同步函數,它一直等待到buffer中的數據全部寫入聲卡驅動才返回
??????? //* - 緩沖去長度為FRAMES_PER_BUFFER,指的是一個聲道上的數據大小
??? ??? err = Pa_WriteStream( stream, audio_data.buffer + audio_data.current/*sampleBlock*/, FRAMES_PER_BUFFER );
??? ??? audio_data.current = audio_data.current + FRAMES_PER_BUFFER * NUM_CHANNELS ;
??? ??? //if( audio_data.current + FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE > audio_data.bufferlen ) break;
??? ??? printf( "%d\n", audio_data.current);
??? ??? if( err ) goto xrun;
??? ??? //err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
??? ??? //if( err ) goto xrun;
??? }
??? err = Pa_StopStream( stream );
??? if( err != paNoError ) goto error;
??? Pa_CloseStream( stream );
??? free( sampleBlock );
??? Pa_Terminate();
??? return 0;
xrun:
??? if( stream ) {
??? ??? Pa_AbortStream( stream );
??? ??? Pa_CloseStream( stream );
??? }
??? free( sampleBlock );
??? Pa_Terminate();
??? if( err & paInputOverflow )
??? ??? fprintf( stderr, "Input Overflow.\n" );
??? if( err & paOutputUnderflow )
??? ??? fprintf( stderr, "Output Underflow.\n" );
??? delete [] audio_data.buffer;
??? FreeLibrary( ghIns );
??? return -2;
error:
??? if( stream ) {
??? ??? Pa_AbortStream( stream );
??? ??? Pa_CloseStream( stream );
??? ??? FreeLibrary( ghIns );
??? }
??? free( sampleBlock );
??? Pa_Terminate();
??? fprintf( stderr, "An error occured while using the portaudio stream\n" );
??? fprintf( stderr, "Error number: %d\n", err );
??? fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
??? delete [] audio_data.buffer;
??? FreeLibrary( ghIns );
??? return -1;
}
三、某次播放Wav文件 截圖 -- 紀念