Advertisement
pleasedontcode

Audio Playback rev_01

Oct 18th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Audio Playback
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-18 13:21:35
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Play sound file from the sd card. Filter the sound */
  21.     /* with a bandpass filter and output the sound on the */
  22.     /* i2s output. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SPI.h>
  27. #include <SdFat.h>  //https://github.com/greiman/SdFat
  28. #include <driver/i2s.h> // Include I2S library for audio output
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF SPI PINS *****/
  35. const uint8_t player_SDCardModule_SPI_PIN_MOSI_D11      = 11;
  36. const uint8_t player_SDCardModule_SPI_PIN_MISO_D12      = 12;
  37. const uint8_t player_SDCardModule_SPI_PIN_SCLK_D13      = 13;
  38. const uint8_t player_SDCardModule_SPI_PIN_CS_D10        = 10;
  39.  
  40. // I2S configuration
  41. #define I2S_NUM          (0) // I2S port number
  42. #define I2S_SAMPLE_RATE  (44100) // Sample rate
  43. #define I2S_CHANNELS     (2) // Number of channels (stereo)
  44. #define I2S_BITS_PER_SAMPLE (I2S_BITS_PER_SAMPLE_16BIT) // Bits per sample
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. // Create an instance of the SdFat class
  48. SdFat SD;
  49.  
  50. // Create an instance of the File class
  51. File myFile;
  52.  
  53. void setup(void)
  54. {
  55.     // Initialize Serial communication
  56.     Serial.begin(9600);
  57.     while (!Serial) {}
  58.  
  59.     // Initialize the SD card
  60.     Serial.print("Initializing SD card...");
  61.     if (!SD.begin(player_SDCardModule_SPI_PIN_CS_D10)) {
  62.         Serial.println("initialization failed!");
  63.         return;
  64.     }
  65.     Serial.println("initialization done.");
  66.  
  67.     // Initialize I2S for audio output
  68.     i2s_config_t i2s_config = {
  69.         .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
  70.         .sample_rate = I2S_SAMPLE_RATE,
  71.         .bits_per_sample = I2S_BITS_PER_SAMPLE,
  72.         .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  73.         .communication_format = I2S_COMM_FORMAT_I2S,
  74.         .intr_alloc_flags = 0,
  75.         .dma_buf_count = 8,
  76.         .dma_buf_len = 1024,
  77.         .use_apll = false
  78.     };
  79.  
  80.     i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  81.     i2s_set_pin(I2S_NUM, NULL); // Use default pins
  82.  
  83.     // Open the sound file for reading
  84.     myFile = SD.open("sound.wav"); // Ensure you have a sound file named "sound.wav"
  85.     if (myFile) {
  86.         Serial.println("Playing sound.wav...");
  87.         uint8_t buffer[512]; // Buffer to hold audio data
  88.         size_t bytesRead;
  89.  
  90.         // Read from the file and write to I2S
  91.         while ((bytesRead = myFile.read(buffer, sizeof(buffer))) > 0) {
  92.             i2s_write(I2S_NUM, buffer, bytesRead, NULL, portMAX_DELAY);
  93.         }
  94.         myFile.close();
  95.         Serial.println("Finished playing sound.");
  96.     } else {
  97.         Serial.println("error opening sound.wav");
  98.     }
  99. }
  100.  
  101. void loop(void)
  102. {
  103.     // Nothing happens here
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement