CoreMIDI and Friends
Chris Adamson, CocoaHeads Ann Arbor
            Sept. 8, 2011
Road Map


• MIDI basics
• MIDI APIs on OSX
• MIDI APIs on iOS
MIDI Basics

• MIDI = Musical Instrument Digital Interface
  • Circa 1982!
• Sends messages between devices, not sounds
  • Notes to play (pitch, impact, vibrato), patch
    selection, timing synchronization, etc.
MIDI Terms

• Device — participant in a MIDI network
• Endpoint — one connection into or out of a
 device

• Source — endpoint that sends data
• Destination — endpoint that receives data
MIDI Messages


STATUS   DATA 1   DATA 2
MIDI Messages

• Channel Voice Messages — Note On, Note
  Off, After-touch, Pitch wheel

  • High nybble of status is command, low
    nybble is channel number

• Channel Mode, System Messages
• Various extensions to the spec over the years
http://www.midi.org/techspecs/midimessages.php
MIDI APIs on OSX
• CoreMIDI — Implementation of MIDI
 messaging

• Instrument Units — Audio Units that generate
 sound in response to MIDI events

• Music Device — Sends commands to
 Instrument Units

• Music Sequence / Music Player — Plays MIDI
 (.mid) files
Core MIDI


• C-based API in Audio Toolbox
  • Largely similar to other Core Audio APIs
  • Uses Core Foundation opaque types,
    memory management, etc.
Core MIDI Types

• MIDIClientRef — Holds state for your MIDI
 session

• MIDIDeviceRef — A device (real or virtual)
 with “entities” (MIDIEntityRef), which are
 logical sub-systems

• MIDIEndPointRef — Source or destination
• MIDIPortRef — An input or output port
Creating client & input port

!MIDIClientRef client = NULL;
!MIDIClientCreate(CFSTR("Core MIDI to System Sounds Demo"),
    MyMIDINotifyProc, self, &client);
!
!MIDIPortRef inPort = NULL;
!MIDIInputPortCreate(client, CFSTR("Input port"),
    MyMIDIReadProc, self, &inPort);
Discovering and connecting
               sources
!unsigned long sourceCount = MIDIGetNumberOfSources();
!for (int i = 0; i < sourceCount; ++i) {
!! MIDIEndpointRef src = MIDIGetSource(i);
!! CFStringRef endpointName = NULL;
!! OSStatus nameErr = MIDIObjectGetStringProperty(src,
                       kMIDIPropertyName, &endpointName);
!! if (noErr == nameErr) {
!! ! NSLog (@" source %d: %@n", i, endpointName);
!! }
!! MIDIPortConnectSource(inPort, src, NULL);
!}
Read Proc

• Callback function indicated by
 MIDIInputPortCreate()

  • Receives MIDIPacketList
    • numPackets and MIDIPacket[]
  • Receives user-info/context pointers that you
    set up in MIDIInputPortCreate() and
    MIDIPortConnectSource()
MIDIPacket

• Contains timeStamp, length, and Byte[]
• Byte[] data is the MIDI message
  • data[0]: status
  • data[1]: MIDI Data 1
  • data[2] (if present): MIDI Data 2
Parsing a NOTE ON message

!MIDIPacket *packet = (MIDIPacket *)pktlist->packet;!
!Byte midiCommand = packet->data[0] >> 4;
!// is it a note-on?
!if (midiCommand == 0x09) {
!! Byte note = packet->data[1] & 0x7F;
!! Byte velocity = packet->data[2] & 0x7F;
Now What?
Instrument Units


• Audio Units that generate sound in response to
 MIDI events

  • kAudioUnitType_MusicDevice
• Do not currently exist on iOS
Instrument                         to audio H/W
                        I/O Unit
    Unit


             AU Graph
Instrument                                  to audio H/W
                   Effect Unit   I/O Unit
    Unit


             AU Graph
Instrument                         to audio H/W
                        I/O Unit
    Unit


             AU Graph
Mu
     si
          cD
               ev
                    ic
                         eM
                              ID
                                   IE
                                        ve
                                             nt
                                                  ()


                                                       Instrument                         to audio H/W
                                                                               I/O Unit
                                                           Unit


                                                                    AU Graph
MusicDevice.h
• Small API to deliver MIDI events to
 instrument units

• Not in Xcode documentation. Check out the
 header file

  • Only 4 functions
  • MusicDeviceMIDIEvent() sends status,
    data1, data2 to a MusicDeviceComponent
    (i.e., an instrument Audio Unit)
Demo
MIDI on iOS


• Core MIDI added in iOS 4.2
• Device connectivity is via dock port
  • Custom hardware
  • iPad Camera Connection Kit
MIDI via the CCK
• MIDI-to-USB adapters semi-officially blessed
 by Apple

  • Adapter must be USB MIDI Class-
    compliant (i.e., doesn’t need drivers)

  • Bus-powered devices may not work, due to
    low power supplied by iPad. Powered USB
    devices generally work.

• http://iosmidi.com/devices/
Playing sounds on iOS


• No instrument units on iOS. Options:
  • Synthesize your own with a render callback
  • Play sampled sounds with Audio Queue, AV
    Player, System Sounds, etc.
Demo
Demo
AUSampler


• New instrument audio unit in Lion… and… …
  • Takes a sampled waveform and pitch-shifts it
   to make it into an instrument

  • Call with MusicDeviceMIDIEvent(), just
   like other instrument units
AUSampler
• Configuration is a huge hassle
  • Huge and tota!y undocumented hassle
  • Build an .aupreset with AU Lab utility, or
    load DLS bank or SoundFont 2 files, or
    provide your own files

  • See WWDC Session 411 (“Music in iOS
    and MacOSX”), then file documentation
    bugs against absence of sample code
In Summary
• Musicians love MIDI. Devices are cheap and
 plentiful

• OSX and iOS love media, MIDI included
  • By comparison, Android has crap MIDI
    support (no javax.sound.midi, just .mid file
    support in android.media.JetPlayer)

• If you’re doing Mac or iOS media apps, you
 should consider supporting MIDI device I/O
Also, you should buy my book




http://www.mypearsonstore.com/bookstore/product.asp?isbn=9780321636843
             http://my.safaribooksonline.com/9780321636973
        http://www.informit.com/promotions/promotion.aspx?promo=137039

More Related Content

ODP
Presentation hardware
PDF
Core Audio Cranks It Up
KEY
Continuous Delivery in Ruby
PDF
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
PDF
OpenID Connect - An Emperor or Just New Cloths?
PPTX
Hadoop Summit 2012 | Optimizing MapReduce Job Performance
PDF
Digital digest //24.06.2016
PPTX
Internaliser et industrialiser ses études de satisfaction digitales - Qualtri...
Presentation hardware
Core Audio Cranks It Up
Continuous Delivery in Ruby
CIS13: Bootcamp: Ping Identity OAuth and OpenID Connect In Action with PingFe...
OpenID Connect - An Emperor or Just New Cloths?
Hadoop Summit 2012 | Optimizing MapReduce Job Performance
Digital digest //24.06.2016
Internaliser et industrialiser ses études de satisfaction digitales - Qualtri...

Viewers also liked (11)

PDF
일본에 불어닥친 클라우드 컴퓨팅 열풍의 현황과 전망
PDF
2nd annual io t summit 2016 - Bangalore India
PPTX
Social Media Strategy DRAFT
PDF
Apc's customised digital signage solutions
PDF
ประชุมชี้แจ้งกรอบวิจัย งบประมาณปี 2558
PDF
[SJSU] Social Media Strategy
PDF
Ipsos MORI Political Monitor - August 2014
PDF
Вестник // Digital Blow Mind // Май 2016
PPTX
8 3 giraldo anderson y jhon trejos
PDF
Box Office Best Practices [Webinar]
일본에 불어닥친 클라우드 컴퓨팅 열풍의 현황과 전망
2nd annual io t summit 2016 - Bangalore India
Social Media Strategy DRAFT
Apc's customised digital signage solutions
ประชุมชี้แจ้งกรอบวิจัย งบประมาณปี 2558
[SJSU] Social Media Strategy
Ipsos MORI Political Monitor - August 2014
Вестник // Digital Blow Mind // Май 2016
8 3 giraldo anderson y jhon trejos
Box Office Best Practices [Webinar]
Ad

Similar to Core MIDI and Friends (20)

KEY
Core Audio in iOS 6 (CocoaConf Portland, Oct. '12)
PPT
Core audio
PDF
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
PPT
Lecture# 7 midi file format
PPTX
The MIDI Protocol - Musical Instrument Digital Interface
PDF
Core Audio in iOS 6 (CocoaConf DC, March 2013)
PDF
Core Audio: Don't Be Afraid to Play it LOUD! [360iDev, San Jose 2010]
PDF
Become a rockstar using FOSS!
PDF
MeeBlip micro Build Workshop at InterAccess
PDF
Voice That Matter 2010 - Core Audio
PDF
A wearable MIDI interface using a wireless sensor network
PDF
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
PDF
Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
PDF
Simple User-Friendly MIDI Controller using Arduino.
PPTX
MIDI for A2 music tech students
PDF
Inter-process audio options on iOS
PPTX
Presentation Seminar
PPTX
MA: Presentation Seminar
PDF
Reactable
PDF
Reactable
Core Audio in iOS 6 (CocoaConf Portland, Oct. '12)
Core audio
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
Lecture# 7 midi file format
The MIDI Protocol - Musical Instrument Digital Interface
Core Audio in iOS 6 (CocoaConf DC, March 2013)
Core Audio: Don't Be Afraid to Play it LOUD! [360iDev, San Jose 2010]
Become a rockstar using FOSS!
MeeBlip micro Build Workshop at InterAccess
Voice That Matter 2010 - Core Audio
A wearable MIDI interface using a wireless sensor network
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
Simple User-Friendly MIDI Controller using Arduino.
MIDI for A2 music tech students
Inter-process audio options on iOS
Presentation Seminar
MA: Presentation Seminar
Reactable
Reactable
Ad

More from Chris Adamson (20)

PDF
Whatever Happened to Visual Novel Anime? (AWA/Youmacon 2018)
PDF
Whatever Happened to Visual Novel Anime? (JAFAX 2018)
PDF
Media Frameworks Versus Swift (Swift by Northwest, October 2017)
PDF
Fall Premieres: Media Frameworks in iOS 11, macOS 10.13, and tvOS 11 (CocoaCo...
PDF
CocoaConf Chicago 2017: Media Frameworks and Swift: This Is Fine
PDF
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
PDF
Firebase: Totally Not Parse All Over Again (Unless It Is) (CocoaConf San Jose...
PDF
Building A Streaming Apple TV App (CocoaConf San Jose, Nov 2016)
PDF
Firebase: Totally Not Parse All Over Again (Unless It Is)
PDF
Building A Streaming Apple TV App (CocoaConf DC, Sept 2016)
PDF
Video Killed the Rolex Star (CocoaConf San Jose, November, 2015)
PDF
Video Killed the Rolex Star (CocoaConf Columbus, July 2015)
PDF
Revenge of the 80s: Cut/Copy/Paste, Undo/Redo, and More Big Hits (CocoaConf C...
PDF
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
PDF
Stupid Video Tricks, CocoaConf Seattle 2014
PDF
Stupid Video Tricks, CocoaConf Las Vegas
PDF
Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014)
PDF
Stupid Video Tricks (CocoaConf DC, March 2014)
PDF
Stupid Video Tricks
PDF
Introduction to the Roku SDK
Whatever Happened to Visual Novel Anime? (AWA/Youmacon 2018)
Whatever Happened to Visual Novel Anime? (JAFAX 2018)
Media Frameworks Versus Swift (Swift by Northwest, October 2017)
Fall Premieres: Media Frameworks in iOS 11, macOS 10.13, and tvOS 11 (CocoaCo...
CocoaConf Chicago 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
Firebase: Totally Not Parse All Over Again (Unless It Is) (CocoaConf San Jose...
Building A Streaming Apple TV App (CocoaConf San Jose, Nov 2016)
Firebase: Totally Not Parse All Over Again (Unless It Is)
Building A Streaming Apple TV App (CocoaConf DC, Sept 2016)
Video Killed the Rolex Star (CocoaConf San Jose, November, 2015)
Video Killed the Rolex Star (CocoaConf Columbus, July 2015)
Revenge of the 80s: Cut/Copy/Paste, Undo/Redo, and More Big Hits (CocoaConf C...
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Stupid Video Tricks, CocoaConf Seattle 2014
Stupid Video Tricks, CocoaConf Las Vegas
Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014)
Stupid Video Tricks (CocoaConf DC, March 2014)
Stupid Video Tricks
Introduction to the Roku SDK

Recently uploaded (20)

PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Hindi spoken digit analysis for native and non-native speakers
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Unlock new opportunities with location data.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
The various Industrial Revolutions .pptx
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Five Habits of High-Impact Board Members
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Getting Started with Data Integration: FME Form 101
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PPTX
Modernising the Digital Integration Hub
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPTX
Tartificialntelligence_presentation.pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
CloudStack 4.21: First Look Webinar slides
Hindi spoken digit analysis for native and non-native speakers
Module 1.ppt Iot fundamentals and Architecture
sustainability-14-14877-v2.pddhzftheheeeee
Unlock new opportunities with location data.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Chapter 5: Probability Theory and Statistics
The various Industrial Revolutions .pptx
Getting started with AI Agents and Multi-Agent Systems
1 - Historical Antecedents, Social Consideration.pdf
Five Habits of High-Impact Board Members
Benefits of Physical activity for teenagers.pptx
Taming the Chaos: How to Turn Unstructured Data into Decisions
Getting Started with Data Integration: FME Form 101
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Web Crawler for Trend Tracking Gen Z Insights.pptx
Modernising the Digital Integration Hub
Developing a website for English-speaking practice to English as a foreign la...
Tartificialntelligence_presentation.pptx

Core MIDI and Friends

  • 1. CoreMIDI and Friends Chris Adamson, CocoaHeads Ann Arbor Sept. 8, 2011
  • 2. Road Map • MIDI basics • MIDI APIs on OSX • MIDI APIs on iOS
  • 3. MIDI Basics • MIDI = Musical Instrument Digital Interface • Circa 1982! • Sends messages between devices, not sounds • Notes to play (pitch, impact, vibrato), patch selection, timing synchronization, etc.
  • 4. MIDI Terms • Device — participant in a MIDI network • Endpoint — one connection into or out of a device • Source — endpoint that sends data • Destination — endpoint that receives data
  • 5. MIDI Messages STATUS DATA 1 DATA 2
  • 6. MIDI Messages • Channel Voice Messages — Note On, Note Off, After-touch, Pitch wheel • High nybble of status is command, low nybble is channel number • Channel Mode, System Messages • Various extensions to the spec over the years http://www.midi.org/techspecs/midimessages.php
  • 7. MIDI APIs on OSX • CoreMIDI — Implementation of MIDI messaging • Instrument Units — Audio Units that generate sound in response to MIDI events • Music Device — Sends commands to Instrument Units • Music Sequence / Music Player — Plays MIDI (.mid) files
  • 8. Core MIDI • C-based API in Audio Toolbox • Largely similar to other Core Audio APIs • Uses Core Foundation opaque types, memory management, etc.
  • 9. Core MIDI Types • MIDIClientRef — Holds state for your MIDI session • MIDIDeviceRef — A device (real or virtual) with “entities” (MIDIEntityRef), which are logical sub-systems • MIDIEndPointRef — Source or destination • MIDIPortRef — An input or output port
  • 10. Creating client & input port !MIDIClientRef client = NULL; !MIDIClientCreate(CFSTR("Core MIDI to System Sounds Demo"), MyMIDINotifyProc, self, &client); ! !MIDIPortRef inPort = NULL; !MIDIInputPortCreate(client, CFSTR("Input port"), MyMIDIReadProc, self, &inPort);
  • 11. Discovering and connecting sources !unsigned long sourceCount = MIDIGetNumberOfSources(); !for (int i = 0; i < sourceCount; ++i) { !! MIDIEndpointRef src = MIDIGetSource(i); !! CFStringRef endpointName = NULL; !! OSStatus nameErr = MIDIObjectGetStringProperty(src, kMIDIPropertyName, &endpointName); !! if (noErr == nameErr) { !! ! NSLog (@" source %d: %@n", i, endpointName); !! } !! MIDIPortConnectSource(inPort, src, NULL); !}
  • 12. Read Proc • Callback function indicated by MIDIInputPortCreate() • Receives MIDIPacketList • numPackets and MIDIPacket[] • Receives user-info/context pointers that you set up in MIDIInputPortCreate() and MIDIPortConnectSource()
  • 13. MIDIPacket • Contains timeStamp, length, and Byte[] • Byte[] data is the MIDI message • data[0]: status • data[1]: MIDI Data 1 • data[2] (if present): MIDI Data 2
  • 14. Parsing a NOTE ON message !MIDIPacket *packet = (MIDIPacket *)pktlist->packet;! !Byte midiCommand = packet->data[0] >> 4; !// is it a note-on? !if (midiCommand == 0x09) { !! Byte note = packet->data[1] & 0x7F; !! Byte velocity = packet->data[2] & 0x7F;
  • 16. Instrument Units • Audio Units that generate sound in response to MIDI events • kAudioUnitType_MusicDevice • Do not currently exist on iOS
  • 17. Instrument to audio H/W I/O Unit Unit AU Graph
  • 18. Instrument to audio H/W Effect Unit I/O Unit Unit AU Graph
  • 19. Instrument to audio H/W I/O Unit Unit AU Graph
  • 20. Mu si cD ev ic eM ID IE ve nt () Instrument to audio H/W I/O Unit Unit AU Graph
  • 21. MusicDevice.h • Small API to deliver MIDI events to instrument units • Not in Xcode documentation. Check out the header file • Only 4 functions • MusicDeviceMIDIEvent() sends status, data1, data2 to a MusicDeviceComponent (i.e., an instrument Audio Unit)
  • 22. Demo
  • 23. MIDI on iOS • Core MIDI added in iOS 4.2 • Device connectivity is via dock port • Custom hardware • iPad Camera Connection Kit
  • 24. MIDI via the CCK • MIDI-to-USB adapters semi-officially blessed by Apple • Adapter must be USB MIDI Class- compliant (i.e., doesn’t need drivers) • Bus-powered devices may not work, due to low power supplied by iPad. Powered USB devices generally work. • http://iosmidi.com/devices/
  • 25. Playing sounds on iOS • No instrument units on iOS. Options: • Synthesize your own with a render callback • Play sampled sounds with Audio Queue, AV Player, System Sounds, etc.
  • 26. Demo
  • 27. Demo
  • 28. AUSampler • New instrument audio unit in Lion… and… … • Takes a sampled waveform and pitch-shifts it to make it into an instrument • Call with MusicDeviceMIDIEvent(), just like other instrument units
  • 29. AUSampler • Configuration is a huge hassle • Huge and tota!y undocumented hassle • Build an .aupreset with AU Lab utility, or load DLS bank or SoundFont 2 files, or provide your own files • See WWDC Session 411 (“Music in iOS and MacOSX”), then file documentation bugs against absence of sample code
  • 30. In Summary • Musicians love MIDI. Devices are cheap and plentiful • OSX and iOS love media, MIDI included • By comparison, Android has crap MIDI support (no javax.sound.midi, just .mid file support in android.media.JetPlayer) • If you’re doing Mac or iOS media apps, you should consider supporting MIDI device I/O
  • 31. Also, you should buy my book http://www.mypearsonstore.com/bookstore/product.asp?isbn=9780321636843 http://my.safaribooksonline.com/9780321636973 http://www.informit.com/promotions/promotion.aspx?promo=137039