Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
バーチャルテルミンを作る/2019-08-05-llt10
Search
Satoshi SAKAO
August 05, 2019
Programming
1
54
バーチャルテルミンを作る/2019-08-05-llt10
社内のLTイベント「えるLT Vol.10」で発表した資料です
Satoshi SAKAO
August 05, 2019
Tweet
Share
More Decks by Satoshi SAKAO
See All by Satoshi SAKAO
Pkl/2024-04-17-llt31
ottijp
0
16
JavaScriptのデバッグ/2023-09-04-llt30
ottijp
0
76
CDK for TerraformでAzureリソースをデプロイする/2023-05-15-llt29
ottijp
0
57
TWELITEへの誘い/2022-12-27-llt28
ottijp
0
100
ビルドツールBazelを触ってみた/2022-09-28-llt27
ottijp
0
97
HashiCorp Vaultを使ったシークレットのセキュアな一元管理 〜Ansibleを添えて〜/2022-07-12-llt26
ottijp
0
110
AWSインフラのデプロイをCDKでカイゼンする/2022-03-23-llt25
ottijp
0
52
Amazon Timestreamでデータ補間/2021-12-27-llt24
ottijp
0
57
ncurses/2021-05-12-llt21
ottijp
0
150
Other Decks in Programming
See All in Programming
PHPはいつから死んでいるかの調査
chiroruxx
1
400
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
410
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
150
Rethinking UI building strategies @ SFI 2024
letelete
0
270
Elm Form Validation
bkuhlmann
0
510
Snowflakeで眠ったデータを起こそう!
estie
0
120
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
GitHub Copilotのススメ
marcy731
1
200
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
1
1.1k
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
180
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
30
6k
Making Projects Easy
brettharned
108
5.5k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
The Mythical Team-Month
searls
216
42k
[RailsConf 2023] Rails as a piece of cake
palkan
23
4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
19
1.7k
Documentation Writing (for coders)
carmenintech
60
3.9k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
Automating Front-end Workflow
addyosmani
1356
200k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
How STYLIGHT went responsive
nonsquared
92
4.8k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
659
120k
Transcript
バーチャルテルミンを作る Satoshi SAKAO えるLT Vol.10 2019/08/05 (Mon) H1
話すひと H2 インフォコム株式会社 品質マネジメント推進室 " ソフトウェアエンジニア JS (ES6) / Node.js
/ GCP / IoT / iOS (Swift) シュタゲ(コミックス) Satoshi SAKAO @ottijp
話すこと • iOS の Core Audio と Audio Unit •
Audio Unit を使ったバーチャルテルミン H3
H4 モチベーション
テルミンを使ってみたい H5 https://bit.ly/2ZwpAAi
高い!! H6 https://amzn.to/2KqEjGt
実物はあきらめた • バーチャルテルミンっぽいものをiOSアプリで作る • iOSのCore Audioサービスを使う H7
H8 Core Audio と Audio Unit
Core Audio なんぞ? • iOS/macOSのオーディオ基盤 • アプリがオーディオを取り扱うためのフレームワークを提供 • 主なサービス(iOS) •
オーディオの再生・録音 • 変換・コーデック • Audio Unit • MIDI H9
Audio Unit なんぞ? • オーディオ処理をするプラグイン • 主な種類 • 信号処理(エフェクタ) •
ソフトウェアシンセサイザ(楽器) • プログラムによる信号発生 • フォーマット変換 H10 https://support.apple.com/kb/PH25047? viewlocale=ja_JP&locale=ja_JP
H11 Audio Unitを使ったバーチャルテルミン
Audio Unitの作成手順 • 1. Audio Unitの作成 • 2. Audio Unitのストリーム設定
• 3. Audio Unitのレンダラ設定 H12
1. Audio Unitの作成 • Audio Component • Audio Unitを生成する型のようなもの •
製造者: Apple, タイプ: Outputのものを検索して取得 • AudioComponentFindNext ファンクションを使う • Audio Unit • Audio Componentのインスタンス • AudioComponentInstanceNew ファンクションでインスタンス化 • AudioUnitInitialize ファンクションで初期化 H13
1. Audio Unitの作成 H14 // Apple OuputΦʔσΟίϯϙʔωϯτͷऔಘ acd = AudioComponentDescription()
acd.componentType = kAudioUnitType_Output acd.componentSubType = kAudioUnitSubType_RemoteIO acd.componentManufacturer = kAudioUnitManufacturer_Apple acd.componentFlags = 0 acd.componentFlagsMask = 0 guard let ac = AudioComponentFindNext(nil, &acd) else { fatalError("audio component not found") } // ΦʔσΟίϯϙʔωϯτͷΠϯελϯεԽͱॳظԽʢΦʔσΟΦϢχοτͷੜʣ AudioComponentInstanceNew(ac, &au) AudioUnitInitialize(au)
2. Audio Unitのストリーム設定 • AudioUnitSetProperty ファンクションで設定 • フォーマット: PCM •
サンプルデータのデータ型: Float • サンプルレート: 44,100Hz • チャネル数: 2 • フレーム中チャネル数: 1 • パケット中フレーム数: 1 • パケットサイズ: 4 bytes • フレームサイズ: 4 bytes • チャネルサイズ: 32 bits H15
2. Audio Unitのストリーム設定 H16 // PCMετϦʔϜϑΥʔϚοτͷઃఆ var asbd = AudioStreamBasicDescription()
asbd.mSampleRate = sampleRate asbd.mFormatID = kAudioFormatLinearPCM asbd.mFormatFlags = kAudioFormatFlagIsFloat asbd.mChannelsPerFrame = 1 asbd.mFramesPerPacket = 1 asbd.mBytesPerPacket = UInt32(MemoryLayout<Float32>.size) asbd.mBytesPerFrame = UInt32(MemoryLayout<Float32>.size) asbd.mBitsPerChannel = UInt32(8 * MemoryLayout<Float32>.size) asbd.mReserved = 0 AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &asbd, UInt32(MemoryLayout<AudioStreamBasicDescription>.size))
3. Audio Unitのレンダラ設定 • 信号生成するコールバックファンクションを作成 • 設定周波数に従って正弦波のデータサンプルを作成 • 作成したコールバックファンクションをオーディオユニットに設定 •
AudioUnitSetProperty ファンクション H17
3. Audio Unitのレンダラ設定 H18 let renderer: AURenderCallback = { (inRef:
UnsafeMutableRawPointer, inActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>, inTimeStamp: UnsafePointer<AudioTimeStamp>, inBusNumber: UInt32, inNumberFrames: UInt32, ioData: UnsafeMutablePointer<AudioBufferList>?) -> OSStatus in let sampler = inRef.bindMemory(to: SineSampler.self, capacity: 1).pointee let out = ioData?[0].mBuffers.mData for i in 0..<inNumberFrames { let sv = Float32(sin(sampler.phase)) out?.storeBytes(of: sv, toByteOffset: Int(i) * MemoryLayout<Float32>.size, as: Float32.self) sampler.phase = sampler.phase + sampler.phaseDelta } return noErr }
3. Audio Unitのレンダラ設定 H19 // ΦʔσΟΦϢχοτʹϨϯμʔίʔϧόοΫΛઃఆ var callback = AURenderCallbackStruct()
callback.inputProc = renderer let selfPt = UnsafeMutablePointer<SineSampler>.allocate(capacity: 1) let a = UnsafeMutableRawPointer(selfPt) a.storeBytes(of: self, toByteOffset: 0, as: SineSampler.self) callback.inputProcRefCon = a AudioUnitSetProperty(au, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, UInt32(MemoryLayout<AURenderCallbackStruct>.size))
H20 Demo
H21
話したこと • iOS の Core Audio と Audio Unit •
Audio Unit を使ったバーチャルテルミン H22
$ exit H23
H24 Appendix
参考 • What Is Core Audio? • https://developer.apple.com/library/archive/documentation/ MusicAudio/Conceptual/CoreAudioOverview/ WhatisCoreAudio/WhatisCoreAudio.html
• iOSでサイン波を鳴らす (Swift4 × AudioUnit) • https://qiita.com/fooka/items/e89b6e7e45302f7236f2 • Swift3のポインタの基礎知識 · M.Ike • https://mike-neko.github.io/blog/swift-pointer/ H25
iOSのCore Audioアーキテクチャ H26 https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/ CoreAudioOverview/WhatisCoreAudio/WhatisCoreAudio.html