sound.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "sound.h"
  2. #include <stdio.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. //#include "driver/gpio.h"
  6. #include "esp_log.h"
  7. #include "sdkconfig.h"
  8. #include "driver/dac.h"
  9. #include "math.h"
  10. #define DAC_CHANNEL 1
  11. #define DAC_PIN GPIO_NUM_25 // GPIO pin for DAC output GPIO25=Channel 1
  12. #define TONE_FREQ 262 // Frequency for C4 (Middle C)
  13. #define PWM_CHANNEL LEDC_CHANNEL_0
  14. #define PWM_TIMER LEDC_TIMER_0
  15. #define PWM_RES LEDC_TIMER_8_BIT // 8-bit resolution (0-255)
  16. typedef struct {
  17. int frequency; // Frequency in Hz
  18. int duration; // Duration in ms
  19. int quietDuration; // The quiet duration after each note
  20. } Note;
  21. // Super Mario Bros. short intro melody
  22. /*Note melody[] = {
  23. {660, 100}, {0, 150}, {660, 100}, {0, 300}, {660, 100}, {0, 300}, {510, 100}, {660, 100}, {770, 100}, {0, 550},
  24. {380, 100}, {0, 500}, {510, 100}, {0, 300}, {380, 100}, {0, 300}, {320, 100}, {0, 700} // End of melody
  25. };*/
  26. /*Note melody[] = {
  27. {440, 500}, {440, 500}, {440, 500}, {349, 350}, {523, 150}, {440, 500}, {349, 350}, {523, 150}, {440, 1000},
  28. {659, 500}, {659, 500}, {659, 500}, {698, 350}, {523, 150}, {415, 500}, {349, 350}, {523, 150}, {440, 1000},
  29. };*/
  30. /*Note melody[] = {
  31. {1318, 150}, {2637, 150}, {2093, 150}, {2349, 150}, {3136, 300}
  32. };*/
  33. /*Note melody[] = {
  34. {320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},
  35. {660, 100, 150}, {660, 100, 300}, {660, 100, 300}, {510, 100, 100}, {660, 100, 300}, {770, 100, 550}, {380, 100, 575},
  36. {510, 100, 450}, {380, 100, 400}, {320, 100, 500}, {440, 100, 300}, {480, 80, 330}, {450, 100, 150}, {430, 100, 300},
  37. {380, 100, 200}, {660, 80, 200}, {760, 50, 150}, {860, 100, 300}, {700, 80, 150}, {760, 50, 350}, {660, 80, 300},
  38. {520, 80, 150}, {580, 80, 150}, {480, 80, 500},
  39. {510, 100, 450}, {380, 100, 400}, {320, 100, 500}, {440, 100, 300}, {480, 80, 330}, {450, 100, 150}, {430, 100, 300},
  40. {380, 100, 200}, {660, 80, 200}, {760, 50, 150}, {860, 100, 300}, {700, 80, 150}, {760, 50, 350}, {660, 80, 300},
  41. {520, 80, 150}, {580, 80, 150}, {480, 80, 500},
  42. {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150},
  43. {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 220}, {500, 100, 300}, {760, 100, 100},
  44. {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 200, 300},
  45. {1020, 80, 300}, {1020, 80, 150}, {1020, 80, 300}, {380, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150},
  46. {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150},
  47. {500, 100, 100}, {570, 100, 420}, {585, 100, 450}, {550, 100, 420}, {500, 100, 360}, {380, 100, 300}, {500, 100, 300},
  48. {500, 100, 150}, {500, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300},
  49. {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 220},
  50. {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 200, 300},
  51. {1020, 80, 300}, {1020, 80, 150}, {1020, 80, 300}, {380, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150},
  52. {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150},
  53. {500, 100, 100}, {570, 100, 420}, {585, 100, 450}, {550, 100, 420}, {500, 100, 360}, {380, 100, 300}, {500, 100, 300},
  54. {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 350}, {660, 80, 150}, {500, 80, 300},
  55. {430, 80, 150}, {380, 80, 600}, {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 150},
  56. {660, 80, 550}, {870, 80, 325}, {760, 80, 600},
  57. {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 350}, {660, 80, 150}, {500, 80, 300},
  58. {430, 80, 150}, {380, 80, 600}, {660, 100, 150}, {660, 100, 300}, {660, 100, 300}, {510, 100, 100}, {660, 100, 300},
  59. {770, 100, 550}, {380, 100, 575}
  60. };*/
  61. const Note melody[] = {
  62. {196.00, 152, 0},
  63. {261.63, 152, 39},
  64. {329.63, 151, 160},
  65. {392.00, 152, 201},
  66. {523.25, 152, 318},
  67. {659.26, 151, 361},
  68. {783.99, 456, 480},
  69. {659.26, 456, 536},
  70. {207.65, 152, 960},
  71. {261.63, 152, 999},
  72. {311.13, 151, 1120},
  73. {415.30, 152, 1161},
  74. {523.25, 152, 1278},
  75. {622.25, 151, 1321},
  76. {830.61, 456, 1440},
  77. {622.25, 456, 1496},
  78. {233.08, 152, 1920},
  79. {293.66, 152, 1959},
  80. {349.23, 151, 2080},
  81. {466.16, 152, 2121},
  82. {587.33, 152, 2238},
  83. {698.46, 151, 2281},
  84. {932.33, 456, 2400},
  85. {932.33, 152, 2456},
  86. {932.33, 152, 2863},
  87. {932.33, 151, 2616},
  88. {659.26, 456, 3025},
  89. };
  90. int melody_size = sizeof(melody) / sizeof(melody[0]);
  91. static void play_tone(int frequency, float duration, int quietDuration) {
  92. int semitones = 2;
  93. int t_freq = frequency * pow(2.0, semitones / 12.0);
  94. int8_t cw_offset = 0;
  95. dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, t_freq, cw_offset};
  96. if (frequency == 0) {
  97. dac_output_disable(DAC_CHANNEL_1);
  98. } else {
  99. dac_cw_generator_config(&cosineConf);
  100. dac_output_enable(DAC_CHANNEL_1);
  101. }
  102. vTaskDelay(pdMS_TO_TICKS(duration));
  103. //dac_output_disable(DAC_CHANNEL_1);
  104. //vTaskDelay(pdMS_TO_TICKS(quietDuration));
  105. }
  106. void play_melody() {
  107. for (int i = 0; i < melody_size; i++) {
  108. play_tone(melody[i].frequency, melody[i].duration, melody[i].quietDuration);
  109. }
  110. dac_output_disable(DAC_CHANNEL_1);
  111. }
  112. static void setupSound() {
  113. int8_t cw_offset = 0;
  114. dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, 440, cw_offset};
  115. dac_cw_generator_config(&cosineConf);
  116. dac_cw_generator_enable();
  117. //dac_output_enable(DAC_CHANNEL_1);
  118. //vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1 second
  119. dac_output_disable(DAC_CHANNEL_1);
  120. ESP_LOGI("SOUND", "Sound setup");
  121. }
  122. static void sound_task(void *pvParameters) {
  123. setupSound();
  124. while (1) {
  125. vTaskDelay(pdMS_TO_TICKS(5000));
  126. play_melody();
  127. }
  128. ESP_LOGI("SOUND", "Sound task starts....");
  129. }
  130. void initSound() {
  131. xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL);
  132. ESP_LOGI("SOUND", "Sound initialized");
  133. }