How to lower the quality and specs of a wav file o

2019-08-03 05:21发布

So to preface my problem, I'll give some context.

In SDL2 you can load wav files such as from the wiki:

SDL_AudioSpec wav_spec;
Uint32 wav_length;
Uint8 *wav_buffer;

/* Load the WAV */
if (SDL_LoadWAV("test.wav", &wav_spec, &wav_buffer, &wav_length) == NULL) {
    fprintf(stderr, "Could not open test.wav: %s\n", SDL_GetError());
} else {
    /* Do stuff with the WAV data, and then... */
    SDL_FreeWAV(wav_buffer);
}

The issue I'm getting from SDL_GetError is Complex WAVE files not supported


Now the wav file I'm intending to open has the following properties:

Playing test.wav.
Detected file format: WAV / WAVE (Waveform Audio) (libavformat)
ID_AUDIO_ID=0
[lavf] stream 0: audio (pcm_s24le), -aid 0
Clip info:
 encoded_by: Pro Tools
ID_CLIP_INFO_NAME0=encoded_by
ID_CLIP_INFO_VALUE0=Pro Tools
 originator_reference: 
ID_CLIP_INFO_NAME1=originator_reference
ID_CLIP_INFO_VALUE1=
 date: 2016-05-1
ID_CLIP_INFO_NAME2=date
ID_CLIP_INFO_VALUE2=2016-05-1
 creation_time: 20:13:34
ID_CLIP_INFO_NAME3=creation_time
ID_CLIP_INFO_VALUE3=20:13:34
 time_reference:
ID_CLIP_INFO_NAME4=time_reference
ID_CLIP_INFO_VALUE4=
ID_CLIP_INFO_N=5
Load subtitles in dir/
ID_FILENAME=dir/test.wav
ID_DEMUXER=lavfpref
ID_AUDIO_FORMAT=1
ID_AUDIO_BITRATE=2304000
ID_AUDIO_RATE=48000
ID_AUDIO_NCH=2
ID_START_TIME=0.00
ID_LENGTH=135.53
ID_SEEKABLE=1
ID_CHAPTERS=0
Selected audio codec: Uncompressed PCM [pcm]
AUDIO: 48000 Hz, 2 ch, s24le, 2304.0 kbit/100.00% (ratio: 288000->288000)
ID_AUDIO_BITRATE=2304000
ID_AUDIO_RATE=48000
ID_AUDIO_NCH=2
AO: [pulse] 48000Hz 2ch s16le (2 bytes per sample)
ID_AUDIO_CODEC=pcm

From the wiki.libsdl.org/SDL_OpenAudioDevice page and subsequent wiki.libsdl.org/SDL_AudioSpec#Remarks page I can at least surmise that a wav file of:

freq = 48000;
format = AUDIO_F32;
channels = 2;
samples = 4096;

quality should work.

The main problem I can see is that my wav file has the s16le format whereas it's not listed on the SDL_AudioSpec page.

This leads me to believe I need to reduce the quality of test.wav so it does not appear as "complex" in SDL.

When I search engine Complex WAVE files not supported nothing helpful comes up, except it appears in the SDL_Mixer library, which as far as I know I'm not using.

Can the format be changed via ffmepg to work in SDL2?

Edit: This appears to be the actual code in SDL2 where it complains. I don't really know enough about C to dig all the way through the vast SDL2 library, but I thought it might help if someone notices something just from hinting variable names and such:

/* Read the audio data format chunk */
chunk.data = NULL;
do {
    if ( chunk.data != NULL ) {
        SDL_free(chunk.data);
        chunk.data = NULL;
    }
    lenread = ReadChunk(src, &chunk);
    if ( lenread < 0 ) {
        was_error = 1;
        goto done;
    }
    /* 2 Uint32's for chunk header+len, plus the lenread */
    headerDiff += lenread + 2 * sizeof(Uint32);
} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );

/* Decode the audio data format */
format = (WaveFMT *)chunk.data;
if ( chunk.magic != FMT ) {
    SDL_SetError("Complex WAVE files not supported");
    was_error = 1;
    goto done;
}

1条回答
2楼-- · 2019-08-03 05:54

After a couple hours of fun audio converting I got it working, will have to tweak it to try and get better sound quality.

To answer the question at hand, converting can be done by:

ffmpeg -i old.wav -acodec pcm_s16le -ac 1 -ar 16000 new.wav

To find codecs on your version of ffmpeg:

ffmpeg -codecs

This format works with SDL.

Next within SDL when setting the desired SDL_AudioSpec make sure to have the correct settings:

freq = 16000;
format = AUDIO_S16LSB;
channels = 2;
samples = 4096;

Finally the main issue was most likely using the legacy SDL_MixAudio instead of the newer SDL_MixAudioFormat

With the following settings:

SDL_MixAudioFormat(stream, mixData, AUDIO_S16LSB, len, SDL_MIX_MAXVOLUME / 2); as can be found on the wiki.

查看更多
登录 后发表回答