NoteFrequency.ino 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Detect the frequency of music notes, by Colin Duffy
  2. This example repeatedly plays a guitar note (output to the DAC pin)
  3. and prints an analysis of the frequency to the Arduino Serial Monitor
  4. https://forum.pjrc.com/threads/32252-Different-Range-FFT-Algorithm/page2
  5. https://github.com/duff2013/AudioTuner
  6. */
  7. /*
  8. C C# D Eb E F F# G G# A Bb B
  9. 0 16.35 17.32 18.35 19.45 20.60 21.83 23.12 24.50 25.96 27.50 29.14 30.87
  10. 1 32.70 34.65 36.71 38.89 41.20 43.65 46.25 49.00 51.91 55.00 58.27 61.74
  11. 2 65.41 69.30 73.42 77.78 82.41 87.31 92.50 98.00 103.8 110.0 116.5 123.5
  12. 3 130.8 138.6 146.8 155.6 164.8 174.6 185.0 196.0 207.7 220.0 233.1 246.9
  13. 4 261.6 277.2 293.7 311.1 329.6 349.2 370.0 392.0 415.3 440.0 466.2 493.9
  14. 5 523.3 554.4 587.3 622.3 659.3 698.5 740.0 784.0 830.6 880.0 932.3 987.8
  15. 6 1047 1109 1175 1245 1319 1397 1480 1568 1661 1760 1865 1976
  16. 7 2093 2217 2349 2489 2637 2794 2960 3136 3322 3520 3729 3951
  17. 8 4186 4435 4699 4978 5274 5588 5920 6272 6645 7040 7459 7902
  18. Guitar strings are E2=82.41Hz, A2=110Hz, D3=146.8Hz, G3=196Hz, B3=246.9Hz, E4=329.6Hz
  19. Bass strings are (5th string) B0=30.87Hz, (4th string) E1=41.20Hz, A1=55Hz, D2=73.42Hz, G2=98Hz
  20. This example tests the yin algorithm with actual notes from nylon string guitar recorded
  21. as wav format at 16B @ 44100 samples/sec. Since the decay of the notes will be longer than what
  22. the teensy can store in flash these notes are truncated to ~120,000B or about 1/2 of the whole
  23. signal.
  24. */
  25. #include <SerialFlash.h>
  26. #include <Audio.h>
  27. #include <Wire.h>
  28. #include <SPI.h>
  29. #include <SD.h>
  30. //---------------------------------------------------------------------------------------
  31. #include "guitar_e2_note.h"
  32. #include "guitar_a2_note.h"
  33. #include "guitar_d3_note.h"
  34. #include "guitar_g3_note.h"
  35. #include "guitar_b3_note.h"
  36. #include "guitar_e4_note.h"
  37. #include "tuba_1.h"
  38. #include "tuba_2.h"
  39. #include "tuba_3.h"
  40. #include "tuba_4.h"
  41. #include "tuba_5.h"
  42. //---------------------------------------------------------------------------------------
  43. AudioAnalyzeNoteFrequency notefreq;
  44. AudioOutputAnalog dac;
  45. AudioPlayMemory wav_note;
  46. AudioMixer4 mixer;
  47. //---------------------------------------------------------------------------------------
  48. AudioConnection patchCord0(wav_note, 0, mixer, 0);
  49. AudioConnection patchCord1(mixer, 0, notefreq, 0);
  50. AudioConnection patchCord2(mixer, 0, dac, 0);
  51. //---------------------------------------------------------------------------------------
  52. IntervalTimer playNoteTimer;
  53. void playNote(void) {
  54. if (!wav_note.isPlaying()) {
  55. // Uncomment one of these sounds to test notefreq
  56. wav_note.play(guitar_e2_note);
  57. //wav_note.play(guitar_a2_note);
  58. //wav_note.play(guitar_d3_note);
  59. //wav_note.play(guitar_g3_note);
  60. //wav_note.play(guitar_b3_note);
  61. //wav_note.play(guitar_e4_note);
  62. //wav_note.play(tuba_1);
  63. //wav_note.play(tuba_2);
  64. //wav_note.play(tuba_3);
  65. //wav_note.play(tuba_4);
  66. //wav_note.play(tuba_5);
  67. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  68. }
  69. }
  70. //---------------------------------------------------------------------------------------
  71. void setup() {
  72. AudioMemory(30);
  73. /*
  74. * Initialize the yin algorithm's absolute
  75. * threshold, this is good number.
  76. */
  77. notefreq.begin(.15);
  78. pinMode(LED_BUILTIN, OUTPUT);
  79. // Audio library isr allways gets priority
  80. playNoteTimer.priority(144);
  81. playNoteTimer.begin(playNote, 1000);
  82. }
  83. void loop() {
  84. // read back fundamental frequency
  85. if (notefreq.available()) {
  86. float note = notefreq.read();
  87. float prob = notefreq.probability();
  88. Serial.printf("Note: %3.2f | Probability: %.2f\n", note, prob);
  89. }
  90. }