adafruit / Adafruit_VS1053_Library

This is a Arduino library for the Adafruit VS1053 Codec Breakout and Music Maker Shields
https://www.adafruit.com/products/1381
135 stars 113 forks source link

Add fast forward feature to playing sound tracks. #74

Closed cogliano closed 1 year ago

cogliano commented 3 years ago

This pull request adds the ability to speed-up or fast forward tracks using the built-in feature of the VS1053 chip. Two functions were added:

I have been using it in my own code for a while and thought it would be useful to contribute it to the library.

To test, simply add a statement like "musicPlayer.setPlaySpeed(2);" to the player_simple.ino example prior to the musicPlayer.startPlayingFile() call.

cogliano commented 3 years ago

Just fixed it. Thanks, I should have seen that.

MaxPleaner commented 2 years ago

Thank you for sharing this @cogliano, was trying to figure this out

MaxPleaner commented 2 years ago

I wonder how difficult it would be to add the ability to slow down, or to manipulate the audio in a more fluid way (e.g. control the speed with a slider).

MaxPleaner commented 2 years ago

I actually just figured this out. If anyone else is curious, you can change the play speed fluidly using sample rate:

void Adafruit_VS1053_FilePlayer::setSampleRate(uint16_t sampleRate) {
  noInterrupts();
  sciWrite(VS1053_REG_AUDATA, sampleRate);
  interrupts();
}

Need to call this after startPlayingFile which sets the initial sample rate. You can then run

 uint16_t track_info = musicPlayer.sciRead(VS1053_REG_AUDATA);
 int rate = (track_info >> 1) * 2;
 int stereo = ((track_info << 15) >> 15);

To read the auto-calculated sample rate, before adjusting it to your liking,.

Awesome!!

johnwargo commented 1 year ago

@ladyada what do you need to be able to merge this one? I can independently test it if you need?

ladyada commented 1 year ago

this one looks OK - thanks for reviewing1