From 1efa5cee1b563976c614edbc60db8fbccfa5cf4b Mon Sep 17 00:00:00 2001 From: Yuuki Nishiyama Date: Thu, 13 Jun 2019 08:26:23 +0300 Subject: [PATCH] Add AWAREAmbientNoiseFFTDelegate into AmbientNoise plugin --- AWAREFramework.podspec | 2 +- .../Plugins/AmbientNoise/AmbientNoise.h | 29 ++++++++++++++++++- .../Plugins/AmbientNoise/AmbientNoise.m | 11 ++++++- .../AWARE-AmbientNoise/ViewController.swift | 18 ++++++++---- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/AWAREFramework.podspec b/AWAREFramework.podspec index b115c25..7cd4d41 100644 --- a/AWAREFramework.podspec +++ b/AWAREFramework.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'AWAREFramework' - s.version = '1.4.14' + s.version = '1.4.15' s.summary = 'AWARE: An Open-source Context Instrumentation Framework' # This description is used to generate tags and improve search results. diff --git a/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.h b/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.h index ec5665b..80d7e88 100755 --- a/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.h +++ b/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.h @@ -33,6 +33,31 @@ extern NSString * const AWARE_PREFERENCES_PLUGIN_AMBIENT_NOISE_SAMPLE_SIZE; /** Silence threshold (default = 50) in dB */ extern NSString * const AWARE_PREFERENCES_PLUGIN_AMBIENT_NOISE_SILENCE_THRESHOLD; +/** + The EZAudioFFTDelegate provides event callbacks for the EZAudioFFT (and subclasses such as the EZAudioFFTRolling) whenvever the FFT is computed. + */ +@protocol AWAREAmbientNoiseFFTDelegate + +@optional + +///----------------------------------------------------------- +/// @name Getting FFT Output Data +///----------------------------------------------------------- + +/** + Triggered when the EZAudioFFT computes an FFT from a buffer of input data. Provides an array of float data representing the computed FFT. + @param fft The EZAudioFFT instance that triggered the event. + @param fftData A float pointer representing the float array of FFT data. + @param bufferSize A vDSP_Length (unsigned long) representing the length of the float array. + */ +- (void) fft:(EZAudioFFT *)fft + updatedWithFFTData:(float *)fftData + bufferSize:(vDSP_Length)bufferSize; + +@end + + + @interface AmbientNoise : AWARESensor // // The microphone component @@ -54,6 +79,8 @@ extern NSString * const AWARE_PREFERENCES_PLUGIN_AMBIENT_NOISE_SILENCE_THRESHOLD // @property (nonatomic, assign, readonly) BOOL isRecording; +@property (nonatomic, weak) id fftDelegate; + @property int frequencyMin; @property int sampleSize; @property double sampleDuration; @@ -64,7 +91,7 @@ extern NSString * const AWARE_PREFERENCES_PLUGIN_AMBIENT_NOISE_SILENCE_THRESHOLD - (BOOL) startSensor; -typedef void (^AudioFileGenerationHandler)(NSURL * fileURL); +typedef void (^AudioFileGenerationHandler)(NSURL * _Nullable fileURL); - (void) setAudioFileGenerationHandler:(AudioFileGenerationHandler __nullable)handler; diff --git a/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.m b/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.m index ae05229..a882551 100755 --- a/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.m +++ b/AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.m @@ -621,7 +621,16 @@ - (void) fft:(EZAudioFFT *)fft bufferSize:(vDSP_Length)bufferSize { maxFrequency = [fft maxFrequency]; - // NSLog(@"%f", maxFrequency); + + if ([self.fftDelegate respondsToSelector:@selector(fft:updatedWithFFTData:bufferSize:)]) { + [self.fftDelegate fft:fft updatedWithFFTData:fftData bufferSize:bufferSize]; + } + +// for (int i = 0; i 0.01) { +// NSLog(@"fft val [%d] %f", i, fftData[i]); +// } +// } // [self setLatestValue:[NSString stringWithFormat:@"dB:%f, RMS:%f, Frequency:%f", db, rms, maxFrequency]]; } diff --git a/Example/AWARE-AmbientNoise/ViewController.swift b/Example/AWARE-AmbientNoise/ViewController.swift index fda847b..3365122 100644 --- a/Example/AWARE-AmbientNoise/ViewController.swift +++ b/Example/AWARE-AmbientNoise/ViewController.swift @@ -38,16 +38,12 @@ class ViewController: UIViewController { noise.sampleDuration = 10 noise.sampleSize = 60 noise.startSensor() -// noise.setSensorEventHandler { (sensor, data) in -// if let d = data { -// print(d) -// } -// } noise.setAudioFileGenerationHandler { (url) in if let url = url { self.recognizeFile(url: url) } } + noise.fftDelegate = self } func recognizeFile(url:URL) { @@ -77,3 +73,15 @@ class ViewController: UIViewController { } +extension ViewController: AWAREAmbientNoiseFFTDelegate { + func fft(_ fft: EZAudioFFT!, updatedWithFFTData fftData: UnsafeMutablePointer!, bufferSize: vDSP_Length) { + if let data = fftData { + for i in 0.. 0.01 { + print(i,data[i]) + } + } + } + } +} +