Skip to content

Commit 47d0d40

Browse files
author
Maciej Makowski
committed
fix: bring back Audiofile example
1 parent 46a3ce5 commit 47d0d40

File tree

3 files changed

+38
-59
lines changed

3 files changed

+38
-59
lines changed

apps/common-app/src/examples/AudioFile/AudioFile.tsx

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
AudioBuffer,
55
AudioContext,
66
AudioBufferSourceNode,
7-
AudioBufferStreamSourceNode,
87
AudioManager,
98
} from 'react-native-audio-api';
109

@@ -26,16 +25,13 @@ const AudioFile: FC = () => {
2625
const [isLoading, setIsLoading] = useState(false);
2726

2827
const [offset, setOffset] = useState(0);
29-
const [playbackRate, setPlaybackRate] = useState(INITIAL_RATE * 1.5);
28+
const [playbackRate, setPlaybackRate] = useState(INITIAL_RATE);
3029
const [detune, setDetune] = useState(INITIAL_DETUNE);
3130

32-
const [audioBuffers, setAudioBuffers] = useState<AudioBuffer[]>([]);
31+
const [audioBuffer, setAudioBuffer] = useState<AudioBuffer | null>(null);
3332

3433
const audioContextRef = useRef<AudioContext | null>(null);
3534
const bufferSourceRef = useRef<AudioBufferSourceNode | null>(null);
36-
const bufferStreamSourceRef = useRef<AudioBufferStreamSourceNode | null>(
37-
null
38-
);
3935

4036
const handlePlaybackRateChange = (newValue: number) => {
4137
setPlaybackRate(newValue);
@@ -59,28 +55,39 @@ const AudioFile: FC = () => {
5955
}
6056

6157
if (isPlaying) {
62-
bufferStreamSourceRef.current?.stop(audioContextRef.current.currentTime);
58+
bufferSourceRef.current?.stop(audioContextRef.current.currentTime);
6359
AudioManager.setLockScreenInfo({
6460
state: 'state_paused',
6561
});
6662
} else {
67-
if (!audioBuffers) {
63+
if (!audioBuffer) {
6864
fetchAudioBuffer();
6965
}
7066

71-
const now = audioContextRef.current.currentTime;
67+
AudioManager.setLockScreenInfo({
68+
state: 'state_playing',
69+
});
7270

73-
bufferStreamSourceRef.current =
74-
audioContextRef.current.createBufferStreamSource();
75-
for (let i = 0; i < audioBuffers.length; i++) {
76-
bufferStreamSourceRef.current.enqueueAudioBuffer(audioBuffers[i]);
77-
}
71+
AudioManager.observeAudioInterruptions(true);
7872

79-
bufferStreamSourceRef.current.connect(
80-
audioContextRef.current.destination
73+
bufferSourceRef.current = audioContextRef.current.createBufferSource({
74+
pitchCorrection: true,
75+
});
76+
bufferSourceRef.current.buffer = audioBuffer;
77+
bufferSourceRef.current.loop = true;
78+
bufferSourceRef.current.onended = (event) => {
79+
setOffset((_prev) => event.value || 0);
80+
};
81+
bufferSourceRef.current.loopStart = LOOP_START;
82+
bufferSourceRef.current.loopEnd = LOOP_END;
83+
bufferSourceRef.current.playbackRate.value = playbackRate;
84+
bufferSourceRef.current.detune.value = detune;
85+
bufferSourceRef.current.connect(audioContextRef.current.destination);
86+
87+
bufferSourceRef.current.start(
88+
audioContextRef.current.currentTime,
89+
offset
8190
);
82-
bufferStreamSourceRef.current.playbackRate.value = playbackRate;
83-
bufferStreamSourceRef.current.start(now);
8491
}
8592

8693
setIsPlaying((prev) => !prev);
@@ -99,37 +106,7 @@ const AudioFile: FC = () => {
99106
return null;
100107
});
101108

102-
if (!buffer) {
103-
setIsLoading(false);
104-
return;
105-
}
106-
107-
const data = buffer.getChannelData(0);
108-
const buffers: AudioBuffer[] = [];
109-
110-
for (let i = 0; i < 25; i++) {
111-
const buffer1 = audioContextRef.current!.createBuffer(
112-
buffer.numberOfChannels,
113-
buffer.sampleRate,
114-
buffer.sampleRate
115-
);
116-
117-
const channelData = buffer1.getChannelData(0);
118-
119-
for (let j = 0; j < buffer.sampleRate; j++) {
120-
channelData[j] = data[j + i * buffer.sampleRate];
121-
}
122-
123-
// if (i === 1) {
124-
// for (let j = 0; j < 1000; j++) {
125-
// console.log(channelData[j]);
126-
// }
127-
// }
128-
129-
buffers.push(buffer1);
130-
}
131-
132-
setAudioBuffers(buffers);
109+
setAudioBuffer(buffer);
133110

134111
setIsLoading(false);
135112
}, []);
@@ -194,7 +171,7 @@ const AudioFile: FC = () => {
194171
<Button
195172
title={isPlaying ? 'Stop' : 'Play'}
196173
onPress={handlePress}
197-
disabled={audioBuffers.length === 0}
174+
disabled={!audioBuffer}
198175
/>
199176
<Spacer.Vertical size={49} />
200177
<Slider

packages/react-native-audio-api/common/cpp/audioapi/core/sources/AudioBufferStreamSourceNode.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,22 @@ void AudioBufferStreamSourceNode::processWithPitchCorrection(
112112

113113
playbackRateBus_->zero();
114114

115-
updatePlaybackInfo(
116-
processingBus, framesToProcess, startOffset, offsetLength);
115+
updatePlaybackInfo(processingBus, framesToProcess, startOffset, offsetLength);
117116

118117
if (playbackRate == 0.0f || (!isPlaying() && !isStopScheduled())) {
119118
processingBus->zero();
120119
return;
121120
}
122121

123-
auto framesNeededToStretch =
124-
static_cast<int>(playbackRate * static_cast<float>(framesToProcess));
125-
auto stretchedStartOffset = static_cast<size_t>(static_cast<float>(startOffset) * playbackRate);
126-
auto stretchedOffsetLength = static_cast<size_t>(static_cast<float>(offsetLength) * playbackRate);
122+
auto framesNeededToStretch =
123+
static_cast<int>(playbackRate * static_cast<float>(framesToProcess));
124+
auto stretchedStartOffset =
125+
static_cast<size_t>(static_cast<float>(startOffset) * playbackRate);
126+
auto stretchedOffsetLength =
127+
static_cast<size_t>(static_cast<float>(offsetLength) * playbackRate);
127128

128-
processWithoutInterpolation(playbackRateBus_, stretchedStartOffset, stretchedOffsetLength);
129+
processWithoutInterpolation(
130+
playbackRateBus_, stretchedStartOffset, stretchedOffsetLength);
129131

130132
stretch_->process(
131133
playbackRateBus_.get()[0],
@@ -172,7 +174,7 @@ void AudioBufferStreamSourceNode::processWithoutInterpolation(
172174
readIndex = 0;
173175

174176
if (isLastBuffer_) {
175-
playbackState_ = PlaybackState::STOP_SCHEDULED;
177+
playbackState_ = PlaybackState::STOP_SCHEDULED;
176178
}
177179
break;
178180
} else {

packages/react-native-audio-api/common/cpp/audioapi/core/sources/AudioBufferStreamSourceNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AudioBufferStreamSourceNode : public AudioScheduledSourceNode {
4646
// internal helper
4747
double vReadIndex_;
4848

49-
// User provided buffer
49+
// User provided buffers
5050
std::queue<std::shared_ptr<AudioBuffer>> buffers_;
5151
bool isLastBuffer_ = false;
5252

0 commit comments

Comments
 (0)