system
Given the following post, respond as if you're commenting on Reddit:
human
I want to write an algorithm to alter the speed of the played music, while keeping the pitch.

I want to write a music editor, and the main component would make it possible to alter the speed of the played music.

Could you help me figure out where to start? - I don't know a lot of math and science in general, so I could start with basic things, if necessary.

Edit:

I just wanted to thank everybody, I really appreciate all the helpful information.
gpt
>while keeping the pitch

You should realise that this is a *very* hard problem. There is no *general* method that can alter the speed of a piece of digitised sound / music (while keeping the pitch) without introducing unwanted side effects. Each technique has their own trade-offs. A piece of music will be suitable for one algorithm and another piece of music with a different style and instrumentation will be suitable for another algorithm. There are entire companies that sell algorithms for this and nothing else!

Two "easiest" methods you can start with:

1) Use granular sampling. You basically create a granular playback engine, chop incoming audio to very small (5-30ms) bits, apply a windowing function over them (so each grain fades in and out gracefully instead of starting and ending abruptly, creating nasty artifacts) and trigger them at different speeds / overlaps. So think of it like resizing an image - you cut multiple copies of image to identical sized square bits and combine them with different levels of overlap to make it bigger (slow down) or discard some (speed up). While doing this, pitch will generally be preserved - there are a lot of variables for this, like window size, overlap ratio, windowing function, even variables for something like how regularly you will trigger grains to decrease the artefacts caused by the windowing function being audible etc.

2) You can use FFT to decompose time domain signal to frequency domain - then play the data with your own sine bank oscillators at your own pace.

Both of those methods will smear the fast attacks, so rhythmic instruments, the attack portions will be smeared when you slow things down. To compensate for that, you need more intelligent algorithms - machine listening / computer audition people deal with that. You detect attacks, or other patterns in sound and tweak your algorithms to respond to those instances robustly etc.

All this requires quite a bit of work, if you aren't an experienced programmer and have no experience in audio programming, DSP, and related math it will take you a loong time to put those together. It certainly is possible, but if you are looking at this as a quick project, you might be disappointed.

There also are open source implementations for this purpose so you can take a look at what they are doing! Beware that best implementations of this are proprietary (those used by popular DAWS like Ableton Live etc.) and cost a pretty penny.