Macの本体搭載マイクから音声入力を行い、音量を計測するAppleScriptです。0.1秒間隔で10秒ほど音量を計測してリストに出力します。
音量計測は割と事前の知識が必要なディープな処理で、システム側で想定されている最大音量を0dbとして、その値からの相対値で結果が出力されます。本Scriptでプラスの値が出ていたら、それは手を叩いたり騒音を出したりと瞬間的に大きな音(実際に録音したものを聞くと、おそらく音割れしている)が入力された場合です。
マイナスの値で出力されると一般的なスマホ上で動く騒音計アプリだと正の値でdb値を出力されるので、このマイナスの値が出てくると実用性を感じません。
ただ、これをプラスの値として出すためには、無響室でMacの実機を持ち込んで基準となる音量を出しつつ計測するしかないでしょう。
機種ごと、同一機種でも製造年ごとに使用されているマイクの特性も異なるでしょうし、経年変化でマイクの穴にホコリが詰まって音量の「感度」が変化してしまうことでしょう。
無響室にかたっぱしから稼働中のマシンを叩き込んで計測するわけにもいかないので、同一機種の同一モデルで環境音もしくはスピーカーから出した音をマイク計測することで「聞こえ方」の変化の平均値を求めて経年変化などを実測していくしかないのでしょう。
なんといったらよいのか、ひたすらめんどくさいです。とりあえず、現状ではこのままです。
| AppleScript名:音量計測テスト v3.scptd |
| — – Created by: Takaaki Naganoya – Created on: 2026/02/25 — – Copyright © 2026 Piyomaru Software, All Rights Reserved — use AppleScript version "2.8" use framework "Foundation" use framework "AVFoundation" use scripting additions property recorder : missing value — 定数の直接定義(結局、ASの数値の桁数が足りなくて直接書いている。ここに残していることにメモ以上の意味はない) property kAudioFormatAppleLossless : "1634492771" as integer — ’alac’ property AVAudioQualityMax : 127 –db値が入る script vS property vList : {} end script on run setupRecorder() of me if recorder is missing value then return — 10秒間、0.1秒おきに音量を取得 repeat 100 times set tmpV to my getVolumeLevel() set the end of (vList of vS) to tmpV delay 0.1 end repeat recorder’s |stop|() return (vList of vS) –> {-47.89, -22.7, -26.29, -30.39, -34.0, -37.11, -39.87, -42.53, -43.21, -39.55, -34.34, -37.54, -27.52, -31.54, -34.65, -36.6, -38.25, -39.7, -41.37, -42.49, -43.89, -38.54, -33.26, -36.65, -39.36, -38.74, -34.47, -37.4, -39.25, -33.12, -36.15, -39.61, -42.5, -35.92, -35.52, -35.85, -38.02, -39.5, -34.86, -39.01, -41.89, -39.15, -37.5, -40.11, -38.1, -35.4, -33.35, -33.85, -35.76, -33.14, -35.21, -35.7, -35.41, -36.55, -33.9, -35.97, -38.45, -36.65, -38.02, -34.14, -35.98, -32.51, -34.71, -37.47, -37.34, -41.38, -39.31, -36.95, -37.92, -34.59, -37.82, -40.41, -43.67, -43.61, -40.3, -34.22, -35.49, -39.51, -43.57, -46.11, -42.36, -41.3, -45.14, -44.23, -38.4, -41.63, -34.71, -37.4, -40.76, -36.16, -39.49, -36.28, -38.27, -39.19, -39.04, -38.36, -39.51, -41.33, -39.5, -42.18, -48.23, -23.73, -27.04, -30.32, -33.36, -36.33, -36.51, -34.89, -38.28, -38.68, -40.08, -41.9, -34.01, -36.47, -32.51, -33.97, -36.79, -34.06, -35.58, -33.09, -36.67, -40.52, -40.13, -37.82, -41.57, -29.71, -32.41, -35.6, -37.91, -34.2, -37.36, -38.87, -38.14, -37.83, -39.65, -33.56, -34.82, -37.31, -35.34, -37.54, -40.05, -42.1, -40.33, -42.72, -45.63, -47.98, -46.96, -45.58, -39.56, -42.37, -44.01, -46.82, -46.01, -41.52, -33.35, -35.45, -34.2, -38.14, -31.96, -35.11, -38.16, -28.24, -32.52, -36.5, -39.57, -41.86, -41.61, -41.71, -43.3, -46.6, -49.13, -51.6, -50.43, -52.44, -37.73, -38.41, -40.31, -32.89, -33.74, -36.4, -40.21, -44.22, -33.76, -36.73, -39.96, -37.03, -37.05, -40.48, -37.44, -40.84, -44.45, -47.59, -32.53, -35.72, -37.82, -39.66, -37.11, -39.43, -37.51, -39.47} end run on setupRecorder() try set tempDir to current application’s NSTemporaryDirectory() set filePath to (tempDir as text) & "vol_monitor.caf" set fileURL to current application’s |NSURL|’s fileURLWithPath:filePath set labelList to {current application’s AVFormatIDKey, current application’s AVSampleRateKey, current application’s AVNumberOfChannelsKey, current application’s AVEncoderAudioQualityKey} set valueList to {"1634492771" as integer, "44100.0" as real, 1, 127} set recordSettings to current application’s NSDictionary’s dictionaryWithObjects:valueList forKeys:labelList set {theRecorder, theError} to current application’s AVAudioRecorder’s alloc()’s initWithURL:fileURL settings:recordSettings |error|:(reference) if theRecorder is missing value then error (theError’s localizedDescription() as text) end if set my recorder to theRecorder recorder’s setMeteringEnabled:true recorder’s |prepareToRecord|() — マイク入力の開始 set success to recorder’s |record|() if not success then display alert "エラー" message "録音の開始に失敗しました。" end if on error errMsg display alert "Setup Error" message errMsg end try end setupRecorder on getVolumeLevel() if recorder is missing value then return -160 recorder’s updateMeters() — 平均音量(dB)を実数として取得 set dbLevel to recorder’s averagePowerForChannel:0 — 小数点以下2桁に丸めて文字列として返す set roundedDb to (round (dbLevel * 100)) / 100 return (roundedDb) end getVolumeLevel |





























