Skip to content

Instantly share code, notes, and snippets.

@GRGSIBERIA
Created May 9, 2017 13:18
Show Gist options
  • Save GRGSIBERIA/f80ec16c2b34e6982d1d6bce1058b83e to your computer and use it in GitHub Desktop.
Save GRGSIBERIA/f80ec16c2b34e6982d1d6bce1058b83e to your computer and use it in GitHub Desktop.
intの配列からモノラルのwavを作る
#pragma once
#include <vector>
#include <iostream>
#include <fstream>
namespace wav
{
struct Header
{
char riff[4] = { 'R', 'I', 'F', 'F' };
unsigned long fileSize; // 36足す
char wave[4] = { 'W', 'A', 'V', 'E' };
char format[4] = { 'f', 'm', 't', ' ' };
unsigned long formatSize = 16;
unsigned short formatCode = 1; // 16bit pcm
unsigned short numChannels = 1; // mono
unsigned long samplingRate = 44100;
unsigned long bytePerSec = 44100 * 2;
unsigned short blockSize = 2;
unsigned short bitsPerSample = 16;
char data[4] = { 'd', 'a', 't', 'a' };
unsigned long dataSize;
};
class Wave
{
public:
Wave() {}
void SaveMono(const std::wstring& path, const std::vector<int>& stream)
{
Header header;
header.dataSize = stream.size() * sizeof(int);
header.fileSize = header.dataSize + 36;
std::ofstream ofst;
ofst.open(path, std::ios::out | std::ios::binary);
ofst.write((char*)&header, 44);
for (int i = 0; i < stream.size(); ++i)
{
short d = (short)(stream[i] >> 8);
ofst.write((char*)&d, 2);
}
ofst.close();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment