iOS SoundTouch框架BPM检测示例

MrH*_*hma 5 audio objective-c++ soundtouch ios

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

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

我将框架添加到我的项目中并设法获得以下内容进行编译:

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
Run Code Online (Sandbox Code Playgroud)

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

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

我尝试过使用memcpy()但似乎没有用.

有人有什么想法?

MrH*_*hma 1

经过数小时的调试和阅读网络上有限的文档后,我修改了一些内容,然后偶然发现了这一点:您需要在函数中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());
Run Code Online (Sandbox Code Playgroud)