Advertisement
Guest User

Untitled

a guest
Oct 30th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import time
  2. import board
  3. import busio
  4. import sdcardio
  5. import storage
  6. import os
  7. import audiocore
  8. import audiobusio
  9. import audiomixer
  10. from lcd.lcd import LCD, CursorMode
  11. from lcd.i2c_pcf8574_interface import I2CPCF8574Interface
  12.  
  13. # Setup SD card and MP3
  14. sck = board.GP18
  15. si = board.GP19
  16. so = board.GP20
  17. cs = board.GP21
  18. spi = busio.SPI(sck, si, so)
  19. sdcard = sdcardio.SDCard(spi, cs)
  20. vfs = storage.VfsFat(sdcard)
  21. storage.mount(vfs, "/sd")
  22.  
  23. # Initialize I2S for audio output
  24. audio = audiobusio.I2SOut(board.GP10, board.GP11, board.GP9)
  25.  
  26. # Print files on SD card for debugging
  27. print("Files on SD card:")
  28. for filename in os.listdir("/sd"):
  29. print(filename)
  30.  
  31. # Setup LCD display
  32. i2c_scl = board.GP17
  33. i2c_sda = board.GP16
  34. i2c_address = 0x27 # I2C address of the LCD
  35. i2c = busio.I2C(scl=i2c_scl, sda=i2c_sda)
  36. interface = I2CPCF8574Interface(i2c, i2c_address)
  37. lcd = LCD(interface, num_rows=2, num_cols=16)
  38. lcd.set_cursor_mode(CursorMode.HIDE)
  39.  
  40. # Load and play WAV files
  41. wav_files = ("poucher1.wav", "poucher2.wav")
  42. wavs = [None] * len(wav_files) # holds the loaded WAVs
  43.  
  44. mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=2,
  45. bits_per_sample=16, samples_signed=True, buffer_size=2048)
  46. audio.play(mixer) # attach mixer to audio playback
  47.  
  48. for i, wav_file in enumerate(wav_files):
  49. print(f"Loading: {wav_file}")
  50. with open(f"/sd/{wav_file}", "rb") as file:
  51. wavs[i] = audiocore.WaveFile(file)
  52. mixer.voice[i].play(wavs[i], loop=True) # start each one playing
  53.  
  54. while True:
  55. print("Loops are playing, doing other tasks...")
  56. time.sleep(1)
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement