Skip to content

Commit

Permalink
Fixed rate change for multiple channel audio
Browse files Browse the repository at this point in the history
  • Loading branch information
waywardgeek committed May 5, 2017
1 parent 6d65569 commit 7b44193
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ private static void runSonic(
public static void main(
String[] argv) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
float speed = 2.0f;
float speed = 1.0f;
float pitch = 1.0f;
float rate = 1.0f;
float rate = 1.5f;
float volume = 1.0f;
boolean emulateChordPitch = false;
int quality = 0;

AudioInputStream stream = AudioSystem.getAudioInputStream(new File("talking.wav"));
AudioInputStream stream = AudioSystem.getAudioInputStream(new File("stereo_test.wav"));
AudioFormat format = stream.getFormat();
int sampleRate = (int)format.getSampleRate();
int numChannels = format.getChannels();
Expand Down
10 changes: 5 additions & 5 deletions Sonic.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,12 +736,12 @@ private void adjustPitch(
// Interpolate the new output sample.
private short interpolate(
short in[],
int inPos,
int inPos, // Index to first sample which already includes channel offset.
int oldSampleRate,
int newSampleRate)
{
short left = in[inPos*numChannels];
short right = in[inPos*numChannels + numChannels];
short left = in[inPos];
short right = in[inPos + numChannels];
int position = newRatePosition*oldSampleRate;
int leftPosition = oldRatePosition*newSampleRate;
int rightPosition = (oldRatePosition + 1)*newSampleRate;
Expand Down Expand Up @@ -774,8 +774,8 @@ private void adjustRate(
while((oldRatePosition + 1)*newSampleRate > newRatePosition*oldSampleRate) {
enlargeOutputBufferIfNeeded(1);
for(int i = 0; i < numChannels; i++) {
outputBuffer[numOutputSamples*numChannels + i] = interpolate(pitchBuffer, position + i,
oldSampleRate, newSampleRate);
outputBuffer[numOutputSamples*numChannels + i] = interpolate(pitchBuffer,
position*numChannels + i, oldSampleRate, newSampleRate);
}
newRatePosition++;
numOutputSamples++;
Expand Down
27 changes: 21 additions & 6 deletions funcgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
#define M_PI 3.14159265358979323846
#endif

#define SAMPLE_RATE 11025
#define NUM_SAMPLES 11025
#define NOISE 15
#define SAMPLE_RATE 22050
#define NUM_SAMPLES 22050
#define NOISE 0

/* Fundamental */
#define MID_FREQ 150
#define FREQ_CHANGE 50
#define MAX_FREQ 2000.0

/* Harmonics */
#define NUM_HARMONICS 5
static double harmonic[NUM_HARMONICS] = {4000.0, 4000.0, 2000.0, 0.0, 1000.0};
#define NUM_HARMONICS 20
static double harmonic[NUM_HARMONICS] = {1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0,
1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0};
/* The phase seems to make no difference in how it sounds to me. */
static double phase[NUM_HARMONICS] = {0.0, 1.0, 2.0, 5.5, 4.1234, 2.8, 6.0, 4.5, 2.67, 0.543,
0.01, 1.234, 2.345, 3.456, 5.9, 4.8, 0.89, 1.33, 3.8, 3.14159};
/* static double phase[NUM_HARMONICS] = {0.0,}; */

int main() {
short samples[NUM_SAMPLES];
Expand All @@ -28,7 +34,16 @@ int main() {
samples[i] = 0.0;
int j;
for (j = 0; j < NUM_HARMONICS; j++) {
samples[i] += harmonic[j]*sin((j+1)*w);
double amplitude = harmonic[j];
if ((j+1)*freq > MAX_FREQ) {
if ((j)*freq >= MAX_FREQ) {
amplitude = 0.0;
} else {
double ratio = ((j+1)*freq - MAX_FREQ)/freq;
amplitude *= 1.0 - ratio;
}
}
samples[i] += amplitude*sin((j+1)*w + phase[j]);
}
/* Add noise */
samples[i] += (float)NOISE*rand()/RAND_MAX;
Expand Down
2 changes: 1 addition & 1 deletion sonic.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ static int adjustRate(
return 0;
}
out = stream->outputBuffer + stream->numOutputSamples*numChannels;
in = stream->pitchBuffer + position;
in = stream->pitchBuffer + position*numChannels;
for(i = 0; i < numChannels; i++) {
*out++ = interpolate(stream, in, oldSampleRate, newSampleRate);
in++;
Expand Down

0 comments on commit 7b44193

Please sign in to comment.