示例#1
0
import board
import busio
from audiocore import WaveFile
try:
    from audioio import AudioOut
except ImportError:
    from audiopwmio import PWMAudioOut as AudioOut
from audiomixer import Mixer

### Using mixer is a workaround for the nRF52840 PWMAudioOut
### not implementing the quiescent_value after a sample
### has completed playing
mixer = Mixer(voice_count=2,
              sample_rate=16000,
              channel_count=2,
              bits_per_sample=16,
              samples_signed=True)

wav_files = ("scanner-left-16k.wav", "scanner-right-16k.wav")

### Use same pins which would be used on a Feather M4 with real DACs
AUDIO_PIN_L = board.A0
AUDIO_PIN_R = board.A1
audio_out = AudioOut(AUDIO_PIN_L, right_channel=AUDIO_PIN_R)

wav_fh = [open(fn, "rb") for fn in wav_files]
wavs = [WaveFile(fh) for fh in wav_fh]

### Voice 0 behaves strangely
### https://github.com/adafruit/circuitpython/issues/3210
示例#2
0
samples_per_waveform = 16
c_waves = 128
buffer_size = c_waves * samples_per_waveform

twopi = 2 * math.pi
a4_hz = 440
a4_st = 9  ### 9 st above C

### TODO - could print error from exact rate or could do this more universally
### This is the sample playback rate for playing the first sample at C4 frequency
rate = round(a4_hz / st_mult[a4_st] * samples_per_waveform)

### voice 0 seems to be louder than the rest, not using it
mixer = Mixer(voice_count=len(st_mult) + 1,
              sample_rate=rate,
              channel_count=1,
              bits_per_sample=16,
              samples_signed=False)

### quiet audible click as this initialises
audio.play(mixer)

### Create samples for each note (semitone) from C4 to C5
note_samples = []
for st, mult in enumerate(st_mult):
    sample = RawSample(array.array("H", [
        round(midpoint + triangle(x * twopi /
                                  (buffer_size / round(c_waves * mult))) * vol)
        for x in range(buffer_size)
    ]),
                       sample_rate=rate)
for wave_type in WAVE_TYPES:
    for octave in range(3, 6):  # [3,4,5]
        for note_letter in note_letters:
            # note with octave e.g. a4
            cur_note = "{}{}".format(note_letter, octave)
            # add wave file to dictionary
            key = "{}{}".format(wave_type, cur_note)
            notes[key] = WaveFile(
                open("notes/{}/{}.wav".format(wave_type, cur_note), "rb"))

# main audio object
audio = AudioOut(left_channel=board.A0, right_channel=board.A1)
# mixer to allow pylyphonic playback
mixer = Mixer(voice_count=8,
              sample_rate=8000,
              channel_count=2,
              bits_per_sample=16,
              samples_signed=True)

audio.play(mixer)

# turn on the rainbow lights
for i, color in enumerate(colors):
    trellis.pixels[i, 0] = color
    trellis.pixels[i, 1] = color
    trellis.pixels[i, 2] = color

# list of keys pressed on the previous iteration
prev_pressed = []

# voice recycling variables