Ok, thank you.
I am currently trying to create a piano app myself in Swift, but am having some trouble with the response of the keys. On the upper left side of this webpage it says you use UIKit as a framework. What framework do you use to create the music part?
I'm just wondering if it's worth it for me to download the objective-c version and translate it to Swift myself. The problems I'm having with my piano app (AVFoundation) is that there is a latency/delay issue, non responsive behaviour and not able to do precise multi-touch. When I look at your video of this app, it looks exactly what I'm looking for and I'm considering downloading it.
Did you use any other specific framework that's not standard in Xcode? I'm just trying to decide if I should go for it or not.
Thank you for your fast response !
The audio part of the piano is created using CoreAudio - An AUSampler (with a piano SoundFont) attached to a remote IO unit.
I didn't use any custom frameworks - just created some custom views and overrode the touch detection methods. Getting the multi-touch to work is tricky.
Personally I'd recommend just leaving the component in Objective C. The piano only requires about 5 lines of code to add it to a project.
So, just to be clear. Would it be easy for me to add your files (in Objective-C) to my Xcode-project which I wrote in Swift and make it work? I'm new in iOS programming and have only been working with Swift so far.
Would I be able to translate the entire file to Swift or should I just do like you said and add the Objective-C files to my app and try to make a bridge? Just want to make sure I understand correctly. Thank you for responding this fast !
And you are saying that if I want to add your piano to my current project it would only need 5 lines of code?
Here's a guide which explains how to import Objective C to Swift:
You could try to translate the classes into Swift. But the problem may be that if you make one small mistake, the component wouldn't work properly.
To add the component you just need to add the piano view to your view, create a new audio controller instance and set your view as the piano delegate. It is very straightforward.
There's an example in the project file.
Could you perhaps create a bare-bones Swift project that imports the Objective C classes and creates a piano view? It seems there are many of us just getting started with Swift who have the same questions. I understand that this task must be trivial for you but it's still daunting to beginners who don't yet know the syntax. This would be an enormous help!
Thanks, Ben, works beautifully!
The only small issues is that Xcode (7.3) says setDelegate and AudioSessionSetProperty have been deprecated. The code will likely still run into the next major iOS version or two, but maybe a future version of your code will have a workaround?
A few questions:
1) Is it possible to put this in another UIView, so it's not full screen?
2) Is it possible to turn off user interaction and trigger the notes programmatically? I'd like to use this just for monitoring. I'll generate some notes within the app and the keyboard will just visualize them.
1) The piano view overrides UIView so it is possible to nest the piano inside a different view.
2) This isn't enabled by default. It would be fairly easy to disable user interaction (just disable it on the piano's UIView). It would also be possible to highlight keys. Each key stores it's MIDI note as a byte. To highlight note 0x50 for example, you would loop over all the keys and compare the midi note property of each to 0x50. When you found the correct note, you could set it to be pressed using the isPressed: (BOOL) pressed method.
It would be possible to use the component with Cocos2D but it would depend on the details of the implementation - sometimes using UIView animations with Cocos2D causes slowdown. Take a look at this tutorial about using Cocos2D with UIKit - http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
This component includes the ability to trigger sounds based on a SoundFont. You could easily use a different sound font if you wanted to.