Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for changing channel playback rate. #236

Open
SDLBugzilla opened this issue Feb 11, 2021 · 6 comments
Open

Add support for changing channel playback rate. #236

SDLBugzilla opened this issue Feb 11, 2021 · 6 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@SDLBugzilla
Copy link
Collaborator

This bug report was migrated from our old Bugzilla tracker.

Reported in version: unspecified
Reported for operating system, platform: All, All

Comments on the original bug report:

On 2017-10-28 20:40:23 +0000, Ivan Epifanov wrote:

Ryan seems to have Mix_PlaybackRate in http://icculus.org/~icculus/tmp/mixer.c
Would be nice to have it in official SDL_Mixer too.

@SDLBugzilla SDLBugzilla added the enhancement New feature or request label Feb 11, 2021
@slouken slouken added this to the 2.8.0 milestone May 20, 2022
@icculus
Copy link
Collaborator

icculus commented May 20, 2022

Ehh...I wrote that for someone that needed an engine revving up effect in their game, but I don't know if it's really good quality work.

@icculus
Copy link
Collaborator

icculus commented May 20, 2022

There was a discussion somewhere of having some way to make SDL_AudioStream change it's resampling rate, instead of locking it in at creation time, and that would be useful here, too, though, so let's keep this open for now in any case.

@slouken
Copy link
Collaborator

slouken commented May 20, 2022

Ehh...I wrote that for someone that needed an engine revving up effect in their game, but I don't know if it's really good quality work.

It's not mathematically correct, but yeah, let's leave this open for future discussion.

@Wohlstand
Copy link
Contributor

Speaking about playback rate switch on the fly, I made a little experiment with the music on my MixerX fork side to see how that thing works: WohlSoft/SDL-Mixer-X@840606f

It works, but, when I change the rate (flushing and re-creating the stream instance), clicks appear (maybe there is a better way to flush all content of the stream buffer until re-create it?).

Speaking about channels, I highly recommend making the new call with the tempo factor as an argument to ensure it will start with given tempo immediately (especially if you choose the channel by a -1 number).

And, more about channels, there are three new fields that would appear:

  • tempo factor;
  • computed source rate (the sample rate passed to SDL_AudioStream as a source to trick it);
  • and the stream pointer itself.

Maybe I'll try to make some on my side for the experiment to see how the thing works in action...

The demo with music on my MixerX fork side:

2022-07-17_03-38-34.mp4

@slouken slouken modified the milestones: 2.8.0, 3.0 Jan 14, 2024
@CicTec
Copy link

CicTec commented Feb 1, 2024

Hi,

I'm porting SDL2 Mixer to our Game engine and seeing the functionality of other ported audio engines, I could provide the following information as a contribution:
*** Used in OpenAL/OpenAL Soft ***
alSourcef(source_id, AL_PITCH, rate); // rate values = 0.0 to 2.0 (1.0 = default rate)

*** Used in FMOD Ex/FMOD Studio ***
FMOD_Sound_SetDefaults(FMOD_SOUND *sound, float frequency, int priority)
Sets a sound's default playback attributes.

  • frequency
    Default playback frequency.
    Units: Hertz Default: 48000

FMOD_RESULT FMOD_Channel_SetFrequency(FMOD_CHANNEL *channel, float frequency)
Sets the frequency or playback rate.

  • frequency
    Playback rate.
    Units: Hertz
    Default frequency is determined by the audio format of the Sound or DSP.

FMOD_RESULT FMOD_Channel_SetPitch(FMOD_CHANNEL *channel, float pitch)
Sets the relative pitch / playback rate.

  • pitch
    Pitch value where 0.5 represents half pitch (one octave down), 1.0 represents unmodified pitch and 2.0 represents double pitch (one octave up).

Changing the rate in both libraries is individual, each sound, music or channel can have its own rate in playing, while maintaining the global one set by default.

Maybe looking at how OpenAL Soft is progressing can help.

@icculus
Copy link
Collaborator

icculus commented Feb 3, 2024

So in the time since this originally came up, SDL3's SDL_AudioStream came along and is super powerful for this; it can change both input and output sample rate on the fly, even managing data that is still queued at a different sample rate than new data that is just entering the queue.

More or less, this does exactly what is needed here.

However, while we have moved SDL_mixer to SDL3, we have not made each SDL_mixer channel use its own audiostream, which we would need to do to make this work.

Figuring out the logistics of that would solve this, and probably other problems, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants