Skip to content

Commit e98d90a

Browse files
committed
Replace Number with float
1 parent 003a80a commit e98d90a

6 files changed

Lines changed: 24 additions & 33 deletions

File tree

tuney/audio/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import numpy as np
22

3-
from ..types import Number
4-
53
EPSILON = 1e-8
64

75

8-
def apply_gain(data: np.ndarray, number: Number) -> None:
6+
def apply_gain(data: np.ndarray, number: float) -> None:
97
if abs(number - 1.0) > EPSILON:
108
data *= number

tuney/audio/oscillator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
import numpy as np
77

8-
from ..types import Number
9-
108

119
class Oscillator:
12-
period: Number = 2 * np.pi
10+
period: float = 2 * np.pi
1311
# TODO: add intensity to compensate for different energies
1412

1513
@abstractmethod
@@ -23,7 +21,7 @@ def function(self, x: np.ndarray, out: np.ndarray) -> np.ndarray:
2321

2422

2523
class Triangle(Oscillator):
26-
width: Number = 0.5
24+
width: float = 0.5
2725

2826
@override
2927
def function(self, x: np.ndarray, out: np.ndarray) -> np.ndarray:
@@ -40,7 +38,7 @@ def function(self, x: np.ndarray, out: np.ndarray) -> np.ndarray:
4038

4139

4240
class Sawtooth(Triangle):
43-
width: Number = 0
41+
width: float = 0
4442

4543

4644
sawtooth, sine, triangle = Sawtooth(), Sine(), Triangle()

tuney/audio/oscillator_player.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import numpy as np
88
from pydantic import BaseModel
99

10-
from ..types import Number
1110
from . import apply_gain
1211
from . import oscillator as osc
1312
from .player import Player
@@ -16,10 +15,10 @@
1615

1716

1817
class Sound(BaseModel, frozen=True):
19-
period: Number = 0x100
20-
gain: Number = 1.0
21-
fade_in_samples: Number = 0x1000
22-
fade_out_samples: Number = 0x1000
18+
period: float = 0x100
19+
gain: float = 1.0
20+
fade_in_samples: float = 0x1000
21+
fade_out_samples: float = 0x1000
2322

2423

2524
@dc.dataclass
@@ -28,7 +27,7 @@ class OscillatorPlayer(Player):
2827
oscillator_name: str = 'sawtooth'
2928

3029
#: Records the the frame we started to fade out.
31-
_fade_frame: dc.InitVar[Number | None] = None
30+
_fade_frame: dc.InitVar[float | None] = None
3231
_stopping: dc.InitVar[bool] = False
3332

3433
@cached_property
@@ -42,10 +41,10 @@ def stop(self) -> None:
4241
super().stop()
4342

4443
def _fill(self, out: np.ndarray) -> bool | None:
45-
period = cast(float, self.sound.period)
44+
period = self.sound.period
4645
start = self.frame_count % period
4746
end = start + len(out)
48-
ratio = cast(float, self.oscillator.period) / period
47+
ratio = self.oscillator.period / period
4948
wave = np.linspace(start * ratio, end * ratio, len(out))
5049
wave = self.oscillator.function(wave, out=wave)
5150

@@ -58,7 +57,7 @@ def _fill(self, out: np.ndarray) -> bool | None:
5857
else:
5958
gain *= ii.max
6059
apply_gain(wave, gain)
61-
fade_in = cast(float, self.sound.fade_in_samples)
60+
fade_in = self.sound.fade_in_samples
6261
if self.frame_count < fade_in and not self._stopping:
6362
_fade(wave, cast(float, self.frame_count) / fade_in, len(out) / fade_in)
6463

@@ -68,8 +67,8 @@ def _fill(self, out: np.ndarray) -> bool | None:
6867
offset = max(0.0, fade_in - self.frame_count)
6968
self._fade_frame = self.frame_count - offset
7069

71-
fade_out = cast(float, self.sound.fade_out_samples)
72-
elapsed = cast(float, self.frame_count - self._fade_frame)
70+
fade_out = self.sound.fade_out_samples
71+
elapsed = self.frame_count - self._fade_frame
7372
if (start := 1 - elapsed / fade_out) <= 0:
7473
super().stop()
7574
return False

tuney/scale/note.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pydantic import BaseModel
66

7-
from ..types import NoteNumber, Number
7+
from ..types import NoteNumber
88
from .scale import Scale
99

1010

@@ -14,7 +14,7 @@ class Note(BaseModel, frozen=True):
1414
number: NoteNumber
1515

1616
@cached_property
17-
def frequency(self) -> Number:
17+
def frequency(self) -> float:
1818
return self.scale.tuning(self.number)
1919

2020
@staticmethod

tuney/scale/tuning.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import dataclasses as dc
44
from contextlib import nullcontext, suppress
5+
from fractions import Fraction
56
from typing import Protocol, cast, runtime_checkable
67

78
from pydantic import BaseModel
89

9-
from ..types import Fraction, Frequency, NoteNumber, Number
10+
from ..types import Frequency, NoteNumber
1011

1112
# TODO: make sure we can serialize and deserialize Fraction (as str)
1213

@@ -19,16 +20,15 @@ def __call__(self, note_number: NoteNumber) -> Frequency: ...
1920
@runtime_checkable
2021
class ToFrequency(Protocol):
2122
def __call__(
22-
self, root_frequency: Number, octave_change: Number, octaves: Number
23+
self, root_frequency: float, octave_change: float, octaves: float
2324
) -> Frequency: ...
2425

2526

2627
class PitchToFrequency(BaseModel, frozen=True):
2728
#: The base rule for converting a pitch to a frequency
2829
function: str = 'power'
2930

30-
def __call__(self, root: Frequency, change: Number, octaves: Number) -> Number:
31-
change, octaves = cast(float, change), cast(float, octaves)
31+
def __call__(self, root: Frequency, change: float, octaves: float) -> float:
3232
if self.function == 'power':
3333
return root * change**octaves
3434
elif self.function == 'linear':
@@ -45,7 +45,7 @@ class TuningImpl(BaseModel, frozen=True, arbitrary_types_allowed=True):
4545
"""
4646

4747
#: Detune everything, in cents of an octave division
48-
detune: Number = 0
48+
detune: float = 0
4949

5050
#: If limit_denominator is greater than zero, use rounded N-limit just intonation
5151
limit_denominator: int = 0
@@ -57,7 +57,7 @@ class TuningImpl(BaseModel, frozen=True, arbitrary_types_allowed=True):
5757
#: the change is a ratio, so if it's 2, each octave is twice the frequency of the
5858
#: last; for "linear", it's a difference, so if it's 100, each octave would be
5959
#: 100Hz greater in frequency than the previous.
60-
octave_change: Number = 2
60+
octave_change: float = 2
6161

6262
#: The rule for converting a pitch to a frequency
6363
pitch_to_frequency: PitchToFrequency = PitchToFrequency()

tuney/types.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
from collections.abc import Callable
2-
from fractions import Fraction
3-
from typing import Any, TypeAlias
4-
5-
import numpy as np
2+
from typing import Any
63

74
type Milliseconds = float
85
type Seconds = float
96

10-
Number: TypeAlias = int | float | np.floating | np.integer | Fraction
11-
type Frequency = Number # Must be non-negative
7+
type Frequency = float # Must be non-negative
128
type NoteNumber = int # May be negative
139

1410
type Callback = Callable[[], Any]

0 commit comments

Comments
 (0)