forked from xaxaxa/libytpmv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplecache.C
More file actions
48 lines (44 loc) · 1.45 KB
/
Copy pathsamplecache.C
File metadata and controls
48 lines (44 loc) · 1.45 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
#include <ytpmv/samplecache.H>
#include <soundtouch/SoundTouch.h>
namespace ytpmv {
SampleCache::SampleCache() {}
int32_t SampleCache::calculatePitch(double pitch) {
// look in cache first
auto it = pitchCache.find(pitch);
if(it != pitchCache.end()) return (*it).second;
// calculate and return semitones * pitchPrecision, rounded to integer
int32_t res = int32_t(round(log2(pitch)*12.*double(pitchPrecision)));
pitchCache[pitch] = res;
return res;
}
basic_string<float>& SampleCache::getPitchShiftedSample(const float* sampleData, int sampleLen, double pitch) {
// look in cache first
Key key = {sampleData, calculatePitch(pitch)};
auto it = entries.find(key);
if(it != entries.end()) return (*it).second;
// perform pitch shifting
soundtouch::SoundTouch st;
st.setChannels(CHANNELS);
st.setSampleRate(44100);
st.setPitch(pitch);
st.putSamples(sampleData,sampleLen/CHANNELS);
// make sure we reference the string object that is actually in the map structure
basic_string<float>& ret = entries[key];
while(1) {
int bs = 4096;
int pos = (int)ret.size();
ret.resize(pos+bs*CHANNELS);
int r = (int)st.receiveSamples(const_cast<float*>(ret.data()+pos), bs);
if(r < bs) {
ret.resize(pos+r*CHANNELS);
break;
}
}
return ret;
}
bool operator<(const SampleCache::Key& k1, const SampleCache::Key& k2) {
if(k1.data < k2.data) return true;
if(k1.data > k2.data) return false;
return k1.pitch < k2.pitch;
}
}