簡體   English   中英

使用C ++中的libsndfile從WAV文件中提取原始音頻數據

[英]Extract raw audio data from WAV files using libsndfile in C++

我正在開發一個將向某些設備提供音頻輸入的應用程序。 該設備希望以原始音頻數據流(16位,48kHz)的形式提供音頻輸入。 因此,無論wave文件中的音頻數據格式(8位,16位,24位,32位等)如何,我都想從WAV文件中提取原始音頻數據。 我計划為此目的使用libsndFile庫。 我修改了libsndfile的C ++示例代碼,如下所示:

#include "stdafx.h"
#include <sndfile.hh>

static void create_file (const char * fname, int format, const short* buffer,const   unsigned int& len)
{   
    // file ;
    int channels = 1 ;  //A Mono wave file.
    int srate = 48000 ;

    printf ("Creating file named '%s'\n", fname) ;

    SndfileHandle file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ;

    int x = file.write (buffer, len) ;
}

static void read_file (const char * fname)
{   
    SndfileHandle       file ;

    file = SndfileHandle (fname) ; 

    const unsigned int uiBuffLen = file.channels() * file.frames();
    short* data = new short [uiBuffLen] ;
    memset(data,0x00,uiBuffLen);

    int x = file.command(SFC_SET_SCALE_FLOAT_INT_READ, (void*)data, uiBuffLen);
    file.read (data, uiBuffLen) ;   //Read the audio data in the form of 16 bit short integer

    //Now create a new wave file with audio data in the form of 16 bit short integers
    create_file ("ConvertedFile.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16,data, (const unsigned int&)uiBuffLen) ;

    //Now fill a buffer containing audio data and dump it into a file so that the same can be fed to a device expecting the raw audio data

    unsigned char* bytBuffer = new unsigned char[uiBuffLen*2];
    memset(bytBuffer, 0x00, uiBuffLen*2);
    file.readRaw(bytBuffer, uiBuffLen*2);

    FILE * pFile;
    pFile = fopen ("RawAudio.dat","w");
    if (pFile!=NULL)
    {
        fwrite(bytBuffer, 1, uiBuffLen*2, pFile);
        fclose (pFile);
    }       

    delete [] data;
    delete [] bytBuffer;
    }

int _tmain(int argc, _TCHAR* argv[])
{
    //The sample file is a Mono file containing audio data in float format.
    const char * fname = "MonoWavFile.wav" ;

    read_file (fname) ;

    return 0;
}

好了,上面的代碼可能看起來很可怕,但是我現在只是在尋找這個主意。 我使用的文件“ MonoWaveFile.wav”是單聲道波形文件,並且具有32位浮點值形式的音頻數據。 我使用libsndfile庫創建一個新文件“ ConvertedFile.wav”。 該文件具有16位PCM格式的音頻數據。 我在媒體播放器中播放了此文件,並且看到轉換已正確完成。

然后,我創建另一個文件“ RawAudio.dat”以僅保存音頻數據,該文件可用於將音頻輸入饋送到設備。 文件已創建,當我將其發送到設備時,音頻根本不正確。 這表明我做錯了什么。 誰能讓我知道我在做什么錯? 我以前從未從事過此類工作,因此,如果能獲得任何幫助,我將不勝感激。

我使用了sf_open_virtual 我使用sf_open_virtual填充了另一個緩沖區。 然后,我使用此“另一個緩沖區”來饋送RTP數據包。 所以我想這就是您所需要的。

我在使它成為從RTP數據包到文件的其他方向時遇到了問題。

//============================================================================
// Name        : libsndfile_demo.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string.h>
#include "sndfile.h"
#include <assert.h>
#include <unistd.h>


using namespace std;

typedef struct
{   sf_count_t offset, length ;
    unsigned char data [160] ;
} VIO_DATA ;


FILE *checker;
const void* old_ptr = NULL;

static sf_count_t vfget_filelen (void *user_data)
{
    VIO_DATA *vf = (VIO_DATA *) user_data ;

    return vf->length ;
} /* vfget_filelen */

static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data)
{
    VIO_DATA *vf = (VIO_DATA *) user_data ;

    switch (whence)
    {   case SEEK_SET :
            vf->offset = offset ;
            break ;

        case SEEK_CUR :
            vf->offset = vf->offset + offset ;
            break ;

        case SEEK_END :
            vf->offset = vf->length + offset ;
            break ;
        default :
            break ;
        } ;

    return vf->offset ;
} /* vfseek */

static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data)
{
    VIO_DATA *vf = (VIO_DATA *) user_data ;

    /*
    **  This will brack badly for files over 2Gig in length, but
    **  is sufficient for testing.
    */
    if (vf->offset + count > vf->length)
        count = vf->length - vf->offset ;

    memcpy (ptr, vf->data + vf->offset, count) ;
    vf->offset += count ;

    return count ;
} /* vfread */

static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data)
{
    static int skip = 0;

    //TODO: Why this is working ?!?!?!
    if (skip < 1)
    {
        skip++;
        return count;
    }

    //SendTo RTP packetizer instead of writing to file
    fwrite(ptr, count, 1, checker);

    return count ;
} /* vfwrite */

static sf_count_t vftell (void *user_data)
{
    VIO_DATA *vf = (VIO_DATA *) user_data ;

    return vf->offset ;
} /* vftell */


int main()
{
    SF_INFO writing_sfinfo;
    writing_sfinfo.channels = 1;
    writing_sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_GSM610;
    writing_sfinfo.samplerate = 8000;
    assert(true == sf_format_check(&writing_sfinfo));

    SF_INFO reading_sfinfo;
    memset(&reading_sfinfo, 0, sizeof(reading_sfinfo));

    SNDFILE *input = sf_open("/home/georgi/Downloads/thank_you_60.PCMA", SFM_READ, &reading_sfinfo);
    checker = fopen("/home/georgi/Downloads/checker.wav", "w+");

    short file_data[reading_sfinfo.channels * 160];
    int read_frames = 0;

    SF_VIRTUAL_IO vio ;

    // Set up pointers to the locally defined functions.
    vio.get_filelen = vfget_filelen ;
    vio.seek = vfseek ;
    vio.read = vfread ;
    vio.write = vfwrite ;
    vio.tell = vftell ;

    VIO_DATA vio_data ;
    // Set virtual file offset and length to zero.
    vio_data.offset = 0 ;
    vio_data.length = 0 ;


    SNDFILE *virt_file = sf_open_virtual (&vio, SFM_WRITE, &writing_sfinfo, &vio_data);

    int old_length = 0;
    while ((read_frames = sf_readf_short(input, file_data, 160)))
    {
        sf_writef_short(virt_file, file_data, read_frames);
    }

    sf_close(virt_file);
    sf_close(input);
    fclose(checker);

    return 0;
}

暫無
暫無

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

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