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

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.
@@ -154,9 +154,10 @@ void AnalyserNode::processNode(
154
154
  // Down mix the input bus to mono
155
155
  downMixBus_->copy(processingBus.get());
156
156
 
157
+ auto framesToCopy = 0;
158
+
157
159
  if (vWriteIndex_ + framesToProcess > inputBuffer_->getSize()) {
158
- auto framesToCopy =
159
- static_cast<int>(inputBuffer_->getSize()) - vWriteIndex_;
160
+ framesToCopy = static_cast<int>(inputBuffer_->getSize()) - vWriteIndex_;
160
161
  memcpy(
161
162
  inputBuffer_->getData() + vWriteIndex_,
162
163
  downMixBus_->getChannel(0)->getData(),
@@ -168,7 +169,7 @@ void AnalyserNode::processNode(
168
169
 
169
170
  memcpy(
170
171
  inputBuffer_->getData() + vWriteIndex_,
171
- downMixBus_->getChannel(0)->getData(),
172
+ downMixBus_->getChannel(0)->getData() + framesToCopy,
172
173
  framesToProcess * sizeof(float));
173
174
 
174
175
  vWriteIndex_ += framesToProcess;
@@ -31,6 +31,10 @@ class IOSAudioRecorder : public AudioRecorder {
31
31
  private:
32
32
  NativeAudioRecorder *audioRecorder_;
33
33
  std::atomic<bool> isRunning_;
34
+
35
+ std::shared_ptr<AudioBus> circularBuffer_;
36
+ int writeIdx_ = 0;
37
+ int readIdx_ = 0;
34
38
  };
35
39
 
36
40
  } // namespace audioapi
@@ -16,15 +16,59 @@ IOSAudioRecorder::IOSAudioRecorder(
16
16
  const std::function<void(std::shared_ptr<AudioBus>, int, double)> &onAudioReady)
17
17
  : AudioRecorder(sampleRate, bufferLength, onError, onStatusChange, onAudioReady)
18
18
  {
19
+ circularBuffer_ = std::make_shared<AudioBus>(std::max(2 * bufferLength, 2048), 1, sampleRate);
20
+
19
21
  AudioReceiverBlock audioReceiverBlock = ^(const AudioBufferList *inputBuffer, int numFrames, AVAudioTime *when) {
20
22
  if (isRunning_.load()) {
21
- auto bus = std::make_shared<AudioBus>(numFrames, 1, sampleRate);
22
-
23
+ // copying to circularBuffer_
24
+ auto *circularBufferChannel = circularBuffer_->getChannel(0)->getData();
23
25
  auto *inputChannel = (float *)inputBuffer->mBuffers[0].mData;
24
- auto *outputChannel = bus->getChannel(0)->getData();
25
26
 
26
- memcpy(outputChannel, inputChannel, numFrames * sizeof(float));
27
- onAudioReady_(bus, numFrames, [when sampleTime] / [when sampleRate]);
27
+ auto framesToProcess = numFrames;
28
+ auto framesToCopy = 0;
29
+
30
+ if (writeIdx_ + numFrames > circularBuffer_->getSize()) {
31
+ framesToCopy = circularBuffer_->getSize() - writeIdx_;
32
+ memcpy(circularBufferChannel + writeIdx_, inputChannel, framesToCopy * sizeof(float));
33
+ framesToProcess -= framesToCopy;
34
+ writeIdx_ = 0;
35
+ }
36
+
37
+ memcpy(circularBufferChannel + writeIdx_, inputChannel + framesToCopy, framesToProcess * sizeof(float));
38
+
39
+ writeIdx_ += framesToProcess;
40
+ if (writeIdx_ >= circularBuffer_->getSize()) {
41
+ writeIdx_ = 0;
42
+ }
43
+
44
+ // copying to output bus and invoking callback
45
+ auto availableFrames =
46
+ writeIdx_ >= readIdx_ ? writeIdx_ - readIdx_ : circularBuffer_->getSize() - (readIdx_ - writeIdx_);
47
+
48
+ while (availableFrames >= bufferLength_) {
49
+ auto bus = std::make_shared<AudioBus>(bufferLength_, 1, sampleRate_);
50
+ auto *outputChannel = bus->getChannel(0)->getData();
51
+
52
+ framesToProcess = bufferLength_;
53
+ framesToCopy = 0;
54
+ if (readIdx_ + bufferLength_ > circularBuffer_->getSize()) {
55
+ framesToCopy = circularBuffer_->getSize() - readIdx_;
56
+ memcpy(outputChannel, circularBufferChannel + readIdx_, framesToCopy * sizeof(float));
57
+ framesToProcess -= framesToCopy;
58
+ readIdx_ = 0;
59
+ }
60
+
61
+ memcpy(outputChannel + framesToCopy, circularBufferChannel + readIdx_, framesToProcess * sizeof(float));
62
+
63
+ readIdx_ += framesToProcess;
64
+ if (readIdx_ >= circularBuffer_->getSize()) {
65
+ readIdx_ = 0;
66
+ }
67
+
68
+ onAudioReady_(bus, bufferLength_, [when sampleTime] / [when sampleRate]);
69
+
70
+ availableFrames -= bufferLength_;
71
+ }
28
72
  }
29
73
  };
30
74
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-api",
3
- "version": "0.6.0-rc.3",
3
+ "version": "0.6.0-rc.4",
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"