Objective-c/IOS:向后播放音频文件的最简单方法是什么

发布于 2024-12-03 19:50:44 字数 132 浏览 0 评论 0原文

我已经在这个问题上挣扎了一段时间了,而且网上没有任何代码示例。谁能帮助我吗?

我的应用程序使用 AVFoundation 来录制音频。 @16 位深度,2 个通道,WAV

我可以访问音频的字节,我只是不知道如何反转它。

I've been struggling with this one for quite a while now, and there are no code examples of it being done on the net. Can anyone help me?

My app uses AVFoundation to record the audio.
@16 bit depth , 2 channels , WAV

I can access the bytes of the audio, I just don't know how to reverse it.

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

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

发布评论

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

评论(3

向地狱狂奔 2024-12-10 19:50:44

波形数据样本是交错的。这意味着数据是这样组织的。

Sample 1 Left | Sample 1 Right | Sample 2 Left | Sample 2 right ... Sample n Left | Sample n right

由于每个样本都是 16 位(2 字节),因此前 2 个通道样本(即左通道和右通道)的大小为 4 字节。

这样您就知道波形数据块中的最后一个样本如下:

wavDataSize - 4

然后,您可以通过从记录末尾开始向后读取将每个样本复制到不同的缓冲区中来一次加载每个样本。当您到达波形数据的开头时,您已经反转了波形数据,并且播放也将反转。

编辑:如果您想在 iOS 上轻松读取波形文件,请查看 音频文件服务参考

编辑2:

readPoint  = waveDataSize;
writePoint = 0;
while( readPoint > 0 )
{
    readPoint -= 4;
    Uint32 bytesToRead = 4;
    Uint32 sample;
    AudioFileReadBytes( inFile, false, maxData, &bytesToRead &sample );
    AudioFileWriteBytes( outFile, false, writePoint, &bytesToRead, &sample );

    writePoint += 4;
}

In wave data samples are interleaved. This means the data is organised like this.

Sample 1 Left | Sample 1 Right | Sample 2 Left | Sample 2 right ... Sample n Left | Sample n right

As each sample is 16-bits (2 bytes) the first 2 channel sample (ie for both left and right) is 4 bytes in size.

This way you know that the last sample in a block of wave data is as follows:

wavDataSize - 4

You can then load each sample at a time by copying it into a different buffer by starting at the end of the recording and reading backwards. When you get to the start of the wave data you have reversed the wave data and playing will be reversed.

Edit: If you want easy ways to read wave files on iOS check out the Audio File Services Reference.

Edit 2:

readPoint  = waveDataSize;
writePoint = 0;
while( readPoint > 0 )
{
    readPoint -= 4;
    Uint32 bytesToRead = 4;
    Uint32 sample;
    AudioFileReadBytes( inFile, false, maxData, &bytesToRead &sample );
    AudioFileWriteBytes( outFile, false, writePoint, &bytesToRead, &sample );

    writePoint += 4;
}
裸钻 2024-12-10 19:50:44

假设是一个单块 WAV 文件,尝试对文件进行内存映射,并在回调期间从文件末尾开始以相反的顺序将样本复制到音频队列或 RemoteIO 缓冲区中,同时使用这些 API 之一来播放音频。在到达 WAV/RIFF 标头(通常是第一个 44 字节)之前停止复制。

Assuming a single-chunk WAV file, try memmapping the file, and copying the samples out in reverse order, starting from the end of the file, into Audio Queue or RemoteIO buffers, during the callbacks, while using one of those APIs to play audio. Stop copying before you get to the WAV/RIFF header (commonly the 1st 44 bytes).

妖妓 2024-12-10 19:50:44

要反转音频,为什么不在 MPMediaPlayback 中使用 currentPlaybackRate (https://developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPMediaPlayback_protocol/Reference/Reference.html)

To reverse a audio, why not use the currentPlaybackRate in MPMediaPlayback (https://developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPMediaPlayback_protocol/Reference/Reference.html)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文