iOS SoundTouch 框架 BPM 检测示例

标签 ios audio objective-c++ soundtouch

我在网上搜索过,找不到关于如何使用 SoundTouch library 的教程。用于节拍检测。

(注意:在此之前我没有 C++ 经验。我知道 C、Objective-C 和 Java。所以我可能搞砸了一些,但它编译了。)

我添加了 framework到我的项目并设法编译以下内容:

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];

NSData *data = [NSData dataWithContentsOfFile:path];

player =[[AVAudioPlayer alloc] initWithData:data error:NULL];

player.volume = 1.0;

player.delegate = self;

[player prepareToPlay];
[player play];

NSUInteger len = [player.data length]; // Get the length of the data

soundtouch::SAMPLETYPE sampleBuffer[len]; // Create buffer array

[player.data getBytes:sampleBuffer length:len]; // Copy the bytes into the buffer

soundtouch::BPMDetect *BPM = new soundtouch::BPMDetect(player.numberOfChannels, [[player.settings valueForKey:@"AVSampleRateKey"] longValue]); // This is working (tested)

BPM->inputSamples(sampleBuffer, len); // Send the samples to the BPM class

NSLog(@"Beats Per Minute = %f", BPM->getBpm()); // Print out the BPM - currently returns 0.00 for errors per documentation

inputSamples(*samples, numSamples) 歌曲字节信息让我很困惑。

如何从歌曲文件中获取这些信息?

我尝试使用 memcpy() 但它似乎不起作用。

有人有什么想法吗?

最佳答案

经过几个小时的调试和阅读网络上有限的文档,我在绊倒这个之前修改了一些东西:你需要将 numSamples 除以 numberOfChannelsinputSamples() 函数。

我的最终代码是这样的:

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];

NSData *data = [NSData dataWithContentsOfFile:path];

player =[[AVAudioPlayer alloc] initWithData:data error:NULL];

player.volume = 1.0;    // optional to play music

player.delegate = self;

[player prepareToPlay]; // optional to play music
[player play];          // optional to play music

NSUInteger len = [player.data length];

soundtouch::SAMPLETYPE sampleBuffer[len];

[player.data getBytes:sampleBuffer length:len];

soundtouch::BPMDetect BPM(player.numberOfChannels, [[player.settings valueForKey:@"AVSampleRateKey"] longValue]);

BPM.inputSamples(sampleBuffer, len/player.numberOfChannels);

NSLog(@"Beats Per Minute = %f", BPM.getBpm());

关于iOS SoundTouch 框架 BPM 检测示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757975/

相关文章:

ios - 如何将多个对象添加到表格 View 单元格?

audio - 将 wav 和 mp4 与 ffmpeg 合并时音视频同步漂移缓慢

Android 使用自定义编码器录制音频

objective-c - Objective C 添加方法到类参数不匹配

ios - 在 Objective C 示例教程中使用 C++ 文件

ios - 如何使用来自 ObjC 类的 std::function 回调在 C++ 中进行异步工作?

ios - 在生产模式下的设备上进行测试时如何查看 NSLog 语句?

iphone - 将 Adwhirl 广告方向从纵向更改为横向

ios - Mapkit中有没有什么函数可以一直跟踪当前可见区域?

ios - 如何在 Nativescript 上访问 ios 核心音频(AudioQueue)