C ++音频文件转换器

时间:2014-09-07 06:32:39

标签: c++ audio encode

我有一个G711编解码器,我想转换一个wav文件并保存。我读取了sample.wav文件,并使用G711 ITU-T编解码器进行转换,并将编码数据保存到output.wav文件中。 这是我的代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "G711.h"
#include <vector>
using namespace std;

// WAVE PCM soundfile format (you can find more in https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ )
typedef struct header_file
{
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;            // sample_rate denotes the sampling rate.
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    char subchunk2_id[4];
    int subchunk2_size;         // subchunk2_size denotes the number of samples.
} header;

typedef struct header_file* header_p;




int main()

{
    FILE * infile = fopen("sample.wav","rb");       // Open wave file in read mode
    FILE * outfile = fopen("Output.wav","wb");      // Create output ( wave format) file in write mode

    int BUFSIZE = 512;                  // BUFSIZE can be changed according to the frame size required (eg:512)
    int count = 0;                      // For counting number of frames in wave file.
    short int buff16[BUFSIZE];              // short int used for 16 bit as input data format is 16 bit PCM audio
    header_p meta = (header_p)malloc(sizeof(header));   // header_p points to a header struct that contains the wave file metadata fields
    int nb;                         // variable storing number of byes returned

    if (infile)
    {
        fread(meta, 1, sizeof(header), infile);
        fwrite(meta,1, sizeof(*meta), outfile);
        cout << " Size of Header file is "<<sizeof(*meta)<<" bytes" << endl;
        cout << " Sampling rate of the input wave file is "<< meta->sample_rate <<" Hz" << endl;
        cout << " Number of samples in wave file are " << meta->subchunk2_size << " samples" << endl;
        cout << " ID: " << meta->chunk_id << endl;
cout << "chunk_size" << meta->chunk_size << endl;
cout << "format" << meta->format << endl;
cout << "subchunk1"<<meta->subchunk1_id << endl;
cout << "subchunk1_size"<<meta->subchunk1_size << endl;
cout << "audio_format"<<meta->audio_format<<endl;
cout << "num_channels"<<meta->num_channels<<endl;
cout << "byte_rate"<<meta->byte_rate<<endl;
cout << "block_align"<<meta->block_align<<endl;
cout << "bits_per_sample"<<meta->bits_per_sample<<endl;
cout << "subchunk2_id"<<meta->subchunk2_id<<endl;


        while (!feof(infile))
        {
            nb = fread(buff16,1,BUFSIZE,infile); // Reading data in chunks of BUFSIZE

            size_t ds = sizeof(buff16) >> 1;
            cout << "ds: " << ds <<endl;
            vector<short> vec_src(sizeof(buff16), 0);
            vector<unsigned char> vec_dst(ds, 0);
            for (int i = 0; i < sizeof(buff16); i++) {
                    vec_src[i] = buff16[i];
            }
            short* src = &vec_src[0];
            uint8_t* dst = &vec_dst[0];
            G711::ALawEncode(dst, buff16, sizeof(buff16));
            unsigned char arr[ds];
            for (unsigned i = 0; i < ds; i++) {
                    arr[i] = *(dst + i);
            }

count++;                    // Incrementing Number of frames




            /* Insert your processing code here*/
            fwrite(arr,sizeof(unsigned char),sizeof(arr),outfile);          // Writing read data into output file
        }
    cout << " Number of frames in the input wave file are " <<count << endl;
    }
    getchar();
return 0;
}

我不太了解C ++,所以请帮助我。如果我运行此代码我得到Output.wav文件,但我不能玩。怎么了?

0 个答案:

没有答案