我在网上搜索过,找不到关于如何使用 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
除以 numberOfChannels
在inputSamples()
函数。
我的最终代码是这样的:
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/