JavaScript/TypeScript ルーム音響 API
libsonare JavaScript/TypeScript パッケージのルーム音響の計測と合成の関数です。オーディオエフェクトと編集 DSP は エフェクト API を参照してください。
ルーム音響解析
これらの関数は、曲そのものではなく録音空間を説明・適用する API です。背景は 残響時間(RT60 と EDT)、部屋の形状と容積、間隔を空けたレシーバーとチャンネル間コヒーレンス を参照してください。
| 目的 | 使う API |
|---|---|
| きれいなインパルス応答(IR)を測る | analyzeImpulseResponse(...) |
| 通常音声から部屋の減衰を推定する | detectAcoustic(...) |
| 音声から実用的な部屋モデルを推定する | estimateRoom(...) |
その推定結果を synthesizeRir の入力にする | roomGeometryFromEstimate(...) |
| 寸法からモノラルのルームインパルス応答を作る | synthesizeRir(...) |
| 目標ルームの響きを音作り効果として足す | roomMorph(...) |
| 同じ響きをステレオ録音に足す(間隔をあけた 2 つのレシーバーで聞く) | roomMorphStereo({ left, right, sampleRate, ... }) |
RIR とルームモーフィング
RIR は room impulse response(ルームインパルス応答)の略で、部屋が短い音にどう反応するかを表すサンプル列です。roomMorph(...) は音作り効果であり、残響除去ではありません。
function analyzeImpulseResponse(request: AnalyzeImpulseResponseRequest): AcousticResult
function analyzeImpulseResponse(
samples: Float32Array,
sampleRate: number,
options?: OptionsOf<AnalyzeImpulseResponseRequest>,
): AcousticResult
function detectAcoustic(request: DetectAcousticRequest): AcousticResult
function detectAcoustic(
samples: Float32Array,
sampleRate: number,
options?: OptionsOf<DetectAcousticRequest>,
): AcousticResult
function estimateRoom(request: EstimateRoomRequest): RoomEstimateResult
function estimateRoom(
samples: Float32Array,
sampleRate: number,
options?: RoomEstimateOptions,
): RoomEstimateResult
function roomGeometryFromEstimate(request: RoomGeometryFromEstimateRequest): RoomGeometry // request form only
function synthesizeRir(request: SynthesizeRirRequest): RirResult // request form only
function roomMorph(request: RoomMorphRequest): RoomMorphResult
function roomMorph(
samples: Float32Array,
sampleRate: number,
options?: RoomMorphOptions,
): RoomMorphResult
function roomMorphStereo(request: RoomMorphStereoRequest): RoomMorphStereoResult // request form only
interface AnalyzeImpulseResponseRequest extends SampleInput {
nOctaveBands?: number; // Octave bands; default 6.
minDecayDb?: number; // Minimum decay range in dB for a valid RT60 fit; default 30.
}
interface AcousticOptions {
nOctaveBands?: number; // Number of octave bands. Default 6.
nThirdOctaveSubbands?: number; // Number of 1/3-octave sub-bands. Default 24.
minDecayDb?: number; // Minimum decay range (dB) for a valid RT60 fit. Default 30.
noiseFloorMarginDb?: number; // Margin (dB) above the noise floor for the decay fit. Default 10.
}
interface RoomEstimateOptions {
aspectHintLw?: number; // Length/width ratio hint of the room. Omit for no hint; an explicit 0 is refused.
aspectHintLh?: number; // Length/height ratio hint of the room. Omit for no hint; an explicit 0 is refused.
referenceAbsorption?: number; // Mean-absorption prior anchoring the volume scale. Omit for the library default, 0.15; a given value must lie in `[0.01, 0.99]`, and one outside it (0 included) is refused.
preferEyring?: boolean; // Use the Eyring statistical late-tail model (default true); false = Sabine.
nOctaveBands?: number; // Octave bands analysed. Omit for the library default; an explicit 0 is refused.
mode?: AcousticModeName; // Analyzer routing, by name: `'auto'`, `'blind'`, `'impulseResponse'`.
minDecayDb?: number; // Analyzer decay-fit span in dB. Omit for the library default; an explicit 0 is refused.
noiseFloorMarginDb?: number; // Analyzer noise-floor margin in dB. Omit for the library default; an explicit 0 is refused.
}
interface RoomGeometryFromEstimateRequest {
estimate: RoomGeometryEstimate; // The room estimate to read the geometry from.
source?: RoomPlacement; // Replaces the source placement scaled into the estimated room.
listener?: RoomPlacement; // Replaces the listener placement scaled into the estimated room.
}
interface SynthesizeRirRequest extends RoomGeometry {
sampleRate: number; // Sample rate in Hz, an integer in `[8000, 384000]`; there is no default.
preferEyring?: boolean; // Use the Eyring statistical late-tail model (default true); false = Sabine.
mixingTimeMs?: number; // Early/late crossover in ms. Omit for auto (~sqrt(V) ms); an explicit 0 is refused.
crossfadeMs?: number; // Equal-power crossfade width around the mixing time in ms. Omit for 5 ms; 0 is a hard switch.
}
interface RoomMorphOptions extends RoomGeometry {
wet?: number; // Wet level of the target room.
sourceTailSuppression?: number; // How far the source recording's own room tail is suppressed.
preferEyring?: boolean; // Use the Eyring statistical late-tail model for the target room (default true); false = Sabine.
mixingTimeMs?: number; // Early/late crossover in ms. Omit for auto (~sqrt(V) ms); an explicit 0 is refused.
crossfadeMs?: number; // Equal-power crossfade width around the mixing time in ms. Omit for 5 ms; 0 is a hard switch.
}
interface RoomMorphStereoRequest extends RoomMorphOptions {
left: Float32Array; // Left channel samples.
right: Float32Array; // Right channel samples; same length as `left`.
sampleRate: number; // Sample rate in Hz, an integer in `[8000, 384000]`; there is no default.
receiverSpacingM?: number; // 受音点間隔(m)は (0, 4]。省略時は、収まるなら 0.5 m、収まらなければ配置できる最大の間隔です。
}
interface AcousticResult {
rt60?: number; // Broadband reverberation time (RT60) in seconds; absent when no decay was fitted.
edt?: number; // Early decay time in seconds; absent when `isBlind` or not fitted.
c50?: number; // Clarity C50 in dB; absent when `isBlind` or not measurable.
c80?: number; // Clarity C80 in dB; absent when `isBlind` or not measurable.
d50?: number; // Definition D50 (early-to-total energy ratio); absent when `isBlind` or not measurable.
rt60Bands?: Float32Array; // Per-octave-band RT60; NaN for a band not fitted.
edtBands?: Float32Array; // Per-octave-band early decay time; absent when `isBlind`, NaN for an unfit band.
c50Bands?: Float32Array; // Per-octave-band C50; absent when `isBlind`, NaN for an unmeasured band.
c80Bands?: Float32Array; // Per-octave-band C80; absent when `isBlind`, NaN for an unmeasured band.
confidence: number; // Support for the estimate, in [0, 1].
isBlind: boolean; // True when measured from a blind recording rather than an impulse response.
}
interface RoomEstimateResult {
volume?: number; // Equivalent volume (m^3); absent with the dimensions when no broadband decay was measurable.
lengthM?: number; // Representative room length in metres; absent when no decay is measurable.
widthM?: number; // Representative room width in metres; absent when no decay is measurable.
heightM?: number; // Representative room height in metres; absent when no decay is measurable.
drrDb?: number; // Direct-to-reverberant ratio; absent when it was not measurable.
confidence: number; // Support for the estimate, in [0, 1].
bandAbsorption?: Float32Array; // Per-octave-band mean wall absorption.
rt60Bands?: Float32Array; // Per-octave-band RT60 in seconds.
}
interface RirResult {
rir: Float32Array; // Impulse response samples.
sampleRate: number; // Sample rate of `rir` in Hz.
diagnostics: RirDiagnostic[]; // Every diagnostic the synthesizer reported, in order. A refused synthesis throws instead, so every entry is a warning — a `maxSec` clamp that cut the tail is one, and is otherwise indistinguishable from an untruncated RIR.
}
interface RoomMorphResult {
audio: Float32Array; // Morphed samples: input length plus the target room's reverb tail.
sampleRate: number; // Sample rate of `audio` in Hz.
diagnostics: RirDiagnostic[]; // Every diagnostic the target-room synthesis reported, in order.
}
interface RoomMorphStereoResult {
left: Float32Array; // Morphed left samples: input length plus the target room's reverb tail.
right: Float32Array; // Morphed right samples, the same length as `left`.
sampleRate: number; // Sample rate of `left` and `right` in Hz.
diagnostics: RirDiagnostic[]; // Target-room synthesis diagnostics, with the same codes as {@link RoomMorphResult.diagnostics}.
}
interface RoomGeometry {
lengthM?: number; // Room length in metres.
widthM?: number; // Room width in metres.
heightM?: number; // Room height in metres.
absorption?: number; // Uniform wall absorption in [0, 1], used when no band absorption or preset is given; a value outside is refused, and the model holds a wall at most 0.999 absorbent. Defaults to 0.2.
bandAbsorption?: Float32Array | number[]; // Optional per-octave-band wall absorption (125/250/500/1k/2k/4k..
bandScattering?: Float32Array | number[]; // Optional per-band wall scattering; missing bands default to 0.
materialPreset?: MaterialPresetName; // Wall-material preset, by name (`'none'`, `'concrete'`, `'wood'`, `'curtain'`, `'carpet'`, `'glass'`).
source?: RoomPlacement; // Source position in metres. Default: the 7 x 5 x 3 room's default placement scaled into the room.
listener?: RoomPlacement; // Listener position in metres. Default: the default placement scaled into the room.
ismOrder?: number; // Image-source reflection order; clamped to the safe maximum.
seed?: number; // Seed of the diffuse tail, a uint32. Omit for the default seed 1; 0 is a seed like any other.
maxSec?: number; // Maximum RIR length in seconds. Omit for the natural length (no cap); an explicit 0 is refused.
airAbsorptionEnabled?: boolean; // Add the ISO 9613-1 atmospheric-absorption term to the late tail's per-band RT60. On by default; it mainly shortens the high bands of a large room.
airTemperatureC?: number; // Air temperature in degrees Celsius. Omit for the ISO reference climate's 20 degC; 0 is 0 degC.
airHumidityPercent?: number; // Relative humidity in percent. Omit for the ISO reference climate's 50 %; 0 is dry air.
}
interface RoomPlacement {
x: number; // X coordinate in metres.
y: number; // Y coordinate in metres.
z: number; // Z coordinate in metres.
}const ir = analyzeImpulseResponse(impulseResponseSamples, sampleRate, { nOctaveBands: 6, minDecayDb: 30 });
console.log(ir.rt60, ir.edt, ir.c50, ir.c80, ir.confidence);
const blind = detectAcoustic(roomRecording, sampleRate, {
nOctaveBands: 6,
nThirdOctaveSubbands: 24,
minDecayDb: 30,
noiseFloorMarginDb: 10,
});
console.log(blind.isBlind, blind.rt60Bands);
const estimate = estimateRoom(roomRecording, sampleRate, {
referenceAbsorption: 0.15,
nOctaveBands: 6,
});
console.log(estimate.volume, estimate.lengthM, estimate.widthM, estimate.heightM);
console.log(estimate.drrDb, estimate.confidence, estimate.bandAbsorption);
if (typeof estimate.lengthM !== 'number' || typeof estimate.widthM !== 'number' || typeof estimate.heightM !== 'number') {
throw new Error('Room dimensions were not measured');
}
// Throws a RangeError naming the refused value and its range (e.g. source.x) for unusable geometry.
const rir = synthesizeRir({ sampleRate, lengthM: 7, widthM: 5, heightM: 3, absorption: 0.2, materialPreset: 'none' });
console.log(rir.sampleRate, rir.rir.length, rir.diagnostics);
// An estimate already uses synthesizeRir's names; after the dimension check above, the helper adds a placement scaled into the estimated room; source and listener override it.
const geometry = roomGeometryFromEstimate({
estimate,
source: { x: 1, y: 1, z: 1.2 },
listener: { x: 2, y: 1.5, z: 1.5 },
});
const resynth = synthesizeRir({ ...geometry, sampleRate });
const morphed = roomMorph(samples, sampleRate, { lengthM: 12, widthM: 9, heightM: 4, wet: 0.6 });
// morphed.audio is the processed signal; morphed.sampleRate and morphed.diagnostics come with itanalyzeImpulseResponse(samples, sampleRate, { nOctaveBands?, minDecayDb? }) の minDecayDb は減衰フィットのしきい値で、既定値は 30 です。
estimateRoom の referenceAbsorption は、体積のスケールを決める平均吸音率の事前値です。省略するとライブラリの既定値(0.15)になります。指定する値は [0.01, 0.99] に収める必要があり、明示的な 0 を含め範囲外の値はクランプされずに RangeError になります。報告される体積は事前値の 3 乗に比例します。
materialPreset('none' | 'concrete' | 'wood' | 'curtain' | 'carpet' | 'glass'、型は MaterialPresetName)と estimateRoom の mode オプション('auto' | 'blind' | 'impulseResponse'、型は AcousticModeName)は名前だけを受け付けます。数値は TypeError、未知の名前は RangeError です。roomGeometryFromEstimate({ estimate, source?, listener? })(リクエスト形式のみ)は RoomPlacement(メートル単位の { x, y, z })の位置を受け取り、RoomGeometry を返します。既定の配置を推定した部屋へ縮尺した位置が入り(渡した位置はそれを置き換えます)、absorption は有限なバンドの平均です。計測できる寸法がない推定結果には RangeError を投げます。
RT60、EDT、C50、C80、D50、バンド別配列、ルーム推定、生成 RIR、信頼度の読み方は ルーム音響解析 を参照してください。