簡體   English   中英

什么是輕量級跨平台WAV播放庫?

[英]What is a lightweight cross platform WAV playing library?

我正在尋找一種輕巧的方法來使我的程序(用C語言編寫)能夠在Windows或Linux上播放音頻文件。 我目前正在使用Windows本機調用,這實際上只是傳遞文件名的單個調用。 我想要在Linux上也可以使用的類似軟​​件。

音頻文件是Microsoft PCM,單通道,22Khz

有什么建議么?

由於我也在尋找問題的答案,因此我做了一些研究,但還沒有找到任何簡單的方法(例如調用一個函數)來播放音頻文件。 但是使用一些代碼行,甚至可以使用已經提到的portaudio和libsndfile(LGPL)以可移植的方式實現。

這是我為測試兩個庫而編寫的一個小測試用例:


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

static int
output_cb(const void * input, void * output, unsigned long frames_per_buffer,
        const PaStreamCallbackTimeInfo *time_info,
        PaStreamCallbackFlags flags, void * data)
{
    SNDFILE * file = data;

    /* this should not actually be done inside of the stream callback
     * but in an own working thread 
     *
     * Note although I haven't tested it for stereo I think you have
     * to multiply frames_per_buffer with the channel count i.e. 2 for
     * stereo */
    sf_read_short(file, output, frames_per_buffer);
    return paContinue;
}

static void
end_cb(void * data)
{
    printf("end!\n");
}

#define error_check(err) \
    do {\
        if (err) { \
            fprintf(stderr, "line %d ", __LINE__); \
            fprintf(stderr, "error number: %d\n", err); \
            fprintf(stderr, "\n\t%s\n\n", Pa_GetErrorText(err)); \
            return err; \
        } \
    } while (0)

int
main(int argc, char ** argv)
{
    PaStreamParameters out_param;
    PaStream * stream;
    PaError err;
    SNDFILE * file;
    SF_INFO sfinfo;

    if (argc < 2)
    {
        fprintf(stderr, "Usage %s \n", argv[0]);
        return 1;
    }

    file = sf_open(argv[1], SFM_READ, &sfinfo);
    printf("%d frames %d samplerate %d channels\n", (int)sfinfo.frames,
            sfinfo.samplerate, sfinfo.channels);

    /* init portaudio */
    err = Pa_Initialize();
    error_check(err);

    /* we are using the default device */
    out_param.device = Pa_GetDefaultOutputDevice();
    if (out_param.device == paNoDevice)
    {
        fprintf(stderr, "Haven't found an audio device!\n");
        return -1;
    }

    /* stero or mono */
    out_param.channelCount = sfinfo.channels;
    out_param.sampleFormat = paInt16;
    out_param.suggestedLatency = Pa_GetDeviceInfo(out_param.device)->defaultLowOutputLatency;
    out_param.hostApiSpecificStreamInfo = NULL;

    err = Pa_OpenStream(&stream, NULL, &out_param, sfinfo.samplerate,
            paFramesPerBufferUnspecified, paClipOff,
            output_cb, file);
    error_check(err);

    err = Pa_SetStreamFinishedCallback(stream, &end_cb);
    error_check(err);

    err = Pa_StartStream(stream);
    error_check(err);

    printf("Play for 5 seconds.\n");
    Pa_Sleep(5000);

    err = Pa_StopStream(stream);
    error_check(err);

    err = Pa_CloseStream(stream);
    error_check(err);

    sf_close(file);

    Pa_Terminate();

    return 0;
}

該示例的一些注釋。 在流回調內部進行數據加載不是好習慣,而是在自己的加載線程內部進行數據加載。 如果您需要播放多個音頻文件,這將變得更加困難,因為並非所有portaudio后端都支持一個設備的多個流,例如OSS后端不支持,而ALSA后端則支持。 我不知道Windows上的情況如何。 由於所有輸入文件都是相同的類型,因此您可以自己將它們混合使用,這會使代碼更加復雜,但同時您還支持OSS。 如果您還有不同的采樣率或通道數,則將變得非常困難。

因此,如果您不想同時播放多個文件,這可能是一個解決方案,或者至少是您的一個開始。

SDL_Mixer雖然不是很輕巧,但確實具有播放WAV文件的簡單界面。 我相信,像SDL一樣,SDL_Mixer也是LGPL。

OpenAL是另一個跨平台音頻庫,更適合3D音頻。

您可能要檢查的另一個開源音頻庫是PortAudio

我已經使用OpenAL在空中交通管制系統中播放wav文件作為警報/警告

我發現的優勢是

  1. 這是跨平台
  2. 與C一起工作(以及其他人,但您的問題與C有關)
  3. 重量輕
  4. 網上有不錯的文檔
  5. 許可證是LGPL,因此您調用API時不會出現許可證問題

您可以嘗試以下一種: libao

我用了irrKlang!

“ irrKlang是針對C ++,C#和所有.NET語言的跨平台聲音庫”

http://www.ambiera.com/irrklang/

我喜歡FMOD 許可證免費供個人使用,對於小型共享軟件或商業項目非常合理

您也可以嘗試Audiere 最新版本發布於2006年,但是它是開源的,並根據LGPL許可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM