Post

Replies

Boosts

Views

Activity

Reply to Passing a variable to a function
I found the answer to this question on another forum. My apologies I worded it badly and didn’t know how to edit the post or put my code in block form so it was long winded. It turns out the answer is to call your function on a background thread like this to run a function called Kick_Snare in this case. DispatchQueue.global(qos: .userInitiated).async { Kick_Snare()} The loop then runs in parallel to the user interface and the buttons are available.
Jun ’21
Reply to Passing a variable to a function
import SwiftUI import PlaygroundSupport import AVFoundation PlaygroundPage.current.wantsFullScreenLiveView = true     // variable declarations var playing:Bool = true var t2: Double = 1 var t1: Double = 1 var BPM:Double = 100 let screenWidth  = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height     // end of variable declarations struct DrumMachine: View{          @State var SnarePlayer: AVAudioPlayer?     var body: some View{         Button(action: {             loop1()// start buttonloop1()         }) {             Image(systemName: "play.circle")                 .foregroundColor(.green)         }         .frame(width: 0.2*screenHeight, height: 0.2*screenHeight)         .scaleEffect(5)         .background(Color.clear)         //finish of play button         Button(action: {// stop button             playing = false         }) {             Image(systemName: "stop.circle")                 .foregroundColor(.red)                 .frame(width: 0.2*screenHeight, height: 0.2*screenHeight)                 .scaleEffect(5)                 .background(Color.clear)                 .disabled(false)         }         .onAppear(){Snare(vol: 0)}     }//end of some view          func Snare(vol: Float){         if let  SnareURL = Bundle.main.url(forResource: "Snare Drum", withExtension: "mp3") {             do {                 try self.SnarePlayer = AVAudioPlayer(contentsOf: SnareURL) /// make the audio player                 SnarePlayer?.volume = vol                                  self.SnarePlayer?.play() /// start playing                              } catch {                 print("Couldn't play audio. Error: \(error)")             }             }     }//end of snare function          func loop1() {         while playing ==  true{             for a in 1...4{                 t1 = CFAbsoluteTimeGetCurrent()                 Snare(vol: 2)                 t2 = CFAbsoluteTimeGetCurrent()                 while t2 - t1 < (1) {                     t2 = CFAbsoluteTimeGetCurrent()                 }             }         }     } }//end of struct PlaygroundPage.current.setLiveView(DrumMachine()) PlaygroundPage.current.wantsFullScreenLiveView = true
Jun ’21
Reply to Passing a variable to a function
import SwiftUI import PlaygroundSupport import AVFoundation PlaygroundPage.current.wantsFullScreenLiveView = true     // variable declarations var playing:Bool = true var t2: Double = 1 var t1: Double = 1 var BPM:Double = 100 let screenWidth  = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height     // end of variable declarations struct DrumMachine: View{          @State var SnarePlayer: AVAudioPlayer?     var body: some View{         Button(action: {// start buttonloop1()         }) {             Image(systemName: "play.circle")                 .foregroundColor(.green)         }         .frame(width: 0.2*screenHeight, height: 0.2*screenHeight)         .scaleEffect(5)         .background(Color.clear)         //finish of play button         Button(action: {// stop button             playing = false         }) {             Image(systemName: "stop.circle")                 .foregroundColor(.red)                 .frame(width: 0.2*screenHeight, height: 0.2*screenHeight)                 .scaleEffect(5)                 .background(Color.clear)                 .disabled(false)         }         .onAppear(){Snare(vol: 0)}     }//end of some view          func Snare(vol: Float){         if let  SnareURL = Bundle.main.url(forResource: "Snare Drum", withExtension: "mp3") {             do {                 try self.SnarePlayer = AVAudioPlayer(contentsOf: SnareURL) /// make the audio player                 SnarePlayer?.volume = vol                                  self.SnarePlayer?.play() /// start playing                              } catch {                 print("Couldn't play audio. Error: \(error)")             }             }     }//end of snare function          func loop1() {         while playing ==  true{             for a in 1...4{                 t1 = CFAbsoluteTimeGetCurrent()                 Snare(vol: 2)                 t2 = CFAbsoluteTimeGetCurrent()                 while t2 - t1 < (1) {                     t2 = CFAbsoluteTimeGetCurrent()                 }             }         }     } }//end of struct PlaygroundPage.current.setLiveView(DrumMachine()) PlaygroundPage.current.wantsFullScreenLiveView = true
Jun ’21
Reply to Passing a variable to a function
This code is in Code Block form as far as I know. if you pasted my code into a blank playground on an iPad all the parentheses containing the blocks would be linked. I suppose I could annotate the start and end of every block but it would make it harder to read. I couldn’t find a way of copying an actual playground to the forum.
Jun ’21