Programar archivo wav en cpp
Enviado por Emiliano León • 29 de Noviembre de 2022 • Tarea • 1.524 Palabras (7 Páginas) • 43 Visitas
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
// Riff chunk
const string chunkID = "RIFF";
const string chunkSize = "----";
const string format = "WAVE";
// FMT chunk
const string subchunk1ID = "fmt ";
const int subchunk1Size = 16;
const int audioFormat = 1;
const int numChannels = 1;
const int sampleRate = 44100;
const int byteRate = sampleRate * numChannels * (subchunk1Size/2);
const int blockAlign = numChannels * (subchunk1Size/8);
const int bitsPerSample = 16;
// Data sub-chunk
const string subchunk2ID= "data";
const string subchunk2Size = "----";
const int duration = 30;
const int amplitudMax = 32760;
const double sol = 391.995;
const double la = 440;
const double si = 493.883;
const double dO = 523.251;
const double re = 587.330;
const double mi = 659.255;
const double fa_sost = 739.989;
const double sol2 = 783.991;
void escribir_en_bytes(ofstream &file, int value, int byteSize){
file.write(reinterpret_cast<const char*>(&value), byteSize);
}
int main(){
ofstream wav;
wav.open("test.wav", ios::binary);
if(wav.is_open()){
wav << chunkID;
wav << chunkSize;
wav << format;
wav << subchunk1ID;
escribir_en_bytes(wav, subchunk1Size, 4);
escribir_en_bytes(wav, audioFormat, 2);
escribir_en_bytes(wav, numChannels, 2);
escribir_en_bytes(wav, sampleRate, 4);
escribir_en_bytes(wav, byteRate, 4);
escribir_en_bytes(wav, blockAlign, 2);
escribir_en_bytes(wav, bitsPerSample, 2);
wav << subchunk2ID;
wav << subchunk2Size;
int iniciar_audio = wav.tellp();
for(int i=0; i < 2; i++){
for(int j=0; j < sampleRate * (duration/2); j++){
...