PaulStoffregen/Audio

Incorrect notes generated in AudioSynthKarplusStrong

shaitanbaali opened this issue · 2 comments

Description

When generating notes with the AudioSynthKarplusStrong object the ones in the C1 octave are bad generated, with several frequencies the output is always the same.

Steps To Reproduce Problem

generate the code to play the following list of notes (C1 to B4) with one second of difference. then check the audio generated and the first 7 notes (C1 to B1) all them sound as E2 being C3 the first correct generated sound.

const int listaEscala[]{
// 1 c d e f g a b - 3 c d e f g a b - 4 c d e f g a b
24, 26, 28, 29, 31, 33, 35, 48, 50, 52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69, 71
};

Hardware & Software

Board: Teensy 4
Shields / modules used: AUdio SHield rev D
Arduino IDE version: 1.8.19
Teensyduino version: 1.57
Operating system & version: OSX 12.6.3
Any other software or hardware? None.

Arduino Sketch

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthKarplusStrong string1; //xy=175,220
AudioAmplifier amp1; //xy=410,229
AudioOutputI2S i2s1; //xy=695,236
AudioConnection patchCord1(string1, amp1);
AudioConnection patchCord2(amp1, 0, i2s1, 0);
AudioConnection patchCord3(amp1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=762,368
// GUItool: end automatically generated code
float midiToFreq[128] = { //MTOF implementation
8.17579, //C-1 0
8.66195,
9.17702,
9.72271,
10.3008,
10.9133,
11.5623,
12.2498,
12.9782,
13.7500,
14.5676,
15.4338,

16.3515, //C0 12
17.3239, //C#0.Db0
18.3540, //D0
19.4454, //D#.Eb0
20.6017, //E0
21.8267, //F0
23.1246, //F#0.Gb0
24.4997, //G0
25.9565, //G#0.Ab0
27.5000, //A0
29.1352, //A#0.Bb0
30.8677, //B0

32.7031, //C1 24
34.6478,
36.7080,
38.8908,
41.2034,
43.6535,
46.2493,
48.9994,
51.9130,
55.0000,
58.2704,
61.7354,

65.4063, //C2 36
69.2956,
73.4161,
77.7817,
82.4068,
87.3070,
92.4986,
97.9988,
103.826,
110.000,
116.540,
123.470,

130.812, //C3 48
138.591,
146.832,
155.563,
164.813,
174.614,
184.997,
195.997,
207.652,
220.000,
233.081,
246.941,

261.625, //C4 60
277.182,
293.664,
311.126,
329.627,
349.228,
369.994,
391.995,
415.304,
440.000,
466.163,
493.883,

523.251, //C5 72
554.365,
587.329,
622.253,
659.255,
698.456,
739.988,
783.990,
830.609,
880.000,
932.327,
987.766,

1046.50, //C6 84
1108.73,
1174.65,
1244.50,
1318.51,
1396.91,
1479.97,
1567.98,
1661.21,
1760.00,
1864.65,
1975.53,

2093.00, //C7 96
2217.46,
2349.31,
2489.01,
2637.02,
2793.82,
2959.95,
3135.96,
3322.43,
3520.00,
3729.31,
3951.06,

4186.00, //C8 108
4434.92,
4698.63,
4978.03,
5274.04,
5587.65,
5919.91,
6271.92,
6644.87,
7040.00,
7458.62,
7902.13,

8372.01, //C9 120
8869.84,
9397.27,
9956.06,
10548.0,
11175.3,
11839.8,
12543.8
};

void setup() {
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.6);
amp1.gain(0.8);
}
int count = 0;
unsigned long myTime = millis();

const int listaEscala[]{ // 3 c d e f g a b - 4 c d e f g a b
24, 26, 28, 29, 31, 33, 35, 48, 50, 52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69, 71
};

void loop() {

if (millis() - myTime >= 1000){
string1.noteOn(midiToFreq[listaEscala[count]], 1);
Serial.println(listaEscala[count]);
count = count + 1;
if (count == 21) {
count = 0;
Serial.println("reset");
}
myTime = millis();
}

}

Errors or Incorrect Output

NO compilation problems. Attached is a record of the loop in audio for testing.

STE-008.mp3.zip

If you look at the code, the buffer used to generate the audio is 536 samples long, which by my calculation corresponds to about 82Hz. So if you request a frequency below E2, you will just get E2.

You could change it, but note it's a "magic number", rather than a defined constant. I counted 3 instances which need changing, but could have missed some...

thanks!