-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPitchShiftSDWavFile.cpp
More file actions
136 lines (123 loc) · 3.15 KB
/
PitchShiftSDWavFile.cpp
File metadata and controls
136 lines (123 loc) · 3.15 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/******************************************************************************
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
/*
* PitchShiftedSDWavFile.cpp
*
* Created on: Aug 31, 2019
* Author: JakeSoft
*/
#include "PitchShiftSDWavFile.h"
PitchShiftSDWavFile::PitchShiftSDWavFile(const char* aFilePath)
: SDWavFile(aFilePath) //Call superclass constructor
{
mCurSample = 0;
mSkipFactor = 0.0;
mRepeatFactor = 0.0;
mRepeatAccumulator = 0.0;
mSkipAccumulator = 0.0;
}
PitchShiftSDWavFile::~PitchShiftSDWavFile()
{
if(nullptr != mpFileReader)
{
delete mpFileReader;
mFileHandle.close();
}
}
int PitchShiftSDWavFile::Fetch16BitSamples(int16_t* apBuffer, int aNumSamples)
{
int lSampleIndex = 0;
for(lSampleIndex = 0;
lSampleIndex < aNumSamples && !IsEnded();
lSampleIndex++)
{
CalculateCurSample();
apBuffer[lSampleIndex] = mCurSample;
}
return lSampleIndex;
}
void PitchShiftSDWavFile::CalculateCurSample()
{
//Always fetch the first sample
if(0 == mSampleIndex)
{
SDWavFile::Fetch16BitSamples(&mCurSample, 1);
}
//We want to skip samples to speed up playback
else if(mSkipFactor > 0.0)
{
if(mSkipAccumulator >= 1.0)
{
while(mSkipAccumulator >= 1.0)
{
SDWavFile::Skip16BitSamples(1);
mSkipAccumulator -= 1.0;
}
}
else
{
mSkipAccumulator += mSkipFactor;
}
SDWavFile::Fetch16BitSamples(&mCurSample, 1);
}
//We want to repeat samples to slow down playback
else if(mRepeatFactor > 0.0)
{
if(mRepeatAccumulator >= 1.0)
{
//Repeat last sample (don't update mCurSample) and adjust the accumulator
mRepeatAccumulator =- 1.0;
}
else
{
//Fetch the next sample and adjust the accumulator
mRepeatAccumulator += mRepeatFactor;
SDWavFile::Fetch16BitSamples(&mCurSample, 1);
}
}
//Play at normal speed
else
{
SDWavFile::Fetch16BitSamples(&mCurSample, 1);
}
mSampleIndex++;
}
void PitchShiftSDWavFile::SetRate(float aRate)
{
//Slow it down
if(aRate < 0.0)
{
mRepeatFactor = -1.0 * aRate; //Flips negative value to positive
//Turn off the stuff that would speed up playback
mSkipFactor = 0.0;
mSkipAccumulator = 0.0;
}
//Speed it up
else if(aRate > 0.0)
{
mSkipFactor = aRate;
//Turn off the stuff that would slow down playback
mRepeatFactor = 0.0;
mRepeatAccumulator = 0.0;
}
//Play at normal speed
else
{
mRepeatFactor = 0.0;
mRepeatAccumulator = 0.0;
mSkipFactor = 0.0;
mSkipAccumulator = 0.0;
}
}