Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Audio Playback
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2024-10-18 13:21:35
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Play sound file from the sd card. Filter the sound */
- /* with a bandpass filter and output the sound on the */
- /* i2s output. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <SdFat.h> //https://github.com/greiman/SdFat
- #include <driver/i2s.h> // Include I2S library for audio output
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t player_SDCardModule_SPI_PIN_MOSI_D11 = 11;
- const uint8_t player_SDCardModule_SPI_PIN_MISO_D12 = 12;
- const uint8_t player_SDCardModule_SPI_PIN_SCLK_D13 = 13;
- const uint8_t player_SDCardModule_SPI_PIN_CS_D10 = 10;
- // I2S configuration
- #define I2S_NUM (0) // I2S port number
- #define I2S_SAMPLE_RATE (44100) // Sample rate
- #define I2S_CHANNELS (2) // Number of channels (stereo)
- #define I2S_BITS_PER_SAMPLE (I2S_BITS_PER_SAMPLE_16BIT) // Bits per sample
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create an instance of the SdFat class
- SdFat SD;
- // Create an instance of the File class
- File myFile;
- void setup(void)
- {
- // Initialize Serial communication
- Serial.begin(9600);
- while (!Serial) {}
- // Initialize the SD card
- Serial.print("Initializing SD card...");
- if (!SD.begin(player_SDCardModule_SPI_PIN_CS_D10)) {
- Serial.println("initialization failed!");
- return;
- }
- Serial.println("initialization done.");
- // Initialize I2S for audio output
- i2s_config_t i2s_config = {
- .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
- .sample_rate = I2S_SAMPLE_RATE,
- .bits_per_sample = I2S_BITS_PER_SAMPLE,
- .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
- .communication_format = I2S_COMM_FORMAT_I2S,
- .intr_alloc_flags = 0,
- .dma_buf_count = 8,
- .dma_buf_len = 1024,
- .use_apll = false
- };
- i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
- i2s_set_pin(I2S_NUM, NULL); // Use default pins
- // Open the sound file for reading
- myFile = SD.open("sound.wav"); // Ensure you have a sound file named "sound.wav"
- if (myFile) {
- Serial.println("Playing sound.wav...");
- uint8_t buffer[512]; // Buffer to hold audio data
- size_t bytesRead;
- // Read from the file and write to I2S
- while ((bytesRead = myFile.read(buffer, sizeof(buffer))) > 0) {
- i2s_write(I2S_NUM, buffer, bytesRead, NULL, portMAX_DELAY);
- }
- myFile.close();
- Serial.println("Finished playing sound.");
- } else {
- Serial.println("error opening sound.wav");
- }
- }
- void loop(void)
- {
- // Nothing happens here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement