react-native-audio-api 0.6.0-rc.2 → 0.6.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,7 +28,9 @@ IOSAudioRecorder::IOSAudioRecorder(
28
28
  }
29
29
  };
30
30
 
31
- audioRecorder_ = [[NativeAudioRecorder alloc] initWithReceiverBlock:audioReceiverBlock bufferLength:bufferLength];
31
+ audioRecorder_ = [[NativeAudioRecorder alloc] initWithReceiverBlock:audioReceiverBlock
32
+ bufferLength:bufferLength
33
+ sampleRate:sampleRate];
32
34
  }
33
35
 
34
36
  IOSAudioRecorder::~IOSAudioRecorder()
@@ -8,13 +8,19 @@ typedef void (^AudioReceiverBlock)(const AudioBufferList *inputBuffer, int numFr
8
8
  @interface NativeAudioRecorder : NSObject
9
9
 
10
10
  @property (nonatomic, assign) int bufferLength;
11
- @property (nonatomic, assign) double sampleRate;
11
+ @property (nonatomic, assign) float sampleRate;
12
12
 
13
13
  @property (nonatomic, strong) AVAudioSinkNode *sinkNode;
14
14
  @property (nonatomic, copy) AVAudioSinkNodeReceiverBlock receiverSinkBlock;
15
15
  @property (nonatomic, copy) AudioReceiverBlock receiverBlock;
16
16
 
17
- - (instancetype)initWithReceiverBlock:(AudioReceiverBlock)receiverBlock bufferLength:(int)bufferLength;
17
+ @property (nonatomic, strong) AVAudioConverter *audioConverter;
18
+ @property (nonatomic, strong) AVAudioFormat *inputFormat;
19
+ @property (nonatomic, strong) AVAudioFormat *outputFormat;
20
+
21
+ - (instancetype)initWithReceiverBlock:(AudioReceiverBlock)receiverBlock
22
+ bufferLength:(int)bufferLength
23
+ sampleRate:(float)sampleRate;
18
24
 
19
25
  - (void)start;
20
26
 
@@ -4,23 +4,34 @@
4
4
 
5
5
  @implementation NativeAudioRecorder
6
6
 
7
- - (instancetype)initWithReceiverBlock:(AudioReceiverBlock)receiverBlock bufferLength:(int)bufferLength
7
+ - (instancetype)initWithReceiverBlock:(AudioReceiverBlock)receiverBlock
8
+ bufferLength:(int)bufferLength
9
+ sampleRate:(float)sampleRate
8
10
  {
9
11
  if (self = [super init]) {
10
12
  self.bufferLength = bufferLength;
11
- self.sampleRate = [[[AudioSessionManager sharedInstance] getDevicePreferredSampleRate] doubleValue];
13
+ self.sampleRate = sampleRate;
12
14
 
13
15
  self.receiverBlock = [receiverBlock copy];
14
16
 
17
+ float devicePrefferedSampleRate = [[[AudioSessionManager sharedInstance] getDevicePreferredSampleRate] floatValue];
18
+
19
+ self.inputFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
20
+ sampleRate:devicePrefferedSampleRate
21
+ channels:1
22
+ interleaved:NO];
23
+ self.outputFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
24
+ sampleRate:sampleRate
25
+ channels:1
26
+ interleaved:NO];
27
+ self.audioConverter = [[AVAudioConverter alloc] initFromFormat:self.inputFormat toFormat:self.outputFormat];
28
+
15
29
  __weak typeof(self) weakSelf = self;
16
30
  self.receiverSinkBlock = ^OSStatus(
17
31
  const AudioTimeStamp *_Nonnull timestamp,
18
32
  AVAudioFrameCount frameCount,
19
33
  const AudioBufferList *_Nonnull inputData) {
20
- AVAudioTime *time = [[AVAudioTime alloc] initWithAudioTimeStamp:timestamp sampleRate:weakSelf.sampleRate];
21
- weakSelf.receiverBlock(inputData, frameCount, time);
22
-
23
- return kAudioServicesNoError;
34
+ return [weakSelf processAudioInput:inputData withFrameCount:frameCount atTimestamp:timestamp];
24
35
  };
25
36
 
26
37
  self.sinkNode = [[AVAudioSinkNode alloc] initWithReceiverBlock:self.receiverSinkBlock];
@@ -29,6 +40,55 @@
29
40
  return self;
30
41
  }
31
42
 
43
+ - (OSStatus)processAudioInput:(const AudioBufferList *)inputData
44
+ withFrameCount:(AVAudioFrameCount)frameCount
45
+ atTimestamp:(const AudioTimeStamp *)timestamp
46
+ {
47
+ float inputSampleRate = self.inputFormat.sampleRate;
48
+ float outputSampleRate = self.outputFormat.sampleRate;
49
+
50
+ AVAudioTime *time = [[AVAudioTime alloc] initWithAudioTimeStamp:timestamp sampleRate:outputSampleRate];
51
+
52
+ if (inputSampleRate != outputSampleRate) {
53
+ AVAudioPCMBuffer *inputBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:self.inputFormat
54
+ frameCapacity:frameCount];
55
+
56
+ memcpy(
57
+ inputBuffer.mutableAudioBufferList->mBuffers[0].mData,
58
+ inputData->mBuffers[0].mData,
59
+ inputData->mBuffers[0].mDataByteSize);
60
+ inputBuffer.frameLength = frameCount;
61
+
62
+ int outputFrameCount = frameCount * outputSampleRate / inputSampleRate;
63
+
64
+ AVAudioPCMBuffer *outputBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:self.audioConverter.outputFormat
65
+ frameCapacity:outputFrameCount];
66
+
67
+ NSError *error = nil;
68
+ AVAudioConverterInputBlock inputBlock =
69
+ ^AVAudioBuffer *_Nullable(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus)
70
+ {
71
+ *outStatus = AVAudioConverterInputStatus_HaveData;
72
+ return inputBuffer;
73
+ };
74
+
75
+ [self.audioConverter convertToBuffer:outputBuffer error:&error withInputFromBlock:inputBlock];
76
+
77
+ if (error) {
78
+ NSLog(@"Error during audio conversion: %@", error.localizedDescription);
79
+ return kAudioServicesBadSpecifierSizeError;
80
+ }
81
+
82
+ self.receiverBlock(outputBuffer.audioBufferList, outputBuffer.frameLength, time);
83
+
84
+ return kAudioServicesNoError;
85
+ }
86
+
87
+ self.receiverBlock(inputData, frameCount, time);
88
+
89
+ return kAudioServicesNoError;
90
+ }
91
+
32
92
  - (void)start
33
93
  {
34
94
  [[AudioEngine sharedInstance] attachInputNode:self.sinkNode];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-api",
3
- "version": "0.6.0-rc.2",
3
+ "version": "0.6.0-rc.3",
4
4
  "description": "react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification",
5
5
  "bin": {
6
6
  "setup-rn-audio-api-web": "./scripts/setup-rn-audio-api-web.js"