-
Notifications
You must be signed in to change notification settings - Fork 1
/
Saw.swift
50 lines (41 loc) · 1.83 KB
/
Saw.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Number of samples generated per second.
let sampleRate = 44_100
/// Stateless sawtooth-shaped waveform function.
/// - Parameters:
/// - time: Number of seconds passed since the current oscillation started.
/// - frequency: Frequency of oscillation, in hertz.
/// - Returns: An audio sample in the normalized range of -1.0...1.0
func saw(time: Float, frequency: Float) -> Float {
let phase = time * frequency
return 2 * (phase - (0.5 + phase).rounded(.down))
}
/// Stateful sawtooth-shaped signal.
struct Saw: Signal {
/// Frequency of oscillation, in hertz.
var frequency: Float = 440.0
/// The "volume" of the signal, i.e. the absolute boundary of the range in which this signal oscillates. Default
/// amplitude of 1.0 means that samples returned from ``Saw/next()`` will always stay in `-1.0...1.0` range.
var amplitude: Float = 1.0
/// Number of seconds passed since the current oscillation started. To avoid overflowing, this value is reset t
/// 0.0 after every oscillation.
var currentTime: Float = 0.0
mutating func next() -> Float {
let result = saw(time: currentTime, frequency: frequency)
self.currentTime += 1.0 / Float(sampleRate)
if self.currentTime > 1.0 / self.frequency {
self.currentTime = 0
}
return result * self.amplitude
}
}