-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioClip.cpp
123 lines (107 loc) · 3.74 KB
/
AudioClip.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
MIT License
Copyright(c) 2020 Stephan Unverwerth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this softwareand associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright noticeand this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "AudioClip.h"
#include "sys.h"
#include <fstream>
#include <cstdint>
AudioClip::AudioClip(const char* filename, int maxRef): AudioSource(maxRef) {
std::ifstream file(filename, std::ios::binary);
if (!file.good()) {
log_error("Can not open audio clip %s.", filename);
return;
}
uint32_t dw;
file.read((char*)&dw, 4); // 'RIFF'
file.read((char*)&dw, 4); // FILESIZE - 8
file.read((char*)&dw, 4); // 'WAVE'
file.read((char*)&dw, 4); // 'fmt '
file.read((char*)&dw, 4); // fmt length
uint16_t fmt_tag;
file.read((char*)&fmt_tag, 2);
uint16_t channels;
file.read((char*)&channels, 2);
uint32_t sampleRate;
file.read((char*)&sampleRate, 4);
uint32_t bytesPerSec;
file.read((char*)&bytesPerSec, 4);
uint16_t blockAlign;
file.read((char*)&blockAlign, 2);
uint16_t bitsPerSample;
file.read((char*)&bitsPerSample, 2);
uint32_t dataLength;
while (file.good()) {
file.read((char*)&dw, 4); // 'data'
file.read((char*)&dataLength, 4); // data length
if (dw == 'atad') break;
file.seekg(dataLength, std::ios::cur);
}
if (!file.good()) {
log_error("Unexpected end of audio file %s.", filename);
return;
}
numChannels_ = channels;
samplesPerSecond_ = sampleRate;
numSamples_ = dataLength / numChannels_ / (bitsPerSample / 8);
samples = new float[numSamples_ * numChannels_];
float* write = samples;
for (int i = 0; i < numSamples_; i++) {
for (int c = 0; c < numChannels_; c++) {
short v;
file.read((char*)&v, 2);
*write++ = float(v) / 65536;
}
}
}
AudioClip::~AudioClip() {
delete[] samples;
}
int AudioClip::render(float* buffer, int numSamples, int nextSample, float volume, float pan, float pitch, bool loop) {
float l = (pan > 0 ? 1 - pan : 1) * volume;
float r = (pan < 0 ? 1 + pan : 1) * volume;
float playbackPointer = nextSample;
float advance = pitch * samplesPerSecond_ / 48000;
if (numChannels_ == 1) {
for (int i = 0; i < numSamples; i++) {
int sample = (int)playbackPointer;
*buffer++ += samples[sample] * l;
*buffer++ += samples[sample] * r;
playbackPointer += advance;
if (playbackPointer >= numSamples_) {
if (!loop) break;
float intpart;
playbackPointer = modf(playbackPointer, &intpart);
}
}
}
else if (numChannels_ == 2) {
for (int i = 0; i < numSamples; i++) {
int sample = (int)playbackPointer;
*buffer++ += samples[sample * 2] * l;
*buffer++ += samples[sample * 2 + 1] * r;
playbackPointer += advance;
if (playbackPointer >= numSamples_) {
if (!loop) break;
float intpart;
playbackPointer = modf(playbackPointer, &intpart);
}
}
}
return (int)playbackPointer;
}