react-native-audio-concat 0.2.0
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.
- package/AudioConcat.podspec +30 -0
- package/LICENSE +20 -0
- package/README.md +39 -0
- package/android/CMakeLists.txt +24 -0
- package/android/build.gradle +128 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/audioconcat/AudioConcat.kt +349 -0
- package/android/src/main/java/com/margelo/nitro/audioconcat/AudioConcatPackage.kt +22 -0
- package/ios/AudioConcat.swift +75 -0
- package/lib/module/AudioConcat.nitro.js +4 -0
- package/lib/module/AudioConcat.nitro.js.map +1 -0
- package/lib/module/index.js +33 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/AudioConcat.nitro.d.ts +16 -0
- package/lib/typescript/src/AudioConcat.nitro.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +28 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/nitro.json +17 -0
- package/nitrogen/generated/android/audioconcat+autolinking.cmake +82 -0
- package/nitrogen/generated/android/audioconcat+autolinking.gradle +27 -0
- package/nitrogen/generated/android/audioconcatOnLoad.cpp +44 -0
- package/nitrogen/generated/android/audioconcatOnLoad.hpp +25 -0
- package/nitrogen/generated/android/c++/JAudioData.hpp +53 -0
- package/nitrogen/generated/android/c++/JAudioDataOrSilence.cpp +26 -0
- package/nitrogen/generated/android/c++/JAudioDataOrSilence.hpp +72 -0
- package/nitrogen/generated/android/c++/JHybridAudioConcatSpec.cpp +77 -0
- package/nitrogen/generated/android/c++/JHybridAudioConcatSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JSilentData.hpp +53 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audioconcat/AudioData.kt +29 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audioconcat/AudioDataOrSilence.kt +42 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audioconcat/HybridAudioConcatSpec.kt +52 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audioconcat/SilentData.kt +29 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audioconcat/audioconcatOnLoad.kt +35 -0
- package/nitrogen/generated/ios/AudioConcat+autolinking.rb +60 -0
- package/nitrogen/generated/ios/AudioConcat-Swift-Cxx-Bridge.cpp +48 -0
- package/nitrogen/generated/ios/AudioConcat-Swift-Cxx-Bridge.hpp +160 -0
- package/nitrogen/generated/ios/AudioConcat-Swift-Cxx-Umbrella.hpp +53 -0
- package/nitrogen/generated/ios/AudioConcatAutolinking.mm +33 -0
- package/nitrogen/generated/ios/AudioConcatAutolinking.swift +25 -0
- package/nitrogen/generated/ios/c++/HybridAudioConcatSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridAudioConcatSpecSwift.hpp +81 -0
- package/nitrogen/generated/ios/swift/AudioData.swift +35 -0
- package/nitrogen/generated/ios/swift/AudioDataOrSilence.swift +18 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +47 -0
- package/nitrogen/generated/ios/swift/HybridAudioConcatSpec.swift +49 -0
- package/nitrogen/generated/ios/swift/HybridAudioConcatSpec_cxx.swift +142 -0
- package/nitrogen/generated/ios/swift/SilentData.swift +35 -0
- package/nitrogen/generated/shared/c++/AudioData.hpp +67 -0
- package/nitrogen/generated/shared/c++/HybridAudioConcatSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridAudioConcatSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/SilentData.hpp +67 -0
- package/package.json +168 -0
- package/src/AudioConcat.nitro.ts +19 -0
- package/src/index.tsx +38 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "AudioConcat"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/felixchen-dev/react-native-audio-concat.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
s.source_files = [
|
|
18
|
+
"ios/**/*.{swift}",
|
|
19
|
+
"ios/**/*.{m,mm}",
|
|
20
|
+
"cpp/**/*.{hpp,cpp}",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
s.dependency 'React-jsi'
|
|
24
|
+
s.dependency 'React-callinvoker'
|
|
25
|
+
|
|
26
|
+
load 'nitrogen/generated/ios/AudioConcat+autolinking.rb'
|
|
27
|
+
add_nitrogen_files(s)
|
|
28
|
+
|
|
29
|
+
install_modules_dependencies(s)
|
|
30
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Meng Chan
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# react-native-audio-concat
|
|
2
|
+
|
|
3
|
+
audio-concat for react-native
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install react-native-audio-concat react-native-nitro-modules
|
|
10
|
+
|
|
11
|
+
> `react-native-nitro-modules` is required as this library relies on [Nitro Modules](https://nitro.margelo.com/).
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { multiply } from 'react-native-audio-concat';
|
|
20
|
+
|
|
21
|
+
// ...
|
|
22
|
+
|
|
23
|
+
const result = multiply(3, 7);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
30
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
31
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
MIT
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
project(audioconcat)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
|
+
|
|
4
|
+
set(PACKAGE_NAME audioconcat)
|
|
5
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
6
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
7
|
+
|
|
8
|
+
# Define C++ library and add all sources
|
|
9
|
+
add_library(${PACKAGE_NAME} SHARED src/main/cpp/cpp-adapter.cpp)
|
|
10
|
+
|
|
11
|
+
# Add Nitrogen specs :)
|
|
12
|
+
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/audioconcat+autolinking.cmake)
|
|
13
|
+
|
|
14
|
+
# Set up local includes
|
|
15
|
+
include_directories("src/main/cpp" "../cpp")
|
|
16
|
+
|
|
17
|
+
find_library(LOG_LIB log)
|
|
18
|
+
|
|
19
|
+
# Link all libraries together
|
|
20
|
+
target_link_libraries(
|
|
21
|
+
${PACKAGE_NAME}
|
|
22
|
+
${LOG_LIB}
|
|
23
|
+
android # <-- Android core
|
|
24
|
+
)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['AudioConcat_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
def reactNativeArchitectures() {
|
|
19
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
20
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
apply plugin: "com.android.library"
|
|
24
|
+
apply plugin: "kotlin-android"
|
|
25
|
+
apply from: '../nitrogen/generated/android/audioconcat+autolinking.gradle'
|
|
26
|
+
|
|
27
|
+
apply plugin: "com.facebook.react"
|
|
28
|
+
|
|
29
|
+
def getExtOrIntegerDefault(name) {
|
|
30
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AudioConcat_" + name]).toInteger()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
android {
|
|
34
|
+
namespace "com.margelo.nitro.audioconcat"
|
|
35
|
+
|
|
36
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
37
|
+
|
|
38
|
+
defaultConfig {
|
|
39
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
40
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
41
|
+
|
|
42
|
+
externalNativeBuild {
|
|
43
|
+
cmake {
|
|
44
|
+
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
|
|
45
|
+
arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
|
46
|
+
abiFilters (*reactNativeArchitectures())
|
|
47
|
+
|
|
48
|
+
buildTypes {
|
|
49
|
+
debug {
|
|
50
|
+
cppFlags "-O1 -g"
|
|
51
|
+
}
|
|
52
|
+
release {
|
|
53
|
+
cppFlags "-O2"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
externalNativeBuild {
|
|
61
|
+
cmake {
|
|
62
|
+
path "CMakeLists.txt"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
packagingOptions {
|
|
67
|
+
excludes = [
|
|
68
|
+
"META-INF",
|
|
69
|
+
"META-INF/**",
|
|
70
|
+
"**/libc++_shared.so",
|
|
71
|
+
"**/libfbjni.so",
|
|
72
|
+
"**/libjsi.so",
|
|
73
|
+
"**/libfolly_json.so",
|
|
74
|
+
"**/libfolly_runtime.so",
|
|
75
|
+
"**/libglog.so",
|
|
76
|
+
"**/libhermes.so",
|
|
77
|
+
"**/libhermes-executor-debug.so",
|
|
78
|
+
"**/libhermes_executor.so",
|
|
79
|
+
"**/libreactnative.so",
|
|
80
|
+
"**/libreactnativejni.so",
|
|
81
|
+
"**/libturbomodulejsijni.so",
|
|
82
|
+
"**/libreact_nativemodule_core.so",
|
|
83
|
+
"**/libjscexecutor.so"
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
buildFeatures {
|
|
88
|
+
buildConfig true
|
|
89
|
+
prefab true
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
buildTypes {
|
|
93
|
+
release {
|
|
94
|
+
minifyEnabled false
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
lintOptions {
|
|
99
|
+
disable "GradleCompatible"
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
compileOptions {
|
|
103
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
104
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
sourceSets {
|
|
108
|
+
main {
|
|
109
|
+
java.srcDirs += [
|
|
110
|
+
"generated/java",
|
|
111
|
+
"generated/jni"
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
repositories {
|
|
118
|
+
mavenCentral()
|
|
119
|
+
google()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
123
|
+
|
|
124
|
+
dependencies {
|
|
125
|
+
implementation "com.facebook.react:react-android"
|
|
126
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
127
|
+
implementation project(":react-native-nitro-modules")
|
|
128
|
+
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
package com.margelo.nitro.audioconcat
|
|
2
|
+
|
|
3
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
4
|
+
import com.margelo.nitro.core.Promise
|
|
5
|
+
import android.media.MediaCodec
|
|
6
|
+
import android.media.MediaCodecInfo
|
|
7
|
+
import android.media.MediaExtractor
|
|
8
|
+
import android.media.MediaFormat
|
|
9
|
+
import android.media.MediaMuxer
|
|
10
|
+
import java.io.File
|
|
11
|
+
import java.nio.ByteBuffer
|
|
12
|
+
import android.util.Log
|
|
13
|
+
|
|
14
|
+
@DoNotStrip
|
|
15
|
+
class AudioConcat : HybridAudioConcatSpec() {
|
|
16
|
+
private data class AudioConfig(
|
|
17
|
+
val sampleRate: Int,
|
|
18
|
+
val channelCount: Int,
|
|
19
|
+
val bitRate: Int
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
private fun extractAudioConfig(filePath: String): AudioConfig {
|
|
23
|
+
val extractor = MediaExtractor()
|
|
24
|
+
try {
|
|
25
|
+
extractor.setDataSource(filePath)
|
|
26
|
+
for (i in 0 until extractor.trackCount) {
|
|
27
|
+
val format = extractor.getTrackFormat(i)
|
|
28
|
+
val mime = format.getString(MediaFormat.KEY_MIME) ?: continue
|
|
29
|
+
if (mime.startsWith("audio/")) {
|
|
30
|
+
val sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE)
|
|
31
|
+
val channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT)
|
|
32
|
+
val bitRate = if (format.containsKey(MediaFormat.KEY_BIT_RATE)) {
|
|
33
|
+
format.getInteger(MediaFormat.KEY_BIT_RATE)
|
|
34
|
+
} else {
|
|
35
|
+
128000 // Default 128kbps
|
|
36
|
+
}
|
|
37
|
+
return AudioConfig(sampleRate, channelCount, bitRate)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
throw Exception("No audio track found in $filePath")
|
|
41
|
+
} finally {
|
|
42
|
+
extractor.release()
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private class StreamingEncoder(
|
|
47
|
+
sampleRate: Int,
|
|
48
|
+
channelCount: Int,
|
|
49
|
+
bitRate: Int,
|
|
50
|
+
outputPath: String
|
|
51
|
+
) {
|
|
52
|
+
private val encoder: MediaCodec
|
|
53
|
+
private val muxer: MediaMuxer
|
|
54
|
+
private var audioTrackIndex = -1
|
|
55
|
+
private var muxerStarted = false
|
|
56
|
+
private val bufferInfo = MediaCodec.BufferInfo()
|
|
57
|
+
private var totalPresentationTimeUs = 0L
|
|
58
|
+
private val sampleRate: Int
|
|
59
|
+
private val channelCount: Int
|
|
60
|
+
|
|
61
|
+
init {
|
|
62
|
+
this.sampleRate = sampleRate
|
|
63
|
+
this.channelCount = channelCount
|
|
64
|
+
|
|
65
|
+
val outputFormat = MediaFormat.createAudioFormat(
|
|
66
|
+
MediaFormat.MIMETYPE_AUDIO_AAC,
|
|
67
|
+
sampleRate,
|
|
68
|
+
channelCount
|
|
69
|
+
)
|
|
70
|
+
outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC)
|
|
71
|
+
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)
|
|
72
|
+
outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384)
|
|
73
|
+
|
|
74
|
+
encoder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC)
|
|
75
|
+
encoder.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
|
76
|
+
encoder.start()
|
|
77
|
+
|
|
78
|
+
muxer = MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fun encodePCMChunk(pcmData: ByteArray, isLast: Boolean = false): Boolean {
|
|
82
|
+
// Feed PCM data to encoder
|
|
83
|
+
val inputBufferIndex = encoder.dequeueInputBuffer(10000)
|
|
84
|
+
if (inputBufferIndex >= 0) {
|
|
85
|
+
val inputBuffer = encoder.getInputBuffer(inputBufferIndex)!!
|
|
86
|
+
inputBuffer.clear()
|
|
87
|
+
inputBuffer.put(pcmData)
|
|
88
|
+
|
|
89
|
+
val presentationTimeUs = totalPresentationTimeUs
|
|
90
|
+
totalPresentationTimeUs += (pcmData.size.toLong() * 1_000_000) / (sampleRate * channelCount * 2)
|
|
91
|
+
|
|
92
|
+
val flags = if (isLast) MediaCodec.BUFFER_FLAG_END_OF_STREAM else 0
|
|
93
|
+
encoder.queueInputBuffer(inputBufferIndex, 0, pcmData.size, presentationTimeUs, flags)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Drain encoder output
|
|
97
|
+
drainEncoder(isLast)
|
|
98
|
+
|
|
99
|
+
return true
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private fun drainEncoder(endOfStream: Boolean) {
|
|
103
|
+
while (true) {
|
|
104
|
+
val outputBufferIndex = encoder.dequeueOutputBuffer(bufferInfo, if (endOfStream) 10000 else 0)
|
|
105
|
+
|
|
106
|
+
when (outputBufferIndex) {
|
|
107
|
+
MediaCodec.INFO_OUTPUT_FORMAT_CHANGED -> {
|
|
108
|
+
if (muxerStarted) {
|
|
109
|
+
throw RuntimeException("Format changed twice")
|
|
110
|
+
}
|
|
111
|
+
val newFormat = encoder.outputFormat
|
|
112
|
+
audioTrackIndex = muxer.addTrack(newFormat)
|
|
113
|
+
muxer.start()
|
|
114
|
+
muxerStarted = true
|
|
115
|
+
Log.d("AwesomeLibrary", "Encoder started, format: $newFormat")
|
|
116
|
+
}
|
|
117
|
+
MediaCodec.INFO_TRY_AGAIN_LATER -> {
|
|
118
|
+
if (!endOfStream) {
|
|
119
|
+
break
|
|
120
|
+
}
|
|
121
|
+
// Continue draining when end of stream
|
|
122
|
+
}
|
|
123
|
+
else -> {
|
|
124
|
+
if (outputBufferIndex >= 0) {
|
|
125
|
+
val outputBuffer = encoder.getOutputBuffer(outputBufferIndex)!!
|
|
126
|
+
|
|
127
|
+
if ((bufferInfo.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
|
|
128
|
+
bufferInfo.size = 0
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (bufferInfo.size > 0 && muxerStarted) {
|
|
132
|
+
outputBuffer.position(bufferInfo.offset)
|
|
133
|
+
outputBuffer.limit(bufferInfo.offset + bufferInfo.size)
|
|
134
|
+
muxer.writeSampleData(audioTrackIndex, outputBuffer, bufferInfo)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
encoder.releaseOutputBuffer(outputBufferIndex, false)
|
|
138
|
+
|
|
139
|
+
if ((bufferInfo.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
|
|
140
|
+
break
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fun finish() {
|
|
149
|
+
// Signal end of stream
|
|
150
|
+
val inputBufferIndex = encoder.dequeueInputBuffer(10000)
|
|
151
|
+
if (inputBufferIndex >= 0) {
|
|
152
|
+
encoder.queueInputBuffer(inputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Drain remaining data
|
|
156
|
+
drainEncoder(true)
|
|
157
|
+
|
|
158
|
+
encoder.stop()
|
|
159
|
+
encoder.release()
|
|
160
|
+
|
|
161
|
+
if (muxerStarted) {
|
|
162
|
+
muxer.stop()
|
|
163
|
+
}
|
|
164
|
+
muxer.release()
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private fun streamDecodeAudioFile(
|
|
169
|
+
filePath: String,
|
|
170
|
+
encoder: StreamingEncoder,
|
|
171
|
+
isLastFile: Boolean
|
|
172
|
+
) {
|
|
173
|
+
val extractor = MediaExtractor()
|
|
174
|
+
var decoder: MediaCodec? = null
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
extractor.setDataSource(filePath)
|
|
178
|
+
|
|
179
|
+
var audioTrackIndex = -1
|
|
180
|
+
var audioFormat: MediaFormat? = null
|
|
181
|
+
|
|
182
|
+
for (i in 0 until extractor.trackCount) {
|
|
183
|
+
val format = extractor.getTrackFormat(i)
|
|
184
|
+
val mime = format.getString(MediaFormat.KEY_MIME) ?: continue
|
|
185
|
+
if (mime.startsWith("audio/")) {
|
|
186
|
+
audioTrackIndex = i
|
|
187
|
+
audioFormat = format
|
|
188
|
+
break
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (audioTrackIndex == -1 || audioFormat == null) {
|
|
193
|
+
throw Exception("No audio track found in $filePath")
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
extractor.selectTrack(audioTrackIndex)
|
|
197
|
+
|
|
198
|
+
val mime = audioFormat.getString(MediaFormat.KEY_MIME)!!
|
|
199
|
+
decoder = MediaCodec.createDecoderByType(mime)
|
|
200
|
+
decoder.configure(audioFormat, null, null, 0)
|
|
201
|
+
decoder.start()
|
|
202
|
+
|
|
203
|
+
val bufferInfo = MediaCodec.BufferInfo()
|
|
204
|
+
var isEOS = false
|
|
205
|
+
val pcmChunkSize = 8192 // Process in 8KB chunks
|
|
206
|
+
|
|
207
|
+
while (!isEOS) {
|
|
208
|
+
// Feed input to decoder
|
|
209
|
+
val inputBufferIndex = decoder.dequeueInputBuffer(10000)
|
|
210
|
+
if (inputBufferIndex >= 0) {
|
|
211
|
+
val inputBuffer = decoder.getInputBuffer(inputBufferIndex)!!
|
|
212
|
+
val sampleSize = extractor.readSampleData(inputBuffer, 0)
|
|
213
|
+
|
|
214
|
+
if (sampleSize < 0) {
|
|
215
|
+
decoder.queueInputBuffer(inputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
|
|
216
|
+
} else {
|
|
217
|
+
val presentationTimeUs = extractor.sampleTime
|
|
218
|
+
decoder.queueInputBuffer(inputBufferIndex, 0, sampleSize, presentationTimeUs, 0)
|
|
219
|
+
extractor.advance()
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Get PCM output from decoder and feed to encoder
|
|
224
|
+
val outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 10000)
|
|
225
|
+
if (outputBufferIndex >= 0) {
|
|
226
|
+
val outputBuffer = decoder.getOutputBuffer(outputBufferIndex)!!
|
|
227
|
+
|
|
228
|
+
if (bufferInfo.size > 0) {
|
|
229
|
+
val pcmData = ByteArray(bufferInfo.size)
|
|
230
|
+
outputBuffer.get(pcmData)
|
|
231
|
+
|
|
232
|
+
// Stream to encoder immediately
|
|
233
|
+
encoder.encodePCMChunk(pcmData, false)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
decoder.releaseOutputBuffer(outputBufferIndex, false)
|
|
237
|
+
|
|
238
|
+
if ((bufferInfo.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
|
|
239
|
+
isEOS = true
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
} finally {
|
|
245
|
+
decoder?.stop()
|
|
246
|
+
decoder?.release()
|
|
247
|
+
extractor.release()
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private fun streamEncodeSilence(
|
|
252
|
+
durationMs: Double,
|
|
253
|
+
encoder: StreamingEncoder,
|
|
254
|
+
sampleRate: Int,
|
|
255
|
+
channelCount: Int
|
|
256
|
+
) {
|
|
257
|
+
val totalSamples = ((durationMs / 1000.0) * sampleRate).toInt()
|
|
258
|
+
val chunkSamples = 4096 // Process in chunks
|
|
259
|
+
val bytesPerSample = channelCount * 2 // 16-bit stereo
|
|
260
|
+
|
|
261
|
+
var samplesRemaining = totalSamples
|
|
262
|
+
|
|
263
|
+
while (samplesRemaining > 0) {
|
|
264
|
+
val currentChunkSamples = minOf(chunkSamples, samplesRemaining)
|
|
265
|
+
val chunkBytes = currentChunkSamples * bytesPerSample
|
|
266
|
+
val silenceChunk = ByteArray(chunkBytes) // All zeros = silence
|
|
267
|
+
|
|
268
|
+
encoder.encodePCMChunk(silenceChunk, false)
|
|
269
|
+
samplesRemaining -= currentChunkSamples
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
override fun concatAudioFiles(data: Array<AudioDataOrSilence>, outputPath: String): Promise<String> {
|
|
274
|
+
return Promise.async {
|
|
275
|
+
if (data.isEmpty()) {
|
|
276
|
+
throw Exception("Data array is empty")
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
Log.d("AwesomeLibrary", "Streaming merge of ${data.size} items")
|
|
280
|
+
Log.d("AwesomeLibrary", "Output: $outputPath")
|
|
281
|
+
|
|
282
|
+
// Get audio config from first audio file
|
|
283
|
+
var audioConfig: AudioConfig? = null
|
|
284
|
+
for (item in data) {
|
|
285
|
+
if (item is AudioDataOrSilence.First) {
|
|
286
|
+
audioConfig = extractAudioConfig(item.value.filePath)
|
|
287
|
+
break
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (audioConfig == null) {
|
|
292
|
+
throw Exception("No audio files found in data array")
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
Log.d("AwesomeLibrary", "Audio config: ${audioConfig.sampleRate}Hz, ${audioConfig.channelCount}ch, ${audioConfig.bitRate}bps")
|
|
296
|
+
|
|
297
|
+
// Delete existing output file
|
|
298
|
+
val outputFile = File(outputPath)
|
|
299
|
+
if (outputFile.exists()) {
|
|
300
|
+
outputFile.delete()
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Create streaming encoder
|
|
304
|
+
val encoder = StreamingEncoder(
|
|
305
|
+
audioConfig.sampleRate,
|
|
306
|
+
audioConfig.channelCount,
|
|
307
|
+
audioConfig.bitRate,
|
|
308
|
+
outputPath
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
try {
|
|
312
|
+
// Process each item
|
|
313
|
+
for ((index, item) in data.withIndex()) {
|
|
314
|
+
when (item) {
|
|
315
|
+
is AudioDataOrSilence.First -> {
|
|
316
|
+
val filePath = item.value.filePath
|
|
317
|
+
Log.d("AwesomeLibrary", "Item $index: Streaming decode $filePath")
|
|
318
|
+
|
|
319
|
+
val isLastFile = (index == data.size - 1)
|
|
320
|
+
streamDecodeAudioFile(filePath, encoder, isLastFile)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
is AudioDataOrSilence.Second -> {
|
|
324
|
+
val durationMs = item.value.durationMs
|
|
325
|
+
Log.d("AwesomeLibrary", "Item $index: Streaming silence ${durationMs}ms")
|
|
326
|
+
|
|
327
|
+
streamEncodeSilence(
|
|
328
|
+
durationMs,
|
|
329
|
+
encoder,
|
|
330
|
+
audioConfig.sampleRate,
|
|
331
|
+
audioConfig.channelCount
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Finish encoding
|
|
338
|
+
encoder.finish()
|
|
339
|
+
Log.d("AwesomeLibrary", "Successfully merged audio to $outputPath")
|
|
340
|
+
|
|
341
|
+
} catch (e: Exception) {
|
|
342
|
+
Log.e("AwesomeLibrary", "Error during streaming merge: ${e.message}", e)
|
|
343
|
+
throw e
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
outputPath
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
package com.margelo.nitro.audioconcat
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.TurboReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
|
|
8
|
+
class AudioConcatPackage : TurboReactPackage() {
|
|
9
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
10
|
+
return null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
14
|
+
return ReactModuleInfoProvider { HashMap() }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
companion object {
|
|
18
|
+
init {
|
|
19
|
+
System.loadLibrary("audioconcat")
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|