0

I am trying to create an app similar to the Reactable.

The user will be able to drag "modules" like an oscillator or filter from a menu into the "play area" and the module will be activated.

I am thinking to initialize the modules as they intersect with the "play area" background object. However, this requires me to name the modules automatically, i.e.:

let osci = AKOscillator()

where osci will automatically count up to be:

let osci1 = AKOscillator()
let osci2 = AKOscillator()
...

etc.

How will I be able to do this?

Thanks

edit: I am trying to use an array by creating an array of

var osciArray = [AKOscillator]()

and in my function to add an oscillator, this is my code:

    let oscis = AKOscillator()
    osciArray.append(oscis)
    osciArray[oscCounter].frequency = freqValue
    osciArray[oscCounter].amplitude = 0.5
    osciArray[oscCounter].start()
    selectedNode.userData = ["counter": oscCounter]
    oscCounter += 1
    currentOutput = osciArray[oscCounter]
    AudioKit.output = currentOutput
    AudioKit.start()

My app builds fine, but once the app starts running on the Simulator I get error : fatal error: Index out of range

  • Doesn't array or collection work for you? – Vlad Apr 13 '17 at 17:02
  • I am trying out an array based on this [thread](http://stackoverflow.com/questions/27716931/create-a-variable-in-swift-with-dynamic-name). However, I am having trouble changing the parameters of the AKOscillator function with an array, such as `osci.frequency = 440` etc – Woon Wu Chyi Apr 13 '17 at 17:15
  • What type of trouble? Don't you mind updating your question by adding some code, errors you're getting and explaining why array doesn't work for you? – Vlad Apr 13 '17 at 17:35
  • I have edited my original post to show my code. Thanks – Woon Wu Chyi Apr 14 '17 at 22:40
  • Index out of range has nothing to do with AudioKit. You are using wrong `oscCounter`. I am not sure why you are incrementing it after adding element and trying to access next element (which is most likely out of range). – Vlad Apr 17 '17 at 15:09
  • I am incrementing the oscCounter so that when I add a new oscillator, it will have a new oscCounter value. their own oscCounter value is saved into the userData of the SKNode so I will access their specific value from there. I think I have solved the issue by initializing the osciArray by setting a hard limit on it, i.e. `var osciArray = AKOscillator(repeating:AKOscillator(), count:3)` – Woon Wu Chyi Apr 17 '17 at 17:10
  • I don't think you solved the issue by setting hard limit. You just not getting fatal error anymore because you're accessing empty object now. Try to debug and make sure you're setting `frequency`, `amplitude` on the same object you're setting `currentOutput` to. – Vlad Apr 17 '17 at 18:02

2 Answers2

0

I haven't used AudioKit, but I read about it a while ago and I have quite a big interest in it. From what I understand from the documentation, it's structured pretty much like SpriteKit: nodes connected together.

I guess then that most classes in the library derive from a base class, just like everything in SpriteKit derives from the SKNode class.

Since you are linking the audio kit nodes with visual representations via SpriteKit nodes, why don't you simply subclass from an SKSpriteNode and add an optional audioNode property with the base class from AudioKit?

That way you can just use SpriteKit to interact directly with the stored audio node property.

BadgerBadger
  • 696
  • 3
  • 12
  • I am trying to understand your suggestion, are you suggesting that I create a subclass of SKSpriteNode that is based on an AudioKit node? I.e. `class SKSpriteNode: AKOscillator { }` or something similar to that? I am new to swift and programming in general so I am still confused. How do I access the SKSpriteNode properties from the original built in class? – Woon Wu Chyi Apr 15 '17 at 23:27
0

I think there's a lot of AudioKit related code in your question, but to answer the question, you only have to look at oscCounter. You don't show its initial value, but I am guessing it was zero. Then you increment it by 1 and try to access osciArray[oscCounter] which has only one element so it should be accessed by osciArray[0]. Move the counter lower and you'll be better off. Furthermore, your oscillators look like local variables, so they'll get lost once the scope is lost. They should be declared as instance variables in your class or whatever this is part of.

Aurelius Prochazka
  • 4,510
  • 2
  • 11
  • 34