Created
May 9, 2017 13:18
-
-
Save GRGSIBERIA/f80ec16c2b34e6982d1d6bce1058b83e to your computer and use it in GitHub Desktop.
intの配列からモノラルのwavを作る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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