

coast_audio adalah perpustakaan pemrosesan audio berkinerja tinggi yang ditulis di DART.
Paket ini bertujuan untuk memberikan fungsi audio tingkat rendah tanpa ketergantungan yang tidak bergetar .
| Android | iOS | MacOS | Windows | Linux | Web |
|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ |
coast_audio dibangun di atas dart:ffi .
Dengan demikian, dapat digunakan di lingkungan DART apa pun yang mendukung FFI.
Beberapa fungsi diimplementasikan dalam kode asli yang menggunakan miniaudio.
Repositori ini berisi binari pra-built untuk setiap platform yang didukung.
Tambahkan yang berikut ke pubspec.yaml Anda:
coast_audio : ^1.0.0android/src/main/jniLibs dalam proyek Anda.{ABI}/libcoast_audio.so dari direktori native/prebuilt/android ke direktori jniLibs . Tambahkan yang berikut ke Podfile Anda:
target 'Runner' do
...
pod 'CoastAudio' , :git => 'https://github.com/SKKbySSK/coast_audio.git' , :tag => '1.0.0'
end Buka file AppDelegate.swift dan tambahkan impor berikut dan call CoastAudioSymbolKeeper.keep() :
import CoastAudio // 1. Add import
@ UIApplicationMain
@ objc class AppDelegate : FlutterAppDelegate {
override func application (
_ application : UIApplication ,
didFinishLaunchingWithOptions launchOptions : [ UIApplication . LaunchOptionsKey : Any ] ?
) -> Bool {
CoastAudioSymbolKeeper . keep ( ) // 2. Add this line to prevent native symbols from being stripped (You can place this anywhere inside your iOS/macOS code)
GeneratedPluginRegistrant . register ( with : self )
return super . application ( application , didFinishLaunchingWithOptions : launchOptions )
}
}{ARCH}/libcoast_audio.so dari direktori native/prebuilt/linux ke direktori linux/libs di proyek Anda.linux/CMakeLists.txt Anda: install ( FILES "linux/libs/ ${CMAKE_SYSTEM_PROCESSOR} /libcoast_audio.so" DESTINATION " ${INSTALL_BUNDLE_LIB_DIR} " COMPONENT Runtime)Todo
Belum ada perpustakaan asli yang sudah dibangun untuk Windows.
Anda perlu membangunnya secara manual.
coast_audio menyediakan berbagai node audio untuk menghasilkan/memproses data audio dengan mudah.
Contoh kode ini menghasilkan gelombang sinus 440Hz dengan menggunakan FunctionNode .
// define the audio format with 48khz sample rate and stereo channels.
final format = AudioFormat (sampleRate : 48000 , channels : 1 , sampleFormat : SampleFormat .int16);
// create a sine wave function node with 440hz frequency.
final functionNode = FunctionNode (
function : const SineFunction (),
frequency : 440 ,
);
AllocatedAudioFrames (length : 1024 , format : format). acquireBuffer ((buffer) {
// read the audio data from the function node to the buffer.
functionNode.outputBus. read (buffer);
// floatList contains sine wave audio data.
final floatList = buffer. asFloat32ListView ();
}); Anda dapat menggunakan WavAudioEncoder untuk menyandikan data audio.
// define the audio format with 48khz sample rate and stereo channels.
final format = AudioFormat (sampleRate : 48000 , channels : 1 , sampleFormat : SampleFormat .int16);
// create a sine wave function node with 440hz frequency.
final functionNode = FunctionNode (
function : const SineFunction (),
frequency : 440 ,
);
final fileOutput = AudioFileDataSource (
file : File ( 'output.wav' ),
mode : FileMode .write,
);
// create a wav audio encoder.
final encoder = WavAudioEncoder (dataSource : fileOutput, inputFormat : format);
encoder. start ();
final duration = AudioTime ( 10 );
AllocatedAudioFrames (length : duration. computeFrames (format), format : format). acquireBuffer ((buffer) {
// read the audio data from the function node to the buffer.
final result = functionNode.outputBus. read (buffer);
// encode the audio data to the wav file.
encoder. encode (buffer. limit (result.frameCount));
});
encoder. finalize (); AudioNode dapat dihubungkan ke node lain untuk membangun grafik audio.
Kode contoh ini menunjukkan cara mencampur dua gelombang sinus dan menulis ke file WAV.
const format = AudioFormat (sampleRate : 48000 , channels : 1 );
final mixerNode = MixerNode (format : format);
// Initialize sine wave nodes and connect them to mixer's input
for ( final freq in [ 264.0 , 330.0 , 396.0 ]) {
final sineNode = FunctionNode (function : const SineFunction (), format : format, frequency : freq);
final mixerInputBus = mixerNode. appendInputBus ();
sineNode.outputBus. connect (mixerInputBus);
}
AllocatedAudioFrames (length : 1024 , format : format).bufferFrames. acquireBuffer ((buffer) {
// read the audio data from the function node to the buffer.
functionNode.outputBus. read (buffer);
// floatList contains mixed sine wave audio data.
final floatList = buffer. asFloat32ListView ();
}); coast_audio menyediakan kelas AudioDevice untuk menangani perangkat audio I/O.
Silakan lihat contoh -contoh berikut:
Ya, Anda dapat menggunakan coast_audio dalam Flutter.
Sebagian besar operasi coast_audio sinkron dan dapat memblokir isolat utama flutter.
Jadi, disarankan untuk menggunakan coast_audio dalam isolat terpisah.
Silakan lihat contoh implementasi aplikasi untuk detail lebih lanjut.
coast_audio di web? Singkatnya, tidak,
Anda harus menggunakan dart:web_audio sebagai gantinya.
Tetapi mungkin tersedia jika FFI didukung di platform web.