Repeating 8bit audio

Hello.
I've just started Arduino and I'd love some input. I'm trying to play & shine an LED continuously while the button is pressed. I've been able to get the LED to shine and the sound to play when I press the LED. However, only the LED continues to shine while the sound only plays once.
How do I get the sound to play continuously ??
This is my code (sorry that it's sloppy)

#include <PCM.h>
const char BUTTON = 2;
const int LED = 3;
//bool pressed = false;
int buttonState = 0;
const unsigned char sample[] PROGMEM = {numbersssssssssssssss};

void setup()
{
  //pinMode(BUTTON, INPUT_PULLUP);
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(sample, OUTPUT);
  
  
}

void loop()
{
  //bool currenState = digitalRead(BUTTON);
  buttonState = digitalRead(BUTTON);
  if (buttonState == LOW)
  {
    digitalWrite(LED, LOW);
    startPlayback(sample, sizeof(sample));
  } 
  else{
    digitalWrite(LED, HIGH);
    //stopPlayback();
    
    
  }
}

Welcome to the forum!

I looked into the library and unfortunately it doesn't seem to be a feature that can be easily implemented using this library.

You could however modify the library and add a parameter to the function startPlayback() or add your own function.

Maybe start a millis timer together with your sound file. Make it restart the sound file after x seconds (when sound file is over). Then also reset the timer.
I guess the playtime of your file will be proportional to its size. So you can automate timing if you have different sound files.

It really seems to be a one-liner modifying the library though ...

Modifying a library written by someone else is almost never a oneliner...

To repeat the playback, it is in fact a one liner (tested).

In the PCM library module named PCM.c, replace this:

// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {
  if (sample >= sounddata_length) {
    if (sample == sounddata_length + lastSample) {
        stopPlayback();
    }

with this:

// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {
  if (sample >= sounddata_length) {
    if (sample == sounddata_length + lastSample) {
      sample=0;
    }

Of course it will never stop repeating, so a more elegant solution could be imagined.

The entire library code is so simple and short that you might as well copy/paste it into your sketch, at the bottom, rather than use #include. Then change it in any way you like.

1 Like

Rather than changing the library, you can call startPlayback(sample, sizeof(sample)); again when the sound ends:

  if (buttonState == LOW)
  {
    digitalWrite(LED, LOW);
    // If the sound is not playing, start it.
    if (TIMSK1 & _BV(OCIE1A) == 0)
       startPlayback(sample, sizeof(sample));
  } 

I seem to be doing something wrong because it's not working..

What is "it"?

sorry, I meant to add this code. Tried this but only the LED came on

Post YOUR modified code.

My modification to the library actually does work.

I looked up how to update a library for Arduino and it works!! As long as the button is pressed down, the sound plays. brilliant solution!

I must admit: it is a oneliner.
But maybe not easy for a newbee to find such a solution...

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.