将音乐文件分割成块

发布于 2024-11-17 09:06:32 字数 114 浏览 3 评论 0原文

您将如何将音乐文件(最好是 mp3)分割成块?

我正在使用 SDL_mixer API。里面可能有一些有用的功能,但我找不到。

目的是对每个块使用 FFT 以获得可视化中使用的频率。

How would you go about splitting a music file (preferably mp3) into chunks?

I am using 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯寂 2024-11-24 09:06:33

我现在发现 SDL_mixer 库不会给你你想要的东西。它对“音乐”(MP3) 的支持从混音器外部播放文件,因此除了 API 提供的音量和位置控制之外,您无法将自己插入到其音频流中。

如果您使用 Mix_Chunk 对象来利用通道混音器,则可以使用 Mix_RegisterEffect 将自己添加为流式传输音乐的通道上的效果。如果您想可视化块的最终混合而不是单个通道或多个通道,您可以自己插入的另一个位置是在混合之后,使用 Mix_SetPostMix。然而,这些块最适合短声音,因为它们完全加载到内存中而不是流式传输 - 并且它们当前不支持 MP3。

如果您决心使用 SDL,请考虑使用 SDL_sound 来完成此任务。这是 SDL 的另一个扩展,它只处理文件的解码。当您使用 Sound_Decode 时,它会一次向您提供一块数据。然后,您可以使用 Mix_HookMusic 将解码后的数据传递到混音器,以保持类似流的方法。或者,如果您想利用混音器和效果函数,您甚至可以使用 Sound_DecodeAll 加载整个文件,并直接填充 Mix_Chunk,但代价是流式传输。

需要注意的事项:

  1. 确保音频以混音器期望的输出格式进行解码。
  2. 请注意解码器有时无法跟上混音器的任何情况 - 在这种情况下,您需要为解码提出一个单独的过程,以便您可以在混音器所在的位置之前进行流式传输并进行处理阅读过程中偶尔会遇到一些小问题,不会出现任何问题。
  3. 在某些情况下,您从解码器获得的块大小可能不是您想要分析的块大小。您也许可以使用 Sound_SetBufferSize 来简化此问题。

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:

  1. Make sure the audio is decoded in the output format that the mixer expects.
  2. 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.
  3. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文