使用C ++创建声音文件

时间:2019-05-16 10:44:03

标签: c++ mp3 wav

我正在尝试使用c ++制作具有不同和弦的音频文件(.wav)。我的问题是我不知道如何同时制作多个声音,也不知道如何根据不同频率的音来创建声音文件。

我发现有些人向人们展示可以在其中执行此操作的代码,但是我不知道该如何使用它,并且当我将其复制粘贴时,它对我不起作用。如果有人可以帮助我,我将不胜感激。

现在,我正在使用在同一页面的人的帮助下编写的这段代码:

#define NOMINMAX
#include <windows.h>
#include <iostream>
#include <cmath>
#include <limits>
#include <fstream>
#include <string>

/**Aquest subprograma l'hem extret de la web, l'hem adaptat a les nostres necessitats, però no l'hem creat des de 0!!**/

typedef signed short BitDepth;//    16bit audio
typedef unsigned long long QWORD;

/*constants necessàries per generar una ona periòdica*/
const double pi = 3.141592653589;
const DWORD samplerate              =   44100;
const WORD channels                 =   2;
const unsigned short SOUND_DURATION =   1;//    1 second for example.
const QWORD NUM_SAMPLES             =   SOUND_DURATION * samplerate * channels;


//es crea l'ona sinusoïdal
void sineWave(BitDepth buffer[], double freq) {
    BitDepth amplitude = std::numeric_limits<BitDepth>::max()*0.5;
    QWORD c=0;
    double d=(samplerate/freq);
    for(QWORD i=0; i<NUM_SAMPLES; i+=channels) {
        double deg=360.0/d;
        buffer[i] = buffer[i+(1*(channels-1))] = sin((c++*deg)*pi/180)*amplitude;
    }
}

//les dades es passen a format .wav

template<typename _Ty> void write(std::ofstream& stream, const _Ty& ty) {
    stream.write((const char*)&ty, sizeof(_Ty));
}

void writeWaveFile(const char* filename, BitDepth* buffer) {
    std::ofstream stream(filename, std::ios::binary);

    stream.write("RIFF", 4);
    ::write<int>(stream, 36+(NUM_SAMPLES*sizeof(BitDepth)));
    stream.write("WAVEfmt ", 8);
    ::write<int>(stream, 16);
    ::write<short>(stream, 1);
    ::write<unsigned short>(stream, channels);
    ::write<int>(stream, samplerate);
    ::write<int>(stream, samplerate*channels*sizeof(BitDepth));
    ::write<short>(stream, channels*sizeof(BitDepth));
    ::write<short>(stream, sizeof(BitDepth)*8);
    stream.write("data", 4);
    ::write<int>(stream, NUM_SAMPLES*sizeof(BitDepth));
    stream.write((const char*)&buffer[0], NUM_SAMPLES*sizeof(BitDepth));
    stream.close();
}

int main(int argc, char** argv) {
    SetConsoleTitleA("PCM Audio Example");

    std::string filename = "Roda_acords";

    BitDepth* grau1 = new BitDepth[NUM_SAMPLES];
    BitDepth* grau2 = new BitDepth[NUM_SAMPLES];
    BitDepth* grau3 = new BitDepth[NUM_SAMPLES];
    BitDepth* grau4 = new BitDepth[NUM_SAMPLES];

    BitDepth* acord = new BitDepth[NUM_SAMPLES];

    memset(grau1, 0, NUM_SAMPLES*sizeof(BitDepth));
    memset(grau2, 0, NUM_SAMPLES*sizeof(BitDepth));
    memset(grau3, 0, NUM_SAMPLES*sizeof(BitDepth));
    memset(grau4, 0, NUM_SAMPLES*sizeof(BitDepth));

    sineWave(grau1, 440.0);
    sineWave(grau2, 523.251);
    sineWave(grau3, 659.255);
    sineWave(grau4, 789.991 );


    writeWaveFile(std::string(filename+std::string(".wav")).c_str(), (grau1));
    delete[] grau1;

    std::cout << filename << ".wav written!" << std::endl;
    std::cin.get();
    return 0;
}
``````````

0 个答案:

没有答案