-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add audio listener, update api
- Loading branch information
1 parent
6415490
commit 52b24b5
Showing
9 changed files
with
245 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.pitchy"> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pitchy"> | ||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,84 @@ | ||
#import "Pitchy.h" | ||
#import <AVFoundation/AVFoundation.h> | ||
|
||
@implementation Pitchy { | ||
AVAudioEngine *audioEngine; | ||
double sampleRate; | ||
double minVolume; | ||
BOOL isRecording; | ||
BOOL isInitialized; | ||
} | ||
|
||
@implementation Pitchy | ||
RCT_EXPORT_MODULE() | ||
|
||
// Method to expose autoCorrelate | ||
RCT_EXPORT_METHOD(autoCorrelate:(NSArray<NSNumber *> *)buf | ||
sampleRate:(double)sampleRate | ||
resolve:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) | ||
{ | ||
// Convert NSArray<NSNumber *> to std::vector<double> | ||
std::vector<double> cBuf; | ||
for (NSNumber *num in buf) { | ||
cBuf.push_back([num doubleValue]); | ||
- (NSArray<NSString *> *)supportedEvents { | ||
return @[@"onPitchDetected"]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(init:(NSDictionary *)config) { | ||
if (!isInitialized) { | ||
audioEngine = [[AVAudioEngine alloc] init]; | ||
AVAudioInputNode *inputNode = [audioEngine inputNode]; | ||
|
||
AVAudioFormat *format = [inputNode inputFormatForBus:0]; | ||
sampleRate = format.sampleRate; | ||
minVolume = [config[@"minVolume"] doubleValue]; | ||
|
||
[inputNode installTapOnBus:0 bufferSize:[config[@"bufferSize"] unsignedIntValue] format:format block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) { | ||
[self detectPitch:buffer]; | ||
}]; | ||
|
||
isInitialized = YES; | ||
} | ||
} | ||
|
||
// Call the autoCorrelate function | ||
double result = pitchy::autoCorrelate(cBuf, sampleRate); | ||
RCT_EXPORT_METHOD(isRecording:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) { | ||
resolve([NSNumber numberWithBool:isRecording]); | ||
} | ||
|
||
resolve([NSNumber numberWithDouble:result]); | ||
RCT_EXPORT_METHOD(start:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) { | ||
if (!isInitialized) { | ||
reject(@"not_initialized", @"Pitchy module is not initialized", nil); | ||
return; | ||
} | ||
|
||
if(isRecording){ | ||
reject(@"already_recording", @"Already recording", nil); | ||
return; | ||
} | ||
|
||
NSError *error = nil; | ||
[audioEngine startAndReturnError:&error]; | ||
if (error) { | ||
reject(@"start_error", @"Failed to start audio engine", error); | ||
} else { | ||
isRecording = YES; | ||
resolve(@(YES)); | ||
} | ||
} | ||
|
||
// Method to expose calculateVolume | ||
RCT_EXPORT_METHOD(calculateVolume:(NSArray<NSNumber *> *)buf | ||
resolve:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) | ||
{ | ||
// Convert NSArray<NSNumber *> to std::vector<double> | ||
std::vector<double> cBuf; | ||
for (NSNumber *num in buf) { | ||
cBuf.push_back([num doubleValue]); | ||
RCT_EXPORT_METHOD(stop:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) { | ||
|
||
if (!isRecording) { | ||
reject(@"not_recording", @"Not recording", nil); | ||
return; | ||
} | ||
|
||
// Call the calculateVolume function | ||
double result = pitchy::calculateVolume(cBuf); | ||
[audioEngine stop]; | ||
isRecording = NO; | ||
resolve(@(YES)); | ||
} | ||
|
||
- (void)detectPitch:(AVAudioPCMBuffer *)buffer { | ||
float *channelData = buffer.floatChannelData[0]; | ||
std::vector<double> buf(channelData, channelData + buffer.frameLength); | ||
|
||
resolve([NSNumber numberWithDouble:result]); | ||
double detectedPitch = pitchy::autoCorrelate(buf, sampleRate, minVolume); | ||
|
||
[self sendEventWithName:@"onPitchDetected" body:@{@"pitch": @(detectedPitch)}]; | ||
} | ||
|
||
@end |
Oops, something went wrong.