Skip to content

Audio cracking with MIX_SetTrackFrequencyRatio below 1.0 #873

Description

@danilwhale

Trying to play 44.1kHz mono(?) track produces noticeable crack in certain cases. Audio file I was able to reproduce this with:

wood2.zip

Minimal reproduction code:

#include <SDL3/SDL.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <stdio.h>
#include <stdbool.h>

int main()
{
    if (!SDL_Init(SDL_INIT_AUDIO)) { printf("failed to init audio: %s", SDL_GetError()); return 1; }
    if (!MIX_Init()) { printf("failed to init mixer: %s", SDL_GetError()); return 1; }
    
    SDL_AudioSpec spec;
    if (!SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL)) { printf("failed to get format: %s", SDL_GetError()); return 1; }
    
    // enforce 44.1khz, which workarounds other bug with 44.1khz track cracking on 48khz or 96khz mixer
    spec.freq = 44100;
    MIX_Mixer* mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec);
    if (!mixer) { printf("failed to create mixer: %s", SDL_GetError()); return 1; }
    
    MIX_Audio* audio = MIX_LoadAudio(mixer, "wood2.ogg", true);
    if (!audio) { printf("failed to load audio: %s", SDL_GetError()); return 1; }
    
    MIX_Track* track = MIX_CreateTrack(mixer);
    if (!track) { printf("failed to create track: %s", SDL_GetError()); return 1; }
    
    MIX_SetTrackAudio(track, audio);
    MIX_SetTrackFrequencyRatio(track, 0.8F); // <--- causes earlier mentioned crack when playing
    if (!MIX_PlayTrack(track, 0)) { printf("failed to play track: %s", SDL_GetError()); return 1; }
    
    while (MIX_TrackPlaying(track)) {}
    
    MIX_DestroyTrack(track);
    MIX_DestroyAudio(audio);    
    MIX_DestroyMixer(mixer);
    MIX_Quit();
    
    return 0; 
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions