Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Take a small step beyond the api

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Take a small step beyond the api

オーディオ入出力APIの向こう側を少しだけ覗いてみた話
2025.05.22. RubyKaigi 2025 事後勉強会 oto

makicamel

May 22, 2025

Transcript

46 slides
  1. PortAudio 初期化して class AudioEngine < FFI::PortAudio::Stream include FFI::PortAudio def initialize(bass_sequencer,

    melody_sequencer) @sources = [bass_sequencer, melody_sequencer] API.Pa_Initialize open(nil, output_params, bass_sequencer.sample_rate, 2 ** 12) start at_exit do @muted = true sleep 0.5 # 無音にしてから終わる close API.Pa_Terminate end end
  2. PortAudio コールバック関数で波データ(サンプリングデータ)を書き込むだけ class AudioEngine < FFI::PortAudio::Stream def process(_input, output, framesPerBuffer,

    _timeInfo, _statusFlags, _userData) samples = Array.new(framesPerBuffer, 0.0) @sources.each do |source| framesPerBuffer.times do |i| # source.generateで波形データを生成する samples[i] += source.generate end end output.write_array_of_float(samples) :paContinue end
  3. QoS(macOS) QoSに応じて優先順位をつけ、スケジューリングする QoSが高いほど応答性とパフォーマンスを重視 QoSが低いほどエネルギー効率を重視 Prioritize Work at the Task Level(Documentation

    Archive)  https://developer.apple.com/library/archive/documentation/ Performance/Conceptual/power_efficiency_guidelines_osx/PrioritizeWorkAtTheTaskLevel.html
  4. スレッド優先度 priority: 97は高いのか? バックグラウンド処理: 1〜9 Ruby, Google Chrome Helper: 31〜34

    iTerm2: 46 ws_main_thread: 79 macOSのGUIアプリのメインスレッド BuiltInSpeakerDevice: 97 ほぼ最高といってよさそう

More Decks by makicamel

Other Decks in Programming