Skip to content

Commit

Permalink
Fixed the ability of playground oscillator and noise generator to stop
Browse files Browse the repository at this point in the history
  • Loading branch information
aure committed Dec 23, 2021
1 parent ad000a2 commit 491ae3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
21 changes: 10 additions & 11 deletions Sources/AudioKit/Nodes/Generators/PlaygroundNoiseGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import CoreAudio // for UnsafeMutableAudioBufferListPointer
public class PlaygroundNoiseGenerator: Node {
fileprivate lazy var sourceNode = AVAudioSourceNode { [self] _, _, frameCount, audioBufferList in
let ablPointer = UnsafeMutableAudioBufferListPointer(audioBufferList)

if self.isStarted {
for frame in 0..<Int(frameCount) {
// Get signal value for this frame at time.
let value = self.amplitude * Float.random(in: -1 ... 1)

// Set the same value on all channels (due to the inputFormat we have only 1 channel though).
for buffer in ablPointer {
let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
buf[frame] = value
}

for frame in 0..<Int(frameCount) {
// Get signal value for this frame at time.
let value = self.amplitude * Float.random(in: -1 ... 1)

// Set the same value on all channels (due to the inputFormat we have only 1 channel though).
for buffer in ablPointer {
let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
buf[frame] = self.isStarted ? value : 0
}

}
return noErr
}
Expand Down
30 changes: 14 additions & 16 deletions Sources/AudioKit/Nodes/Generators/PlaygroundOscillator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ public class PlaygroundOscillator: Node {
fileprivate lazy var sourceNode = AVAudioSourceNode { [self] _, _, frameCount, audioBufferList in
let ablPointer = UnsafeMutableAudioBufferListPointer(audioBufferList)

if self.isStarted {
let phaseIncrement = (twoPi / Float(Settings.sampleRate)) * self.frequency
for frame in 0..<Int(frameCount) {
// Get signal value for this frame at time.
let index = Int(self.currentPhase / twoPi * Float(self.waveform!.count))
let value = self.waveform![index] * self.amplitude

// Advance the phase for the next frame.
self.currentPhase += phaseIncrement
if self.currentPhase >= twoPi { self.currentPhase -= twoPi }
if self.currentPhase < 0.0 { self.currentPhase += twoPi }
// Set the same value on all channels (due to the inputFormat we have only 1 channel though).
for buffer in ablPointer {
let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
buf[frame] = value
}
let phaseIncrement = (twoPi / Float(Settings.sampleRate)) * self.frequency
for frame in 0..<Int(frameCount) {
// Get signal value for this frame at time.
let index = Int(self.currentPhase / twoPi * Float(self.waveform!.count))
let value = self.waveform![index] * self.amplitude

// Advance the phase for the next frame.
self.currentPhase += phaseIncrement
if self.currentPhase >= twoPi { self.currentPhase -= twoPi }
if self.currentPhase < 0.0 { self.currentPhase += twoPi }
// Set the same value on all channels (due to the inputFormat we have only 1 channel though).
for buffer in ablPointer {
let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
buf[frame] = self.isStarted ? value : 0
}
}
return noErr
Expand Down

0 comments on commit 491ae3d

Please sign in to comment.