I’m working on an iPad music sequencer and the code that I use for actually playing sound is based on this example.
Basically, CoreAudio calls the RenderTone method from that example every time it needs new samples to play.
I have an OO model for my project in mind and what I want to do is something like: create a Song object, add Tracks to the song. Each Track has multiple Bars (measures) that are made up of multiple Notes. Based on the current 'play' position the right samples will be generated for every note that has to be played at the specified time.
All these objects will be based on the user input in the GUI of the application. But does that mean I’ll have to read the user interface each time the RenderTone method is called, and initiate all my objects (starting with Song, like I described above) so I can calculate my frames?
So what I want to know is: how can I make my Song object available at all times so that I can call something like Song.getSamples(int numberOfSamples, int playPosition); when the RenderTone method is called?
I hope this is clear, it’s not easy to explain so if there’s something I forgot to mention, let me know.
No. The GUI should update the model (
Song), and theRenderTonemethod should also reference theSong. The model should be stored separately from the View (GUI) classes. It may be created by some central object (such as the app controller at the beginning of the program) and handed to the GUI and to the player. Or you could use a Singleton to hold theSongor allSongobjects (like a SingletonSongManagerthat might have acurrentSongproperty).