libsdl-org / SDL_mixer

An audio mixer that supports various file formats for Simple Directmedia Layer.
zlib License
433 stars 146 forks source link

Is default MP3 implementation broken? #438

Open fjtrujy opened 2 years ago

fjtrujy commented 2 years ago

Hello, I'm having some problems running mp3 files in the PS2 port.

First of all, let me tell you how I'm compiling the library:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$PS2DEV/ps2sdk/ps2dev.cmake  -DCMAKE_INSTALL_PREFIX=$PS2SDK/ports -DBUILD_SHARED_LIBS=OFF \
    -DCMAKE_POSITION_INDEPENDENT_CODE=OFF -DSDL2MIXER_DEPS_SHARED=OFF -DSDL2MIXER_OPUS=OFF -DSDL2MIXER_MIDI=OFF -DSDL2MIXER_FLAC=OFF \
    -DSDL2MIXER_MOD=ON -DSDL2MIXER_SAMPLES=OFF ..

Then I have created the next example:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

static const int width = 640;
static const int height = 480;

int main(int argc, char **argv)
{
    // Initialize SDL video and audio systems
    SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);

    // Initialize SDL mixer
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);

    int flags = 0;
    flags |= MIX_INIT_MP3;
    int initted = Mix_Init(flags);
    if((initted & flags) != flags)
    {
        printf("Mix_Init: Failed to init required mod support!\n");
        printf("Mix_Init: %s\n", Mix_GetError());
        return 1;
    }

    // Load audio files
    Mix_Music *backgroundSound = Mix_LoadMUS("background.mp3");
    Mix_Chunk *jumpEffect = Mix_LoadWAV("jumpEffect.wav");

    // Create a SDL window
    SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_FULLSCREEN);

    // Create a renderer (accelerated and in sync with the display refresh rate)
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

    // Loading Controller
    SDL_Joystick *joy = SDL_JoystickOpen(0);

    // Start the background music
    Mix_PlayMusic(backgroundSound, -1);

    bool running = true;
    SDL_Event event;
    while(running)
    {
        // Process events
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                running = false;
            }
            else if(event.type == SDL_JOYBUTTONDOWN)
            {
                printf("Playing jumpEffect\n");
                Mix_PlayChannel(-1, jumpEffect, 0);
            }
        }

        // Clear screen
        SDL_RenderClear(renderer);

        // Show what was drawn
        SDL_RenderPresent(renderer);
    }

    // Release resources

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    Mix_FreeMusic(backgroundSound);
    Mix_FreeChunk(jumpEffect);
    Mix_CloseAudio();
    SDL_JoystickClose(joy);
    SDL_Quit();

    return 0;
}

I'm able to reproduce wav file without any issue every time that I press a key, however, the background music is not properly decoded, making a lot of noise:

https://user-images.githubusercontent.com/10010409/184988656-5f5a0286-e5e0-45a3-8287-638fd12837ea.mp4

Here you have also the original audio files:

PS2_mp3_issue.zip

Cheers

slouken commented 2 years ago

Hmm, it's working fine here, built on macOS using autotools. Have you tried on any other platforms? Maybe it's specific to the PS2 port?

fjtrujy commented 2 years ago

@sharkwouter could you give it a try in PSP and check if you are suffering the same error?

cheers