#include "sound.h" #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" //#include "driver/gpio.h" #include "esp_log.h" #include "sdkconfig.h" #include "driver/dac.h" #include "math.h" #define DAC_CHANNEL 1 #define DAC_PIN GPIO_NUM_25 // GPIO pin for DAC output GPIO25=Channel 1 #define TONE_FREQ 262 // Frequency for C4 (Middle C) #define PWM_CHANNEL LEDC_CHANNEL_0 #define PWM_TIMER LEDC_TIMER_0 #define PWM_RES LEDC_TIMER_8_BIT // 8-bit resolution (0-255) typedef struct { int frequency; // Frequency in Hz int duration; // Duration in ms int quietDuration; // The quiet duration after each note } Note; // Super Mario Bros. short intro melody /*Note melody[] = { {660, 100}, {0, 150}, {660, 100}, {0, 300}, {660, 100}, {0, 300}, {510, 100}, {660, 100}, {770, 100}, {0, 550}, {380, 100}, {0, 500}, {510, 100}, {0, 300}, {380, 100}, {0, 300}, {320, 100}, {0, 700} // End of melody };*/ /*Note melody[] = { {440, 500}, {440, 500}, {440, 500}, {349, 350}, {523, 150}, {440, 500}, {349, 350}, {523, 150}, {440, 1000}, {659, 500}, {659, 500}, {659, 500}, {698, 350}, {523, 150}, {415, 500}, {349, 350}, {523, 150}, {440, 1000}, };*/ /*Note melody[] = { {1318, 150}, {2637, 150}, {2093, 150}, {2349, 150}, {3136, 300} };*/ /*Note melody[] = { {320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200}, {660, 100, 150}, {660, 100, 300}, {660, 100, 300}, {510, 100, 100}, {660, 100, 300}, {770, 100, 550}, {380, 100, 575}, {510, 100, 450}, {380, 100, 400}, {320, 100, 500}, {440, 100, 300}, {480, 80, 330}, {450, 100, 150}, {430, 100, 300}, {380, 100, 200}, {660, 80, 200}, {760, 50, 150}, {860, 100, 300}, {700, 80, 150}, {760, 50, 350}, {660, 80, 300}, {520, 80, 150}, {580, 80, 150}, {480, 80, 500}, {510, 100, 450}, {380, 100, 400}, {320, 100, 500}, {440, 100, 300}, {480, 80, 330}, {450, 100, 150}, {430, 100, 300}, {380, 100, 200}, {660, 80, 200}, {760, 50, 150}, {860, 100, 300}, {700, 80, 150}, {760, 50, 350}, {660, 80, 300}, {520, 80, 150}, {580, 80, 150}, {480, 80, 500}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 220}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 200, 300}, {1020, 80, 300}, {1020, 80, 150}, {1020, 80, 300}, {380, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 420}, {585, 100, 450}, {550, 100, 420}, {500, 100, 360}, {380, 100, 300}, {500, 100, 300}, {500, 100, 150}, {500, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 220}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 200, 300}, {1020, 80, 300}, {1020, 80, 150}, {1020, 80, 300}, {380, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 420}, {585, 100, 450}, {550, 100, 420}, {500, 100, 360}, {380, 100, 300}, {500, 100, 300}, {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 350}, {660, 80, 150}, {500, 80, 300}, {430, 80, 150}, {380, 80, 600}, {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 150}, {660, 80, 550}, {870, 80, 325}, {760, 80, 600}, {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 350}, {660, 80, 150}, {500, 80, 300}, {430, 80, 150}, {380, 80, 600}, {660, 100, 150}, {660, 100, 300}, {660, 100, 300}, {510, 100, 100}, {660, 100, 300}, {770, 100, 550}, {380, 100, 575} };*/ const Note melody[] = { {196.00, 152, 0}, {261.63, 152, 39}, {329.63, 151, 160}, {392.00, 152, 201}, {523.25, 152, 318}, {659.26, 151, 361}, {783.99, 456, 480}, {659.26, 456, 536}, {207.65, 152, 960}, {261.63, 152, 999}, {311.13, 151, 1120}, {415.30, 152, 1161}, {523.25, 152, 1278}, {622.25, 151, 1321}, {830.61, 456, 1440}, {622.25, 456, 1496}, {233.08, 152, 1920}, {293.66, 152, 1959}, {349.23, 151, 2080}, {466.16, 152, 2121}, {587.33, 152, 2238}, {698.46, 151, 2281}, {932.33, 456, 2400}, {932.33, 152, 2456}, {932.33, 152, 2863}, {932.33, 151, 2616}, {659.26, 456, 3025}, }; int melody_size = sizeof(melody) / sizeof(melody[0]); static void play_tone(int frequency, float duration, int quietDuration) { int semitones = 2; int t_freq = frequency * pow(2.0, semitones / 12.0); int8_t cw_offset = 0; dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, t_freq, cw_offset}; if (frequency == 0) { dac_output_disable(DAC_CHANNEL_1); } else { dac_cw_generator_config(&cosineConf); dac_output_enable(DAC_CHANNEL_1); } vTaskDelay(pdMS_TO_TICKS(duration)); //dac_output_disable(DAC_CHANNEL_1); //vTaskDelay(pdMS_TO_TICKS(quietDuration)); } void play_melody() { for (int i = 0; i < melody_size; i++) { play_tone(melody[i].frequency, melody[i].duration, melody[i].quietDuration); } dac_output_disable(DAC_CHANNEL_1); } static void setupSound() { int8_t cw_offset = 0; dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, 440, cw_offset}; dac_cw_generator_config(&cosineConf); dac_cw_generator_enable(); //dac_output_enable(DAC_CHANNEL_1); //vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1 second dac_output_disable(DAC_CHANNEL_1); ESP_LOGI("SOUND", "Sound setup"); } static void sound_task(void *pvParameters) { setupSound(); while (1) { vTaskDelay(pdMS_TO_TICKS(5000)); play_melody(); } ESP_LOGI("SOUND", "Sound task starts...."); } void initSound() { xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL); ESP_LOGI("SOUND", "Sound initialized"); }