TMRpcm icon indicating copy to clipboard operation
TMRpcm copied to clipboard

TMRh20/TMRpcm and speedcontrol

Open twilight102 opened this issue 2 years ago • 2 comments

Hello, I have a question, but I think with me many. Is it possible to add a speed (pitch) control in the tmrpcm files? I've found some on the arduino forum but don't get it working. https://forum.arduino.cc/t/success-getting-tmrpcm-to-change-the-pitch-and-speed-of-sounds/586697/15 This is what they wrote:

The problem is that this code is trying to modify variables that are global within the tmrpcm library code, but not your program. It works if you put it inside the tmrpcm library.

Create a function in the TMRpcm.cpp file, which contains the code which Lucario448 wrote. I called this ‘changespeed’, so my function declaration was this:

void TMRpcm::changespeed(unsigned int newspeed){ unsigned long newSR = map(newspeed, 0, 1023, 8000, 44100);

if (bitRead(optionByte,6)) *ICRn[tt] = 10 * (800000 /  newSR);
else *ICRn[tt] = 10 * (1600000 / newSR);

} and I put this at line 448, just before the area marked

//*************************************************************************************** //********************** Functions for single track playback **************************** (though I could probably have put this anywhere in the tmrpcm.cpp file).

Then, I modified the ‘TMRpcm.h’ file accordingly, to add the new function to the list of available functions or the compiler gets confused if you don’t. Again, I added this before the play function (line 31)

void changespeed(unsigned int newspeed);

Now, I can call this function from anywhere in my code whenever I want to change the playback speed, if I want to control if from analog0, I just call tmrpcm.changespeed(analogRead(A0));

If it can be implemented properly, then this is a good addition to Tmrpcm.

Ebro de Wit (NL)

twilight102 avatar Jun 09 '22 17:06 twilight102

Interesting, this is a feature I never thought about. If I get some development time on this lib, I'll look into this one further. For now I'll leave this issue open as a potential enhancement.

TMRh20 avatar Jun 12 '22 15:06 TMRh20

thanks for the reply.

In model building, a lot of diesel noises are made, with the TMRPCM library, but that doesn't work very well. They take a sample of a real diesel. Only the speed change cannot be made. It would be a real addition, a world opens up!

Ebro

twilight102 avatar Jun 12 '22 16:06 twilight102

I just added some new functions to the library per your suggestions, but with some modifications to keep it very simple.

tmrpcm.speedUp(10); -- Speeds up the audio by adjusting the ICR value by 10 tmrpcm.speedDown(5); -- Speeds down the audio by adjusting the ICR value by 5 tmrpcm.getSpeed(); -- Gets the current ICR value of the audio playing tmrpcm.setSpeed(200); -- Sets the current ICR value of the audio playing tmrpcm.setSpeed(200,1); -- Sets the current ICR value and make it persist, so subsequent tracks will use the same value

All of the above commands will also take a timer value like 0 for the default timer, 1 for the next available timer and so on. This is only important on Arduino Mega etc with multiple 16-bit timers. tmrpcm.setSpeed(200, 0, 1); to set the speed to 200 on the second timer.

Users would need to download the library directly from GitHub currently to test out. To enable this functionality edit pcmConfig.h and uncomment the #define SPEED_CONTROL line

TMRh20 avatar Sep 28 '23 02:09 TMRh20