-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioWriter.cpp
More file actions
101 lines (90 loc) · 2.98 KB
/
AudioWriter.cpp
File metadata and controls
101 lines (90 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Created by gordonleem on 5/8/17.
//
#include <iomanip>
#include "AudioWriter.h"
void SoxDatWriter::writeHeader(const AudioGenerator &audio, unsigned int sampleRate)
{
getFile() << "; File: " << getFilename() << std::endl;
getFile() << "; Sample Rate " << sampleRate << std::endl;
getFile() << "; Channels 1" << std::endl;
}
void SoxDatWriter::writeData(const AudioGenerator &audio, unsigned int sampleRate)
{
getFile() << std::fixed;
getFile() << std::setprecision(15); // 150db of dynamic range should exceed
double offset(1.0/double(sampleRate));
for(double i(0); i < audio.getDuration(); i += offset)
{
getFile() << i << ' ' << audio[i] << std::endl;
}
}
void WaveWriter::WriteFourCC(const std::string &str)
{
for(int i(0); i<4; i++)
{
uint8_t c(i < str.length() ? str[i] : 0);
WriteInt8(c);
}
}
void WaveWriter::WriteInt16(uint16_t val)
{
WriteInt8(val & 0xFF);
WriteInt8(val >> 8);
}
void WaveWriter::WriteInt32(uint32_t val)
{
WriteInt16(val & 0xFFFF);
WriteInt16(val >> 16);
}
void WaveWriter::WriteFloat32(float val)
{
// Evil, evil stuff
// Assumptions:
// * that both floating point and integer number representations have the same 'endianess'
// * that 'float' is an IEEE 754 32 bit floating point number
uint32_t *ptr = reinterpret_cast<uint32_t *>(&val);
WriteInt32(*ptr);
}
void WaveWriter::WriteFloat32(double val)
{
WriteFloat32(static_cast<float>(val));
}
void WaveWriter::writeHeader(const AudioGenerator &audio, unsigned int sampleRate)
{
uint16_t sampleSize(m_pcm ? 2 : 4);
uint32_t samples(GetSampleCount(audio, sampleRate));
// Assumes mono signal
WriteFourCC("RIFF"); // Magic number
WriteInt32(samples * sampleSize + 40); // File size
WriteFourCC("WAVE"); // File type
WriteFourCC("fmt "); // fmt chunk
WriteInt32(16); // Format header size
WriteInt16(m_pcm ? 1 : 3); // Audio format (1 = PCM, 3 = 32 bit float)
WriteInt16(1); // Channels
WriteInt32(sampleRate); // Sample Rate
WriteInt32(sampleRate * sampleSize * 8); // Bit rate
WriteInt16(sampleSize); // Sample size
WriteInt16(sampleSize * 8); // Bits per sample
WriteFourCC("data"); // Data header
WriteInt32(samples * sampleSize); // Data size
}
void WaveWriter::writeData(const AudioGenerator &audio, unsigned int sampleRate)
{
uint32_t samples(GetSampleCount(audio, sampleRate));
double offset(1.0/double(sampleRate));
for(uint32_t i(0); i < samples; i ++)
{
double sample(audio[i * offset]);
if(m_pcm)
{
uint16_t val(65535);
val *= (sample + 1) / 2;
WriteInt16(val);
}
else
{
WriteFloat32(sample);
}
}
}