iOS SoundTouch框架BPM检测示例 [英] iOS SoundTouch framework BPM Detection example

查看:261
本文介绍了iOS SoundTouch框架BPM检测示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have searched all over the web and cannot find a tutorial on how to use the SoundTouch library for beat detection.

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

(Note: I have no C++ experience prior to this. I do know C, Objective-C, and Java. So I could have messed some of this up, but it compiles.)

我添加了框架到我的项目并设法获得以下编译:

I added the framework to my project and managed to get the following to compile:

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)歌曲字节信息让我困惑。

The inputSamples(*samples, numSamples) song byte information confuses me.

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

How do I get these pieces of information from a song file?

我尝试使用 memcpy()但它似乎无法正常工作。

I tried using memcpy() but it doesn't seem to be working.

有人有什么想法吗?

推荐答案

经过数小时的调试和阅读有限的文档在网上,我修改了一些东西,然后绊倒了:你需要在 numberOfChannels 中将 numSamples 划分为 inputSamples() function。

After hours and hours of debugging and reading the limited documentation on the web, I modified a few things before stumbling upon this: You need to divide numSamples by numberOfChannels in the inputSamples() function.

我的最终代码是这样的:

My final code is like so:

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检测示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆