acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Speaker.cpp (2254B)

      1 #include "Speaker.h"
      2 
      3 
      4 void playNotificationSound() {
      5     playTone(1000, 50);
      6     delay(100);
      7     playTone(1500, 50);
      8     delay(100);
      9     playTone(2000, 50);
     10     delay(100);
     11     playTone(500, 50);
     12 }
     13 
     14 
     15 void playRTTTL(const char* rtttl) {
     16     static AudioGeneratorRTTTL *rtttlGenerator = new AudioGeneratorRTTTL();
     17     static AudioOutputI2S *audioOutput = new AudioOutputI2S();
     18     static AudioFileSourcePROGMEM *fileSource = new AudioFileSourcePROGMEM(rtttl, strlen(rtttl));
     19 
     20     audioOutput->begin();
     21     rtttlGenerator->begin(fileSource, audioOutput);
     22 
     23     while (rtttlGenerator->isRunning())
     24         rtttlGenerator->loop();
     25 
     26     rtttlGenerator->stop();
     27     fileSource->close();
     28 }
     29 
     30 
     31 void playTone(float frequency, int duration, int volume) {
     32     volume = constrain(volume, 0, 32767);
     33     const int wave_period = SAMPLE_RATE / frequency;
     34     int16_t sample_buffer[wave_period];
     35 
     36     for (int i = 0; i < wave_period; ++i)
     37         sample_buffer[i] = (i < wave_period / 2) ? volume : -volume;
     38 
     39     int total_samples = SAMPLE_RATE * duration / 1000;
     40     int samples_written = 0;
     41 
     42     while (samples_written < total_samples) {
     43         int to_write = min(wave_period, total_samples - samples_written);
     44         i2s_write(BOARD_I2S_PORT, sample_buffer, to_write * sizeof(int16_t), (size_t *)&to_write, portMAX_DELAY);
     45         samples_written += to_write;
     46     }
     47 }
     48 
     49 
     50 void setupI2S() {
     51     i2s_config_t i2s_config = {
     52         .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
     53         .sample_rate = SAMPLE_RATE,
     54         .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
     55         .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
     56         .communication_format = I2S_COMM_FORMAT_STAND_I2S,
     57         .intr_alloc_flags = 0,
     58         .dma_buf_count = 8,
     59         .dma_buf_len = 64,
     60         .use_apll = false,
     61         .tx_desc_auto_clear = true,
     62         .fixed_mclk = 0
     63     };
     64 
     65     i2s_pin_config_t pin_config = {
     66         .bck_io_num   = BOARD_I2S_BCK,
     67         .ws_io_num    = BOARD_I2S_WS,
     68         .data_out_num = BOARD_I2S_DOUT,
     69         .data_in_num  = I2S_PIN_NO_CHANGE
     70     };
     71 
     72     i2s_driver_install(BOARD_I2S_PORT, &i2s_config, 0, NULL);
     73     i2s_set_pin(BOARD_I2S_PORT, &pin_config);
     74     i2s_set_clk(BOARD_I2S_PORT, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
     75 }