Skip to content

Commit 19eb032

Browse files
authored
Merge pull request #22 from FoamyGuy/volume_api_floats
change volume api to 0.0-1.0
2 parents d57dd2e + a49dc40 commit 19eb032

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

adafruit_fruitjam/peripherals.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class Peripherals:
136136
"""Peripherals Helper Class for the FruitJam Library
137137
138138
:param audio_output: The audio output interface to use 'speaker' or 'headphone'
139-
:param safe_volume_limit: The maximum volume allowed for the audio output. Default is 12.
139+
:param safe_volume_limit: The maximum volume allowed for the audio output. Default is 0.75.
140140
Using higher values can damage some speakers, change at your own risk.
141141
:param sample_rate: The sample rate to play back audio data in hertz. Default is 11025.
142142
:param bit_depth: The bits per sample of the audio data. Supports 8 and 16 bits. Default is 16.
@@ -150,7 +150,7 @@ class Peripherals:
150150
def __init__( # noqa: PLR0913, PLR0912
151151
self,
152152
audio_output: str = "headphone",
153-
safe_volume_limit: int = 12,
153+
safe_volume_limit: float = 0.75,
154154
sample_rate: int = 11025,
155155
bit_depth: int = 16,
156156
i2c: busio.I2C = None,
@@ -189,12 +189,12 @@ def __init__( # noqa: PLR0913, PLR0912
189189
else:
190190
self._audio = None
191191

192-
if safe_volume_limit < 1 or safe_volume_limit > 20:
193-
raise ValueError("safe_volume_limit must be between 1 and 20")
192+
if not (0.0 <= safe_volume_limit <= 1.0):
193+
raise ValueError("safe_volume_limit must be between 0.0 and 1.0")
194194
self.safe_volume_limit = safe_volume_limit
195195

196196
self.audio_output = audio_output
197-
self._apply_volume(7)
197+
self._apply_volume(0.35)
198198

199199
self._sd_mounted = False
200200
sd_pins_in_use = False
@@ -321,20 +321,20 @@ def stop_play(self):
321321
self.wavfile.close()
322322

323323
@property
324-
def volume(self) -> int:
324+
def volume(self) -> float:
325325
"""
326-
The volume level of the Fruit Jam audio output. Valid values are 1-20.
326+
The volume level of the Fruit Jam audio output. Valid values are 0.0 - 1.0.
327327
"""
328328
return self._volume
329329

330330
@volume.setter
331-
def volume(self, volume_level: int) -> None:
331+
def volume(self, volume_level: float) -> None:
332332
"""
333-
:param volume_level: new volume level 1-20
333+
:param volume_level: new volume level 0.0 - 1.0
334334
:return: None
335335
"""
336-
if not (1 <= volume_level <= 20):
337-
raise ValueError("Volume level must be between 1 and 20")
336+
if not (0.0 <= volume_level <= 1.0):
337+
raise ValueError("Volume level must be between 0.0 and 1.0")
338338

339339
if volume_level > self.safe_volume_limit:
340340
raise ValueError(
@@ -374,14 +374,14 @@ def _apply_audio_output(self, audio_output: str = None) -> None:
374374
self._dac.headphone_output = self._audio_output == "headphone"
375375
self._dac.speaker_output = self._audio_output == "speaker"
376376

377-
def _apply_volume(self, volume_level: int = None) -> None:
377+
def _apply_volume(self, volume_level: float = None) -> None:
378378
"""
379379
Map the basic volume level to a db value and set it on the DAC.
380380
"""
381381
if volume_level is not None:
382382
self._volume = volume_level
383383
if self._dac is not None:
384-
db_val = map_range(self._volume, 1, 20, -63, 23)
384+
db_val = map_range(self._volume, 0.0, 1.0, -63, 23)
385385
self._dac.dac_volume = db_val
386386

387387
def deinit(self) -> None:

examples/fruitjam_headphone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pobj = adafruit_fruitjam.peripherals.Peripherals(audio_output="headphone")
99

1010
FILES = ["beep.wav", "dip.wav", "rise.wav"]
11-
VOLUMES = [5, 7, 10, 11, 12]
11+
VOLUMES = [0.25, 0.35, 0.50, 0.55, 0.60]
1212

1313
while True:
1414
print("\n=== Headphones Test ===")

examples/fruitjam_speaker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pobj = adafruit_fruitjam.peripherals.Peripherals(audio_output="speaker")
99

1010
FILES = ["beep.wav", "dip.wav", "rise.wav"]
11-
VOLUMES = [5, 7, 10, 11, 12]
11+
VOLUMES = [0.25, 0.35, 0.50, 0.55, 0.60]
1212

1313
while True:
1414
print("\n=== Speaker Test ===")

examples/fruitjam_synthio_speaker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
synth = synthio.Synthesizer(sample_rate=44100)
1313
pobj.audio.play(synth)
14-
VOLUMES = [5, 7, 10, 11, 12]
14+
VOLUMES = [0.25, 0.35, 0.50, 0.55, 0.60]
1515
C_major_scale = [60, 62, 64, 65, 67, 69, 71, 72, 71, 69, 67, 65, 64, 62, 60]
1616
while True:
1717
print("\n=== Synthio Test ===")

0 commit comments

Comments
 (0)