Advertisement
Guest User

Untitled

a guest
Jun 10th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <driver/i2s.h>
  2. #include <SPIFFS.h>
  3.  
  4. #define BUTTON_PIN 9
  5.  
  6. // I2S pin config (verified working)
  7. #define I2S_BCLK 3
  8. #define I2S_LRC 2
  9. #define I2S_DOUT 10
  10.  
  11. #define SAMPLE_RATE 8000
  12. #define I2S_PORT I2S_NUM_0
  13.  
  14. void setup() {
  15. delay(2000);
  16. Serial.begin(115200);
  17. Serial.println("Button + WAV playback via I2S");
  18.  
  19. pinMode(BUTTON_PIN, INPUT_PULLUP);
  20.  
  21. if (!SPIFFS.begin()) {
  22. Serial.println("SPIFFS Mount Failed");
  23. return;
  24. }
  25.  
  26. i2s_config_t i2s_config = {
  27. .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
  28. .sample_rate = SAMPLE_RATE,
  29. .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  30. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // Full stereo frame
  31. .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  32. .intr_alloc_flags = 0,
  33. .dma_buf_count = 16,
  34. .dma_buf_len = 128,
  35. .use_apll = false, // More stable on ESP32-C3
  36. .tx_desc_auto_clear = true,
  37. .fixed_mclk = 0
  38. };
  39.  
  40. i2s_pin_config_t pin_config = {
  41. .bck_io_num = I2S_BCLK,
  42. .ws_io_num = I2S_LRC,
  43. .data_out_num = I2S_DOUT,
  44. .data_in_num = I2S_PIN_NO_CHANGE
  45. };
  46.  
  47. i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  48. i2s_set_pin(I2S_PORT, &pin_config);
  49. i2s_set_clk(I2S_PORT, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
  50. i2s_zero_dma_buffer(I2S_PORT);
  51. }
  52.  
  53. void playWav(const char* filename) {
  54. File file = SPIFFS.open(filename);
  55. if (!file) {
  56. Serial.println("Failed to open WAV file");
  57. return;
  58. }
  59.  
  60. file.seek(44); // Skip WAV header
  61.  
  62. const size_t monoBufSize = 512;
  63. uint8_t buffer[monoBufSize];
  64. size_t bytesRead;
  65.  
  66. while ((bytesRead = file.read(buffer, monoBufSize)) > 0) {
  67. int samples = bytesRead / 2;
  68. int16_t* stereoBuf = (int16_t*)malloc(samples * 2 * sizeof(int16_t));
  69.  
  70. for (int i = 0; i < samples; ++i) {
  71. int16_t mono = ((int16_t*)buffer)[i];
  72. stereoBuf[2 * i] = 0; // Left muted
  73. stereoBuf[2 * i + 1] = mono; // Right plays audio
  74. }
  75.  
  76. size_t bytesWritten;
  77. i2s_write(I2S_PORT, stereoBuf, samples * 4, &bytesWritten, portMAX_DELAY);
  78. vTaskDelay(pdMS_TO_TICKS(1));
  79. free(stereoBuf);
  80. }
  81.  
  82. file.close();
  83. Serial.println("Finished playing WAV");
  84. }
  85.  
  86. void loop() {
  87. static bool lastButtonState = HIGH;
  88. bool currentState = digitalRead(BUTTON_PIN);
  89.  
  90. if (lastButtonState == HIGH && currentState == LOW) {
  91. Serial.println("Button pressed! Playing WAV...");
  92. playWav("/coin.wav");
  93. }
  94.  
  95. lastButtonState = currentState;
  96. delay(20); // debounce
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement