更新其他后台播放音乐,C3支持v1.25.0,修改sant的es-dl

This commit is contained in:
dahanzimin
2025-10-23 19:21:56 +08:00
parent 7b15abef9c
commit b1b24b27a6
15 changed files with 239 additions and 1080 deletions

View File

@@ -3,14 +3,9 @@ Music buzzer
Micropython library for the Music buzzer
=======================================================
#Based on Author: qiren123(MIDI Music) 20220618
#Make changes to instantiation 20220622
#Increase level reversal selection 20220716
dahanzimin From the Mixly Team
@dahanzimin From the Mixly Team
"""
import _thread, gc
from time import sleep_ms
from machine import Pin, PWM
@@ -30,13 +25,15 @@ Letter = 'ABCDEFG#R'
class MIDI():
def __init__(self, pin, volume=100, invert=0, pa_ctrl=None):
self.reset()
self._invert=invert
self._invert = invert
self._pin = pin
self._volume = volume
self._play = False
self._over = True
self._pwm = None
self._pa_ctrl = pa_ctrl
def set_volume(self,volume):
def set_volume(self, volume):
if not 0 <= volume <= 100:
raise ValueError("Volume value is in the range: 0-100")
self._volume=volume
@@ -97,45 +94,60 @@ class MIDI():
self.set_duration(int(tone[(pos + 1):]))
tone = tone[:pos]
def play(self, tune, duration=None):
if self._pa_ctrl: self._pa_ctrl(1)
def play(self, tune, duration=None, pa_delay=100):
if self._pa_ctrl: self._pa_ctrl(1, pa_delay)
self._pwm = PWM(Pin(self._pin), duty=1023 if self._invert else 0)
self._play = True
self._over = False
if duration is None:
self.set_default(tune[0])
else:
self.set_duration(duration)
for tone in tune:
tone = tone.upper()
if not self._play:
break
if tone[0] not in Letter:
continue
midi = self.midi(tone)
self._pwm.duty(1023-self._volume) if self._invert else self._pwm.duty(self._volume)
self._pwm.freq(midi[0])
if self._play: self._pwm.duty((1023-self._volume) if self._invert else self._volume)
if self._play: self._pwm.freq(midi[0])
sleep_ms(midi[1])
self._pwm.freq(400000)
if self._play: self._pwm.freq(400000)
sleep_ms(1)
if self._pa_ctrl: self._pa_ctrl(0)
if self._pa_ctrl: self._pa_ctrl(0, 0)
self._pwm.deinit()
sleep_ms(10)
self._over = True
def pitch(self, freq):
if self._pa_ctrl: self._pa_ctrl(1)
self._pwm = PWM(Pin(self._pin))
self._pwm.duty(1023-self._volume) if self._invert else self._pwm.duty(self._volume)
self._pwm.freq(int(freq))
def play_thread(self, tune, duration=None, pa_delay=100):
self._play = False
while not self._over:
sleep_ms(10)
if not self._play:
gc.collect()
_thread.start_new_thread(self.play, (tune, duration, pa_delay))
sleep_ms(100)
def pitch_time(self, freq, delay):
if self._pa_ctrl: self._pa_ctrl(1)
def pitch(self, freq, pa_delay=100):
if self._pa_ctrl: self._pa_ctrl(1, pa_delay)
self._pwm = PWM(Pin(self._pin))
self._pwm.duty(1023-self._volume) if self._invert else self._pwm.duty(self._volume)
self._pwm.freq(int(freq))
self._pwm.duty((1023-self._volume) if self._invert else self._volume)
self._pwm.freq(int(freq))
def pitch_time(self, freq, delay, pa_delay=100):
if self._pa_ctrl: self._pa_ctrl(1, pa_delay)
self._pwm = PWM(Pin(self._pin))
self._pwm.duty((1023-self._volume) if self._invert else self._volume)
self._pwm.freq(int(freq))
sleep_ms(delay)
if self._pa_ctrl: self._pa_ctrl(0)
if self._pa_ctrl: self._pa_ctrl(0, 0)
self._pwm.deinit()
sleep_ms(10)
def stop(self):
if self._pa_ctrl: self._pa_ctrl(0)
self._play = False
if self._pa_ctrl: self._pa_ctrl(0, 0)
if self._pwm: self._pwm.deinit()
sleep_ms(10)