# mPy - FK0047 audio exercises with i2s, play a wav file
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-12-22  initial release

from	machine	import	Pin, I2S, freq;		freq(240_000_000)
import	math

# ============================ main =============================

# on FK0047: sck=BCK, ws=LCK and sd=DIN
def main():
    i2s = I2S(1, sck=Pin(14), ws=Pin(26), sd=Pin(27), mode=I2S.TX,
              bits=16, format=I2S.STEREO, rate=44100, ibuf=5000)
    with open('alesis.wav', "rb") as f:
        f.seek(44)										# skip header
        buff = bytearray(5000)
        try:
            while True:
                if n := f.readinto(buff): i2s.write(buff[:n])
                else                    : f.seek(44)	# replay, skip header
        except:
            i2s.deinit()
    print("terminated by Ctrl-c")


main()

# ============================= end ===============================
