Skip to content

Commit

Permalink
Add AWAREAmbientNoiseFFTDelegate into AmbientNoise plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuuki Nishiyama committed Jun 13, 2019
1 parent 8c24353 commit 1efa5ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion AWAREFramework.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 28 additions & 1 deletion AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NSObject>

@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 <AWARESensorDelegate, EZMicrophoneDelegate, EZRecorderDelegate, EZAudioFFTDelegate, CXCallObserverDelegate>
//
// The microphone component
Expand All @@ -54,6 +79,8 @@ extern NSString * const AWARE_PREFERENCES_PLUGIN_AMBIENT_NOISE_SILENCE_THRESHOLD
//
@property (nonatomic, assign, readonly) BOOL isRecording;

@property (nonatomic, weak) id<AWAREAmbientNoiseFFTDelegate> fftDelegate;

@property int frequencyMin;
@property int sampleSize;
@property double sampleDuration;
Expand All @@ -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;

Expand Down
11 changes: 10 additions & 1 deletion AWAREFramework/Classes/Plugins/AmbientNoise/AmbientNoise.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<bufferSize; i++) {
// if (fftData[i] > 0.01) {
// NSLog(@"fft val [%d] %f", i, fftData[i]);
// }
// }
// [self setLatestValue:[NSString stringWithFormat:@"dB:%f, RMS:%f, Frequency:%f", db, rms, maxFrequency]];
}

Expand Down
18 changes: 13 additions & 5 deletions Example/AWARE-AmbientNoise/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -77,3 +73,15 @@ class ViewController: UIViewController {

}

extension ViewController: AWAREAmbientNoiseFFTDelegate {
func fft(_ fft: EZAudioFFT!, updatedWithFFTData fftData: UnsafeMutablePointer<Float>!, bufferSize: vDSP_Length) {
if let data = fftData {
for i in 0..<Int(bufferSize){
if data[i] > 0.01 {
print(i,data[i])
}
}
}
}
}

0 comments on commit 1efa5ce

Please sign in to comment.