From 1670a5882478139a84e10ebf20ff3828280cbc2a Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 16 Sep 2025 16:33:26 -0500 Subject: [PATCH 1/2] change volume api to 0.0-1.0 --- adafruit_fruitjam/peripherals.py | 24 ++++++++++++------------ examples/fruitjam_headphone.py | 2 +- examples/fruitjam_speaker.py | 2 +- examples/fruitjam_synthio_speaker.py | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/adafruit_fruitjam/peripherals.py b/adafruit_fruitjam/peripherals.py index efe3315..e5458f0 100644 --- a/adafruit_fruitjam/peripherals.py +++ b/adafruit_fruitjam/peripherals.py @@ -136,7 +136,7 @@ class Peripherals: """Peripherals Helper Class for the FruitJam Library :param audio_output: The audio output interface to use 'speaker' or 'headphone' - :param safe_volume_limit: The maximum volume allowed for the audio output. Default is 12. + :param safe_volume_limit: The maximum volume allowed for the audio output. Default is 0.75. Using higher values can damage some speakers, change at your own risk. :param sample_rate: The sample rate to play back audio data in hertz. Default is 11025. :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: def __init__( # noqa: PLR0913, PLR0912 self, audio_output: str = "headphone", - safe_volume_limit: int = 12, + safe_volume_limit: float = 0.75, sample_rate: int = 11025, bit_depth: int = 16, i2c: busio.I2C = None, @@ -189,12 +189,12 @@ def __init__( # noqa: PLR0913, PLR0912 else: self._audio = None - if safe_volume_limit < 1 or safe_volume_limit > 20: - raise ValueError("safe_volume_limit must be between 1 and 20") + if not (0.0 <= safe_volume_limit <= 1.0): + raise ValueError("safe_volume_limit must be between 0.0 and 1.0") self.safe_volume_limit = safe_volume_limit self.audio_output = audio_output - self._apply_volume(7) + self._apply_volume(0.35) self._sd_mounted = False sd_pins_in_use = False @@ -321,20 +321,20 @@ def stop_play(self): self.wavfile.close() @property - def volume(self) -> int: + def volume(self) -> float: """ The volume level of the Fruit Jam audio output. Valid values are 1-20. """ return self._volume @volume.setter - def volume(self, volume_level: int) -> None: + def volume(self, volume_level: float) -> None: """ - :param volume_level: new volume level 1-20 + :param volume_level: new volume level 0.0 - 1.0 :return: None """ - if not (1 <= volume_level <= 20): - raise ValueError("Volume level must be between 1 and 20") + if not (0.0 <= volume_level <= 1.0): + raise ValueError("Volume level must be between 0.0 and 1.0") if volume_level > self.safe_volume_limit: raise ValueError( @@ -374,12 +374,12 @@ def _apply_audio_output(self, audio_output: str = None) -> None: self._dac.headphone_output = self._audio_output == "headphone" self._dac.speaker_output = self._audio_output == "speaker" - def _apply_volume(self, volume_level: int = None) -> None: + def _apply_volume(self, volume_level: float = None) -> None: """ Map the basic volume level to a db value and set it on the DAC. """ if volume_level is not None: self._volume = volume_level if self._dac is not None: - db_val = map_range(self._volume, 1, 20, -63, 23) + db_val = map_range(self._volume, 0.0, 1.0, -63, 23) self._dac.dac_volume = db_val diff --git a/examples/fruitjam_headphone.py b/examples/fruitjam_headphone.py index decd1e5..4773c28 100644 --- a/examples/fruitjam_headphone.py +++ b/examples/fruitjam_headphone.py @@ -8,7 +8,7 @@ pobj = adafruit_fruitjam.peripherals.Peripherals(audio_output="headphone") FILES = ["beep.wav", "dip.wav", "rise.wav"] -VOLUMES = [5, 7, 10, 11, 12] +VOLUMES = [0.25, 0.35, 0.50, 0.55, 0.60] while True: print("\n=== Headphones Test ===") diff --git a/examples/fruitjam_speaker.py b/examples/fruitjam_speaker.py index 64ec015..34bbfc3 100644 --- a/examples/fruitjam_speaker.py +++ b/examples/fruitjam_speaker.py @@ -8,7 +8,7 @@ pobj = adafruit_fruitjam.peripherals.Peripherals(audio_output="speaker") FILES = ["beep.wav", "dip.wav", "rise.wav"] -VOLUMES = [5, 7, 10, 11, 12] +VOLUMES = [0.25, 0.35, 0.50, 0.55, 0.60] while True: print("\n=== Speaker Test ===") diff --git a/examples/fruitjam_synthio_speaker.py b/examples/fruitjam_synthio_speaker.py index a44ce28..2da3782 100644 --- a/examples/fruitjam_synthio_speaker.py +++ b/examples/fruitjam_synthio_speaker.py @@ -11,7 +11,7 @@ synth = synthio.Synthesizer(sample_rate=44100) pobj.audio.play(synth) -VOLUMES = [5, 7, 10, 11, 12] +VOLUMES = [0.25, 0.35, 0.50, 0.55, 0.60] C_major_scale = [60, 62, 64, 65, 67, 69, 71, 72, 71, 69, 67, 65, 64, 62, 60] while True: print("\n=== Synthio Test ===") From a49dc400987cfdf4f4ee8ffdd780f8878f8934d0 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 16 Sep 2025 17:53:26 -0500 Subject: [PATCH 2/2] update volume docstring --- adafruit_fruitjam/peripherals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_fruitjam/peripherals.py b/adafruit_fruitjam/peripherals.py index e5458f0..844ff61 100644 --- a/adafruit_fruitjam/peripherals.py +++ b/adafruit_fruitjam/peripherals.py @@ -323,7 +323,7 @@ def stop_play(self): @property def volume(self) -> float: """ - The volume level of the Fruit Jam audio output. Valid values are 1-20. + The volume level of the Fruit Jam audio output. Valid values are 0.0 - 1.0. """ return self._volume