Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

音量計測テスト v3

Posted on 2月 25 by Takaaki Naganoya

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

★Click Here to Open This Script 

(Visited 1 times, 1 visits today)
Posted in Sound System | Tagged 15.0savvy 26.0savvy | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 26, Tahoe
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • Script Debuggerがフリーダウンロードで提供されることに
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • Appleに買収されたPixelmator ProがAppleとしての初アップデート
  • Dock Menu
  • Applicationのactivateを記録する v2
  • Numbersで選択範囲のdateの年を+1する
  • Claris FileMaker Pro 2025(v22)がリリースされた
  • macOS 15.5beta5(24F74)でaliasのキャスティングバグが修正された???
  • シンプルな文字置換
  • 複数の重複検出ルーチンを順次速度計測
  • macOS 15.7.2 スクリプトメニューから実行できなくなった地図系ライブラリ?
  • NaturalLanguage.frameworkを用いて日本語テキストの形態素解析を行う
  • Apple Creator Studioに含まれるKeynote/Pages/Numbersは新バージョン?
  • デフォルトタイトルと重なっているものと指定サイズのものを抽出
  • Adobe InDesignのAppleScript実行エンジンがCarbonからCocoaベースに書き換えられる
  • 画像をAppleScriptでアスキーアート化

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (169) 26.0savvy (41) CotEditor (67) Finder (53) Keynote (122) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (80) Pages (58) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Newt On Project
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • Scripting Additions
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2026年2月
  • 2026年1月
  • 2025年12月
  • 2025年11月
  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC