Splitting music file into chunks
How would you go about splitting a music file (preferably mp3) into chunks?
I am usi开发者_运维百科ng the SDL_mixer API. There may be some useful functions in there but I couldn't find any.
The purpose is to use FFT on each chunk to get the frequencies to use in visualization.
I see now that the SDL_mixer library isn't going to give you what you want. Its support for "music" (MP3) plays the file externally from the mixer, so apart from volume and position controls that the API provides, you can't insert yourself into its audio stream.
If you were using Mix_Chunk
objects instead to take advantage of the channel mixer, you would be able to add yourself as an effect on the channel that's streaming the music, using Mix_RegisterEffect
. Another place you could insert yourself is after the mix, using Mix_SetPostMix
, if you wanted to visualize the final mix of chunks instead of an individual channel or channels. However, those chunks are best suited for short sounds, as they are loaded entirely into memory rather than streamed – and they don't currently support MP3.
If you're committed to using SDL, consider using SDL_sound for this task. This is another extension to SDL, which handles just the decoding of files. It hands you data a chunk at a time when you use Sound_Decode
. You can then take the decoded data and pass it to the mixer by using Mix_HookMusic
to keep a stream-like approach. Or, you could even load the whole file with Sound_DecodeAll
, and fill in a Mix_Chunk
directly, if you want to take advantage of the mixer and effect functions, at the expense of streaming.
Things to watch out for:
- Make sure the audio is decoded in the output format that the mixer expects.
- Look out for any case where the decoder can't keep up with the mixer on occasion – in which case you'd need to come up with a separate process for the decoding so that you can stream in ahead of where the mixer is and handle occasional bumps in reading without glitching.
- There may be cases where the chunk size you get from the decoder is not the chunk size you want for analysis. You might be able to use
Sound_SetBufferSize
to simplify this problem.
精彩评论