-
-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add api to concatenate audio files #1045
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
Open
Traviskn
wants to merge
8
commits into
software-mansion:main
Choose a base branch
from
Traviskn:file-concatenator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3cf7c05
feat: add api to concatenate audio files
Traviskn 1f10806
refactor: address pr comments
Traviskn a1f7928
feat: support concatenating WAV and FLAC file formats
Traviskn 3b25743
fix: cocoapods installation from a packed/local tarball
Traviskn 0f5d336
refactor: address PR comments
Traviskn ab02def
feat: added concatenation to recording demo
mdydek 6337b85
fix: remove unsupported CAF file concatenation
Traviskn d8e1c2c
Merge branch 'file-concatenator' of github-personal:Traviskn/react-na…
Traviskn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| sidebar_label: File concatenation | ||
| --- | ||
|
|
||
| import { MobileOnly } from '@site/src/components/Badges'; | ||
|
|
||
| # File concatenation <MobileOnly /> | ||
|
|
||
| You can concatenate existing audio files without creating an `AudioContext` using | ||
| the exported [`concatAudioFiles`](#concataudiofiles) function. | ||
|
|
||
| This is useful when recording with the | ||
| [`rotateIntervalBytes`](/docs/inputs/audio-recorder#audiorecorderfileoptions) | ||
| option. Rotation keeps long recordings split into smaller files while recording, | ||
| and `concatAudioFiles` can join those files back into one file after the | ||
| recording is finished. | ||
|
|
||
| :::warning | ||
| `concatAudioFiles` is not supported on web. | ||
| ::: | ||
|
|
||
| :::caution | ||
| FFmpeg-backed formats require FFmpeg to be available in the native build. WAV | ||
| concatenation uses miniaudio and does not require FFmpeg. | ||
| ::: | ||
|
|
||
| ### `concatAudioFiles` | ||
|
|
||
| Concatenates compatible local audio files into a single output file. | ||
|
|
||
| | Parameter | Type | Description | | ||
| | :----------: | :--------: | :------------------------------------------------------------------------------------ | | ||
| | `inputPaths` | `string[]` | Local filesystem paths or `file://` URLs to concatenate, in output order. | | ||
| | `outputPath` | `string` | Local filesystem path or `file://` URL where the joined audio file should be written. | | ||
|
|
||
| #### Returns `Promise<string>`. | ||
|
|
||
| The promise resolves with the provided `outputPath` when concatenation | ||
| completes. | ||
|
|
||
| #### Compatibility | ||
|
|
||
| All input files must use compatible audio parameters. In practice, files should | ||
| come from the same recorder configuration: | ||
|
|
||
| - the same container format, | ||
| - the same audio codec, | ||
| - the same sample rate, | ||
| - the same channel layout, | ||
| - compatible codec parameters. | ||
|
|
||
| Use an output path with an extension that matches the input files, for example | ||
| joining `.m4a` segments into an `.m4a` output file or `.wav` segments into a | ||
| `.wav` output file. | ||
|
|
||
| <details> | ||
| <summary>Example usage with rotated recordings</summary> | ||
|
|
||
| ```tsx | ||
| import { AudioRecorder, FileDirectory, FileFormat, concatAudioFiles } from 'react-native-audio-api'; | ||
|
|
||
| const audioRecorder = new AudioRecorder(); | ||
|
|
||
| audioRecorder.enableFileOutput({ | ||
| directory: FileDirectory.Cache, | ||
| fileNamePrefix: 'meeting', | ||
| format: FileFormat.M4A, | ||
| rotateIntervalBytes: 8 * 1024 * 1024, | ||
| }); | ||
|
|
||
| // Start and stop the recorder as usual. | ||
| const result = audioRecorder.stop(); | ||
|
|
||
| if (result.status === 'success') { | ||
| const outputPath = result.paths[0].replace(/[^/]+$/, 'meeting-complete.m4a'); | ||
|
|
||
| await concatAudioFiles(result.paths, outputPath); | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary>Example usage with explicit paths</summary> | ||
|
|
||
| ```tsx | ||
| import { concatAudioFiles } from 'react-native-audio-api'; | ||
|
|
||
| const outputPath = '/path/to/session.m4a'; | ||
|
|
||
| await concatAudioFiles( | ||
| ['/path/to/session-001.m4a', '/path/to/session-002.m4a', '/path/to/session-003.m4a'], | ||
| outputPath | ||
| ); | ||
| ``` | ||
|
|
||
| </details> | ||
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
60 changes: 60 additions & 0 deletions
60
...react-native-audio-api/common/cpp/audioapi/HostObjects/utils/AudioFileUtilsHostObject.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 @@ | ||
| #include <audioapi/HostObjects/utils/AudioFileUtilsHostObject.h> | ||
| #include <audioapi/core/utils/AudioFileConcatenator.h> | ||
| #include <audioapi/jsi/JsiPromise.h> | ||
|
|
||
| #include <jsi/jsi.h> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace audioapi { | ||
|
|
||
| AudioFileUtilsHostObject::AudioFileUtilsHostObject( | ||
| jsi::Runtime *runtime, | ||
| const std::shared_ptr<react::CallInvoker> &callInvoker) { | ||
| promiseVendor_ = std::make_shared<PromiseVendor>(runtime, callInvoker); | ||
| addFunctions(JSI_EXPORT_FUNCTION(AudioFileUtilsHostObject, concatAudioFiles)); | ||
| } | ||
|
|
||
| JSI_HOST_FUNCTION_IMPL(AudioFileUtilsHostObject, concatAudioFiles) { | ||
| if (count < 2 || !args[0].isObject() || !args[1].isString()) { | ||
| throw jsi::JSError(runtime, "concatAudioFiles expects input paths and an output path."); | ||
| } | ||
|
|
||
| auto inputPathArray = args[0].asObject(runtime).asArray(runtime); | ||
| const auto inputPathCount = inputPathArray.size(runtime); | ||
| std::vector<std::string> inputPaths; | ||
| inputPaths.reserve(inputPathCount); | ||
|
|
||
| for (size_t i = 0; i < inputPathCount; ++i) { | ||
| auto value = inputPathArray.getValueAtIndex(runtime, i); | ||
| if (!value.isString()) { | ||
| throw jsi::JSError(runtime, "concatAudioFiles input paths must be strings."); | ||
| } | ||
| inputPaths.push_back(value.asString(runtime).utf8(runtime)); | ||
| } | ||
|
|
||
| auto outputPath = args[1].asString(runtime).utf8(runtime); | ||
|
|
||
| auto promise = promiseVendor_->createAsyncPromise( | ||
| [inputPaths = std::move(inputPaths), outputPath]() -> PromiseResolver { | ||
| auto result = audioapi::concatAudioFiles(inputPaths, outputPath); | ||
|
|
||
| if (result.is_err()) { | ||
| return [result = std::move(result)]( | ||
| jsi::Runtime &runtime) -> std::variant<jsi::Value, std::string> { | ||
| return result.unwrap_err(); | ||
| }; | ||
| } | ||
|
|
||
| return [outputPath = std::move(outputPath)]( | ||
| jsi::Runtime &runtime) -> std::variant<jsi::Value, std::string> { | ||
| return jsi::String::createFromUtf8(runtime, outputPath); | ||
| }; | ||
| }); | ||
|
|
||
| return promise; | ||
| } | ||
|
|
||
| } // namespace audioapi |
24 changes: 24 additions & 0 deletions
24
...s/react-native-audio-api/common/cpp/audioapi/HostObjects/utils/AudioFileUtilsHostObject.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,24 @@ | ||
| #pragma once | ||
|
|
||
| #include <audioapi/jsi/JsiHostObject.h> | ||
| #include <audioapi/jsi/JsiPromise.h> | ||
|
|
||
| #include <jsi/jsi.h> | ||
| #include <memory> | ||
|
|
||
| namespace audioapi { | ||
| using namespace facebook; | ||
|
|
||
| class AudioFileUtilsHostObject : public JsiHostObject { | ||
| public: | ||
| explicit AudioFileUtilsHostObject( | ||
| jsi::Runtime *runtime, | ||
| const std::shared_ptr<react::CallInvoker> &callInvoker); | ||
|
|
||
| JSI_HOST_FUNCTION_DECL(concatAudioFiles); | ||
|
|
||
| private: | ||
| std::shared_ptr<PromiseVendor> promiseVendor_; | ||
| }; | ||
|
|
||
| } // namespace audioapi |
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
Oops, something went wrong.
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.