audio-toolbox-rs

April 3, 2026 · View on GitHub

crates.io docs.rs License GitHub Actions Discord

About Shiguredo's open source software

We will not respond to PRs or issues that have not been discussed on Discord. Also, Discord is only available in Japanese.

Please read https://github.com/shiguredo/oss before use.

時雨堂のオープンソースソフトウェアについて

利用前に https://github.com/shiguredo/oss をお読みください。

shiguredo_audio_toolbox について

Apple の Audio Toolbox を利用した音声エンコーダーおよびデコーダーの Rust バインディングです。

macOS 専用で、ビルド時に Xcode の SDK ヘッダーを参照して bindgen でバインディングを自動生成します。

特徴

  • Audio Toolbox による AAC-LC エンコード
  • Audio Toolbox によるデコード (AAC-LC / MP3 / Opus)
  • エンコーダーはサンプルレート / チャンネル数 / ビットレート / 品質を指定可能
  • デコーダーは入力サンプルレートとチャンネル数を指定可能
  • 出力は 16 bit ステレオ PCM 固定

対応コーデック

エンコード

コーデックEncoderCodec
AAC-LCEncoderCodec::AacLc

デコード

コーデックDecoderCodec
AAC-LCDecoderCodec::AacLc
MP3DecoderCodec::Mp3
OpusDecoderCodec::Opus

動作要件

  • macOS (arm64)
  • Rust 1.88 以降
  • Xcode Command Line Tools (ビルド時に Audio Toolbox のヘッダーファイルが必要)

ビルド

cargo build

docs.rs 向けビルド

Xcode がない環境 (Linux 等) では、docs.rs 向けのドキュメント生成のみ可能です。

DOCS_RS=1 cargo doc --no-deps

使い方

エンコード

use shiguredo_audio_toolbox::{Encoder, EncoderCodec, EncoderConfig};

let mut encoder = Encoder::new(EncoderConfig {
    codec: EncoderCodec::AacLc,
    sample_rate: 48000,
    channels: 2,
    bitrate: Some(128_000),
    bitrate_control_mode: None,
    codec_quality: None,
    vbr_quality: None,
})?;

// PCM データ (i16, インターリーブ) をエンコード
let pcm: &[i16] = &[0; 1024 * 2];
encoder.encode(pcm)?;
while let Some(frame) = encoder.next_frame() {
    println!("encoded bytes: {}, samples: {}", frame.data.len(), frame.samples);
}

// 残りのフレームをフラッシュ
encoder.finish()?;
while let Some(frame) = encoder.next_frame() {
    println!("flushed bytes: {}", frame.data.len());
}

デコード

use shiguredo_audio_toolbox::{Decoder, DecoderCodec, DecoderConfig};

let mut decoder = Decoder::new(DecoderConfig {
    codec: DecoderCodec::AacLc,
    input_sample_rate: 48000,
    input_channels: 2,
})?;

// 1 パケットずつ decode する(前のパケットを next_frame で消費するまで再度 decode しない)
// `packets` は各パケットの圧縮バイト列のイテラブル(例: `Vec<Vec<u8>>`)
for packet in packets {
    decoder.decode(&packet)?;
    while let Some(pcm) = decoder.next_frame()? {
        println!("decoded samples: {}", pcm.len() / 2);
    }
}
decoder.finish()?;

while let Some(pcm) = decoder.next_frame()? {
    println!("decoded samples (flush): {}", pcm.len() / 2);
}

コーデック情報取得

supported_codecs() 関数でシステムがサポートするコーデックの情報を取得できます。

use shiguredo_audio_toolbox::supported_codecs;

for info in supported_codecs() {
    println!("{:?}: decode={}, encode={}",
        info.codec,
        info.decoding.supported,
        info.encoding.supported,
    );
    if info.encoding.supported {
        println!("  bitrate control modes: {:?}", info.encoding.bitrate_control_modes);
    }
}

AudioCodecType

コーデックAudioCodecType
AAC-LCAudioCodecType::AacLc
AAC-HEAudioCodecType::AacHe
AAC-HE v2AudioCodecType::AacHeV2
AAC-LDAudioCodecType::AacLd
AAC-ELDAudioCodecType::AacEld
MP3AudioCodecType::Mp3
OpusAudioCodecType::Opus
FLACAudioCodecType::Flac
ALACAudioCodecType::Alac

supported_codecs() が返す対象は EncoderCodec / DecoderCodec に対応するコーデック (AAC-LC, MP3, Opus) のみです。

AudioCodecInfo

フィールド説明
codecAudioCodecTypeコーデック種別
decodingAudioDecodingInfoデコード対応情報
encodingAudioEncodingInfoエンコード対応情報

AudioDecodingInfo

フィールド説明
supportedboolデコードが利用可能かどうか

AudioEncodingInfo

フィールド説明
supportedboolエンコードが利用可能かどうか
bitrate_control_modesVec<BitRateControlMode>利用可能なビットレート制御モード

設定

EncoderConfig

フィールド説明
codecEncoderCodecコーデック種別
sample_rateu32サンプルレート (Hz)、0 不可
channelsu8チャンネル数、0 不可
bitrateOption<u32>ビットレート (bps)、未指定時はバックエンド依存
bitrate_control_modeOption<BitRateControlMode>ビットレート制御モード、未指定時はバックエンド依存
codec_qualityOption<CodecQuality>コーデック品質、未指定時はバックエンド依存
vbr_qualityOption<u32>VBR 品質 (0-127)、未指定時はバックエンド依存

DecoderConfig

フィールド説明
codecDecoderCodecコーデック種別
input_sample_rateu32入力のサンプルレート
input_channelsu8入力のチャンネル数、0 不可

デコーダー出力フォーマット:

パラメータ説明
サンプルレート入力と同じ入力に合わせる
チャンネル数2 (ステレオ)固定
ビット深度16 bit (i16)固定

EncodedFrame

フィールド説明
dataVec<u8>圧縮データ
samplesusizeフレームに含まれるサンプル数

Error

フィールド説明
statusi32AudioConverter API のステータスコード (OSStatus)
function&'static strエラーが発生した API 関数名

サンプル

sine_to_mp4

正弦波 PCM を AAC-LC エンコードして MP4 ファイルに保存するサンプルです。

cargo run --example sine_to_mp4 -- --bitrate 256000 --duration 10 --freq 880 --output tone.mp4

スレッド安全性

Encoder / DecoderSend を実装していません。Apple の AudioConverter API にスレッド間移動の規範的根拠がないためです。

ライセンス

Apache License 2.0

Copyright 2026-2026, Shiguredo Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.