Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play musical notes in Swift Playground

Tags:

I am trying to play a short musical note sequence with a default sine wave as sound inside a Swift Playground. At a later point I'd like to replace the sound with a Soundfont but at the moment I'd be happy with just producing some sound.

I want this to be a midi like sequence with direct control over the notes, not something purely audio based. The AudioToolbox seems to provide what I am looking for but I have troubles fully understanding its usage. Here's what I am currently trying

import AudioToolbox  // Creating the sequence      var sequence:MusicSequence = nil var musicSequence = NewMusicSequence(&sequence)  // Creating a track  var track:MusicTrack = nil var musicTrack = MusicSequenceNewTrack(sequence, &track)  // Adding notes  var time = MusicTimeStamp(1.0) for index:UInt8 in 60...72 {     var note = MIDINoteMessage(channel: 0,                                note: index,                                velocity: 64,                                releaseVelocity: 0,                                duration: 1.0 )     musicTrack = MusicTrackNewMIDINoteEvent(track, time, &note)     time += 1 }  // Creating a player      var musicPlayer:MusicPlayer = nil var player = NewMusicPlayer(&musicPlayer)  player = MusicPlayerSetSequence(musicPlayer, sequence) player = MusicPlayerStart(musicPlayer) 

As you can imagine, there's no sound playing. I appreciate any ideas on how to have that sound sequence playing aloud.

like image 913
Bernd Avatar asked May 08 '16 17:05

Bernd


People also ask

What type of code does swift playground use?

But then what? Swift Playgrounds teaches concepts and uses real Swift structure, but it's not real code. It doesn't make an app, it just guides Byte around and solves puzzles. Swift doesn't have a real command called collectGem() after all.


2 Answers

You have to enable the asynchronous mode for the Playground.

Add this at the top (Xcode 7, Swift 2):

import XCPlayground XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 

and your sequence will play.

The same for Xcode 8 (Swift 3):

import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true 
like image 106
Eric Aya Avatar answered Sep 19 '22 21:09

Eric Aya


Working MIDI example in a Swift Playground

import PlaygroundSupport import AudioToolbox  var sequence : MusicSequence? = nil var musicSequence = NewMusicSequence(&sequence)  var track : MusicTrack? = nil var musicTrack = MusicSequenceNewTrack(sequence!, &track)  // Adding notes  var time = MusicTimeStamp(1.0) for index:UInt8 in 60...72 { // C4 to C5     var note = MIDINoteMessage(channel: 0,                                note: index,                                velocity: 64,                                releaseVelocity: 0,                                duration: 1.0 )     musicTrack = MusicTrackNewMIDINoteEvent(track!, time, &note)     time += 1 }  // Creating a player  var musicPlayer : MusicPlayer? = nil var player = NewMusicPlayer(&musicPlayer)  player = MusicPlayerSetSequence(musicPlayer!, sequence) player = MusicPlayerStart(musicPlayer!)  PlaygroundPage.current.needsIndefiniteExecution = true 

Great MIDI reference page with a nice chart

MIDI Notes Reference

like image 25
Cameron Lowell Palmer Avatar answered Sep 17 '22 21:09

Cameron Lowell Palmer