2013-07-20 14 views
5

Ho cercato su tutto il Web e non riesco a trovare un tutorial su come usare lo SoundTouch library per il rilevamento dei battiti.iOS SoundTouch framework BPM Esempio di rilevamento

(Nota:... Non ho C esperienza ++ prima di questo io lo so C, Objective-C e Java Così ho potuto incasinato po 'di questo, ma si compila)

ho aggiunto il framework al mio progetto e sono riusciti a ottenere i seguenti per compilare:

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 

le informazioni di byte inputSamples(*samples, numSamples) canzone mi confonde.

Come posso ottenere queste informazioni da un file di brano?

Ho provato a utilizzare memcpy() ma non sembra funzionare.

Qualcuno ha qualche idea?

risposta

1

Dopo ore e ore di debugging e la lettura della documentazione limitata sul web, ho modificato alcune cose prima di inciampare su questo: è necessario dividere numSamples dal numberOfChannels nella funzione inputSamples().

Il mio codice finale è in questo modo:

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()); 
+0

cool puoi dire come hai aggiunto la libreria SoundTouch al progetto xcode? – otakuProgrammer

+0

Basta scaricare il framework e importarlo nel progetto. – MrHappyAsthma

+0

ok sono riuscito a includerlo nel progetto xcode, il codice sopra non funzionerà, alcune volte l'app si blocca, ho provato entrambi i file wav e mp3, ho anche usato il campione audio orig dal sito web soundtouch, mi dà un crash problema, o risultato nan (quando si utilizza l'audio campione originale) – otakuProgrammer

0

ho provato questa soluzione per leggere la BPM da file mp3 (utilizzando la classe TSLibraryImport da convertire in WAV) all'interno della Music Library iOS:

       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()); 


           }]; 

la stranezza è che la calcolata BMP è sempre lo stesso valore:

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

No importa quale traccia era il numero di fotogrammi o la dimensione del buffer (qui ho usato la dimensione del buffer 2K come per l'esempio SoundTouch nel codice sorgente della libreria).

0

Per Swift 3:

https://github.com/Luccifer/BPM-Analyser

e usarlo come:

guard let filePath = Bundle.main.path(forResource: "TestMusic", ofType: "m4a"), 
let url = URL(string: filePath) else {return "error occured, check fileURL"} 

BPMAnalyzer.core.getBpmFrom(url, completion: nil) 

Sentitevi liberi di commentare!

Problemi correlati