5

ウェブ全体を検索しましたが、 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

曲のinputSamples(*samples, numSamples)バイト情報は私を混乱させます。

これらの情報を曲ファイルから取得するにはどうすればよいですか?

使ってみmemcpy()ましたが、うまくいかないようです。

誰にも考えはありますか?

4

3 に答える 3

0

iOSミュージックライブラリ内のmp3ファイルからBPMを読み取るためにこのソリューションを試しました(TSLibraryImportクラスを使用してwavに変換します):

                                MPMediaItem *item = [collection representativeItem];

                                 NSURL *urlStr = [item valueForProperty:MPMediaItemPropertyAssetURL];

                                 TSLibraryImport* import = [[TSLibraryImport alloc] init];

                                 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                 NSString *documentsDirectory = [paths objectAtIndex:0];

                                 NSURL* destinationURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"temp_data"]];
                                 [[NSFileManager defaultManager] removeItemAtURL:destinationURL error:nil];

                                 [import importAsset:urlStr toURL:destinationURL completionBlock:^(TSLibraryImport* import) {

                                     NSString *outPath = [documentsDirectory stringByAppendingPathComponent:@"temp_data"];


                                     NSData *data = [NSData dataWithContentsOfFile:outPath];
                                     AVAudioPlayer *player =[[AVAudioPlayer alloc] initWithData:data error:NULL];

                                     NSUInteger len = [player.data length];
                                     int numChannels = player.numberOfChannels;

                                     soundtouch::SAMPLETYPE sampleBuffer[1024];

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


                                     for (NSUInteger i = 0; i <= len - 1024; i = i + 1024) {

                                         NSRange r = NSMakeRange(i, 1024);
                                         //NSData *temp = [player.data subdataWithRange:r];
                                         [player.data getBytes:sampleBuffer range:r];

                                         int samples = sizeof(sampleBuffer) / numChannels;

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

                                     }

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


                                 }];

奇妙なことに、計算された BMP は常に同じ値です。

2013-10-02 03:05:36.725 AppTestAudio[1464:1803]  Beats Per Minute = 117.453835

どのトラックがフレーム数またはバッファ サイズであったかに関係なく (ここでは、ライブラリのソース コードの SoundTouch の例として 2K のバッファ サイズを使用しました)。

于 2013-10-02T01:12:10.803 に答える