wave文件导出已损坏

时间:2014-08-01 17:57:24

标签: c++ format export wave

我编写了两个函数,它们应该将音频浮点缓冲区导出到.wav文件中,但是我在播放导出的文件时遇到了问题。 Audacity像它应该的那样播放它(听起来和我的应用程序完全一样),然而,Ableton(DAW软件)似乎误解了wav的某些部分,所以它听起来真的是扭曲的。 (就像失真效应一样)

我认为,能够以某种方式假设样本深度(较小),因此实际样本会超出极限。

我有两个函数,一个从两个浮点缓冲区创建一个int32_t缓冲区(左右混合到一个缓冲区中),另一个函数写入.wav文件,包括格式块等.I猜猜哪个地方有问题。

班级成员/结构

// static I use in the export function
static const int FORMAT_PCM = 1;
static const int CHANNEL_COUNT = 2;    // fix stereo
static const int BYTES_PER_SAMPLE = 4; // fix bytes per sample, 32bit audio

// a function I found in the internet, helps writting the bytes to the file
template <typename T>
static void write(std::ofstream& stream, const T& t) {
    stream.write((const char*)&t, sizeof(T));
};

// used "structure" to store the buffer
class StereoAudioBuffer {
public:
    StereoAudioBuffer(int length) : sizeInSamples(2*length){
        samples = new int32_t[2*length];
    };
    ~StereoAudioBuffer() {delete samples;};

    int32_t *samples;
    const int sizeInSamples;
};

转换功能

StereoAudioBuffer* WaveExport::convertTo32BitStereo(
        float *leftSamples, 
        float*rightSamples, 
        int length) 
{
    StereoAudioBuffer *buffer = new StereoAudioBuffer(length);
    float max = 0;

    // find max sample 
    for(int i = 0; i < length; i++) {
        if(abs(leftSamples[i]) > max) {
            max = abs(leftSamples[i]);
        }
        if(abs(rightSamples[i]) > max) {
            max = abs(rightSamples[i]);
        }
    }



    // normalise and scale to size(int32_t)

    float factor = 2147483000.0f / max;

    for(int i = 0; i < length; i++) {
        buffer->samples[2*i] = leftSamples[i] * factor ;
        buffer->samples[2*i+1] = rightSamples[i] * factor;
    }
    return buffer;
}

导出功能(此代码的一部分来自互联网,遗憾的是,我再也找不到来源了

void WaveExport::writeStereoWave(
        const char *path, 
        StereoAudioBuffer* buffer, 
        int sampleRate) 
{
    std::ofstream stream(path, std::ios::binary);

    // RIFF
    stream.write("RIFF", 4);

    // FILE SIZE
    write<int>(stream, 36 + buffer->sizeInSamples * BYTES_PER_SAMPLE); // 32 bits -> 4 bytes

    // WAVE
    stream.write("WAVE", 4);

    // FORMAT CHUNK
    stream.write("fmt ", 4);

    write<int>(stream, 16);

    write<short>(stream, FORMAT_PCM);                                       // Format
    write<short>(stream, CHANNEL_COUNT);                                    // Channels
    write<int>(stream, sampleRate);                                         // Sample Rate
    write<int>(stream, sampleRate * CHANNEL_COUNT * BYTES_PER_SAMPLE);      // Byterate
    write<short>(stream, CHANNEL_COUNT * BYTES_PER_SAMPLE);                 // Frame size
    write<short>(stream, 8 * BYTES_PER_SAMPLE);                             // Bits per sample

    int dataChunkSize = buffer->sizeInSamples * BYTES_PER_SAMPLE;
    // SAMPLES
    stream.write("data", 4);
    stream.write((const char*)&dataChunkSize, 4);
    stream.write((const char*)buffer->samples, BYTES_PER_SAMPLE*buffer->sizeInSamples);
}

有人知道怎么写.wav文件,也许可以告诉我我做错了什么或错过了什么? 谢谢!

1 个答案:

答案 0 :(得分:0)

没有问题。我使用的是32位.wav,在应用程序中不支持,我用于播放。

我将导出函数更改为使用int16_t,16位深度,并且工作正常。