-
-
Notifications
You must be signed in to change notification settings - Fork 32
Audio stretcher module #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ce2052f
feat: add AudioStretcher draft
vltkv 663d301
refactor: add audio stretcher module
vltkv cb27e43
refactor: increase stretchedBuffer size
vltkv 10b630f
fix: audiostretcher
vltkv 3a49f72
Merge branch 'audio-decoding-module' into audio-stretcher-module
vltkv 529c4bb
fix: hostobjects fixes
vltkv e13e2ae
Merge branch 'audio-decoding-module' into audio-stretcher-module
vltkv feae46a
docs: add time-stretching desc
vltkv fc531c5
feat: audio stretcher
vltkv 4383078
fix: remove unnecessary sampleRate field
vltkv 433766b
fix: apply suggestions
vltkv 88151fd
Merge branch 'audio-decoding-module' into audio-stretcher-module
vltkv 8072594
refactor: use createAsyncPromise
vltkv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
sidebar_position: 2 | ||
--- | ||
|
||
import { Optional, MobileOnly } from '@site/src/components/Badges'; | ||
|
||
# Time stretching | ||
|
||
You can change the playback speed of an audio buffer independently, without creating an AudioContext, using the exported function [`changePlaybackSpeed`](/docs/utils/decoding#decodeaudiodata). | ||
|
||
### `changePlaybackSpeed` | ||
|
||
Changes the playback speed of an audio buffer. | ||
|
||
| Parameter | Type | Description | | ||
| :----: | :----: | :-------- | | ||
| `input` | `AudioBuffer` | The audio buffer whose playback speed you want to change. | | ||
| `playbackSpeed` | `number` | The factor by which to change the playback speed. Values between [1.0, 2.0] speed up playback, values between [0.5, 1.0] slow it down. | | ||
|
||
|
||
#### Returns `Promise<AudioBuffer>`. | ||
|
||
<details> | ||
<summary>Example usage</summary> | ||
```tsx | ||
const url = ... // url to an audio | ||
const sampleRate = 48000 | ||
|
||
const buffer = await fetch(url) | ||
.then((response) => response.arrayBuffer()) | ||
.then((arrayBuffer) => decodeAudioData(arrayBuffer, sampleRate)) | ||
.then((audioBuffer) => changePlaybackSpeed(audioBuffer, 1.25)) | ||
.catch((error) => { | ||
console.error('Error decoding audio data source:', error); | ||
return null; | ||
}); | ||
``` | ||
</details> | ||
|
74 changes: 74 additions & 0 deletions
74
...eact-native-audio-api/android/src/main/cpp/audioapi/android/core/utils/AudioStretcher.cpp
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <audioapi/core/sources/AudioBuffer.h> | ||
#include <audioapi/core/utils/AudioStretcher.h> | ||
#include <audioapi/libs/audio-stretch/stretch.h> | ||
#include <audioapi/utils/AudioArray.h> | ||
#include <audioapi/utils/AudioBus.h> | ||
#include <cstdint> | ||
|
||
namespace audioapi { | ||
|
||
std::vector<int16_t> AudioStretcher::castToInt16Buffer(AudioBuffer &buffer) { | ||
const size_t numChannels = buffer.getNumberOfChannels(); | ||
const size_t numFrames = buffer.getLength(); | ||
|
||
std::vector<int16_t> int16Buffer(numFrames * numChannels); | ||
|
||
for (size_t ch = 0; ch < numChannels; ++ch) { | ||
const float *channelData = buffer.getChannelData(ch); | ||
for (size_t i = 0; i < numFrames; ++i) { | ||
int16Buffer[i * numChannels + ch] = floatToInt16(channelData[i]); | ||
} | ||
} | ||
|
||
return int16Buffer; | ||
} | ||
|
||
std::shared_ptr<AudioBuffer> AudioStretcher::changePlaybackSpeed( | ||
AudioBuffer buffer, | ||
float playbackSpeed) { | ||
const float sampleRate = buffer.getSampleRate(); | ||
const size_t outputChannels = buffer.getNumberOfChannels(); | ||
const size_t numFrames = buffer.getLength(); | ||
|
||
if (playbackSpeed == 1.0f) { | ||
return std::make_shared<AudioBuffer>(buffer); | ||
} | ||
|
||
std::vector<int16_t> int16Buffer = castToInt16Buffer(buffer); | ||
|
||
auto stretcher = stretch_init( | ||
static_cast<int>(sampleRate / 333.0f), | ||
static_cast<int>(sampleRate / 55.0f), | ||
outputChannels, | ||
0x1); | ||
|
||
int maxOutputFrames = stretch_output_capacity( | ||
stretcher, static_cast<int>(numFrames), 1 / playbackSpeed); | ||
std::vector<int16_t> stretchedBuffer(maxOutputFrames * outputChannels); | ||
|
||
int outputFrames = stretch_samples( | ||
stretcher, | ||
int16Buffer.data(), | ||
static_cast<int>(numFrames), | ||
stretchedBuffer.data(), | ||
1 / playbackSpeed); | ||
|
||
outputFrames += | ||
stretch_flush(stretcher, stretchedBuffer.data() + (outputFrames)); | ||
stretchedBuffer.resize(outputFrames * outputChannels); | ||
stretch_deinit(stretcher); | ||
|
||
auto audioBus = | ||
std::make_shared<AudioBus>(outputFrames, outputChannels, sampleRate); | ||
|
||
for (int ch = 0; ch < outputChannels; ++ch) { | ||
auto channelData = audioBus->getChannel(ch)->getData(); | ||
for (int i = 0; i < outputFrames; ++i) { | ||
channelData[i] = int16ToFloat(stretchedBuffer[i * outputChannels + ch]); | ||
} | ||
} | ||
|
||
return std::make_shared<AudioBuffer>(audioBus); | ||
} | ||
|
||
} // namespace audioapi |
poneciak57 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
60 changes: 60 additions & 0 deletions
60
...react-native-audio-api/common/cpp/audioapi/HostObjects/utils/AudioStretcherHostObject.cpp
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
|
||
#include <audioapi/HostObjects/sources/AudioBufferHostObject.h> | ||
#include <audioapi/HostObjects/utils/AudioStretcherHostObject.h> | ||
#include <audioapi/core/utils/AudioStretcher.h> | ||
#include <audioapi/jsi/JsiPromise.h> | ||
|
||
#include <jsi/jsi.h> | ||
#include <memory> | ||
#include <string> | ||
#include <thread> | ||
#include <utility> | ||
|
||
namespace audioapi { | ||
|
||
AudioStretcherHostObject::AudioStretcherHostObject( | ||
jsi::Runtime *runtime, | ||
const std::shared_ptr<react::CallInvoker> &callInvoker) { | ||
promiseVendor_ = std::make_shared<PromiseVendor>(runtime, callInvoker); | ||
poneciak57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stretcher_ = std::make_shared<AudioStretcher>(); | ||
addFunctions( | ||
JSI_EXPORT_FUNCTION(AudioStretcherHostObject, changePlaybackSpeed)); | ||
} | ||
|
||
JSI_HOST_FUNCTION_IMPL(AudioStretcherHostObject, changePlaybackSpeed) { | ||
auto audioBuffer = | ||
args[0].getObject(runtime).asHostObject<AudioBufferHostObject>(runtime); | ||
auto playbackSpeed = static_cast<float>(args[1].asNumber()); | ||
|
||
auto promise = promiseVendor_->createPromise( | ||
poneciak57 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
[this, audioBuffer, playbackSpeed](std::shared_ptr<Promise> promise) { | ||
std::thread([this, | ||
audioBuffer, | ||
playbackSpeed, | ||
promise = std::move(promise)]() { | ||
auto result = stretcher_->changePlaybackSpeed( | ||
*audioBuffer->audioBuffer_, playbackSpeed); | ||
|
||
if (!result) { | ||
promise->reject("Failed to change audio playback speed."); | ||
return; | ||
} | ||
|
||
auto audioBufferHostObject = | ||
std::make_shared<AudioBufferHostObject>(result); | ||
|
||
promise->resolve([audioBufferHostObject = std::move( | ||
audioBufferHostObject)](jsi::Runtime &runtime) { | ||
auto jsiObject = jsi::Object::createFromHostObject( | ||
runtime, audioBufferHostObject); | ||
jsiObject.setExternalMemoryPressure( | ||
runtime, audioBufferHostObject->getSizeInBytes()); | ||
return jsiObject; | ||
}); | ||
}).detach(); | ||
}); | ||
return promise; | ||
} | ||
|
||
} // namespace audioapi |
27 changes: 27 additions & 0 deletions
27
...s/react-native-audio-api/common/cpp/audioapi/HostObjects/utils/AudioStretcherHostObject.h
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <audioapi/HostObjects/sources/AudioBufferHostObject.h> | ||
#include <audioapi/core/utils/AudioStretcher.h> | ||
#include <audioapi/jsi/JsiPromise.h> | ||
|
||
#include <jsi/jsi.h> | ||
#include <memory> | ||
#include <string> | ||
#include <thread> | ||
#include <utility> | ||
|
||
namespace audioapi { | ||
using namespace facebook; | ||
|
||
class AudioStretcherHostObject : public JsiHostObject { | ||
public: | ||
explicit AudioStretcherHostObject( | ||
jsi::Runtime *runtime, | ||
const std::shared_ptr<react::CallInvoker> &callInvoker); | ||
poneciak57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
JSI_HOST_FUNCTION_DECL(changePlaybackSpeed); | ||
|
||
private: | ||
std::shared_ptr<AudioStretcher> stretcher_; | ||
std::shared_ptr<PromiseVendor> promiseVendor_; | ||
}; | ||
} // namespace audioapi |
1 change: 0 additions & 1 deletion
1
packages/react-native-audio-api/common/cpp/audioapi/core/utils/AudioDecoder.h
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
packages/react-native-audio-api/common/cpp/audioapi/core/utils/AudioStretcher.h
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
namespace audioapi { | ||
|
||
class AudioBus; | ||
class AudioBuffer; | ||
|
||
class AudioStretcher { | ||
public: | ||
explicit AudioStretcher() {} | ||
|
||
[[nodiscard]] static std::shared_ptr<AudioBuffer> changePlaybackSpeed( | ||
AudioBuffer buffer, | ||
float playbackSpeed); | ||
|
||
private: | ||
float sampleRate_; | ||
|
||
static std::vector<int16_t> castToInt16Buffer(AudioBuffer &buffer); | ||
|
||
[[nodiscard]] static inline int16_t floatToInt16(float sample) { | ||
return static_cast<int16_t>(sample * 32768.0f); | ||
} | ||
[[nodiscard]] static inline float int16ToFloat(int16_t sample) { | ||
return static_cast<float>(sample) / 32768.0f; | ||
poneciak57 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
} // namespace audioapi |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.