{"id":1114,"date":"2021-08-02T03:09:09","date_gmt":"2021-08-01T20:09:09","guid":{"rendered":"https:\/\/bigdolphin.com.vn\/?p=1114"},"modified":"2024-03-26T15:28:53","modified_gmt":"2024-03-26T08:28:53","slug":"playing-with-maixduino-recording-and-playing-audio-with-micropython","status":"publish","type":"post","link":"https:\/\/bigdolphin.com.vn\/?p=1114","title":{"rendered":"Playing with Maixduino: recording and playing audio using Micropython"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. Prepare<\/h3>\n\n\n\n<ul><li>Maixduino<\/li><li>1W speaker<\/li><li>Micro SD card<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Format SD card and connect speaker<\/h3>\n\n\n\n<p>Micro SD card should be formatted as FAT32 type. SD card should be placed into Maixduino after saving script to the board. If we save script to the board while card is being inside, MaixPy IDE will save script to SD instead of flash memory.<\/p>\n\n\n\n<p>Connect a 1W speaker to the board as in the picture below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"461\" src=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image-1024x461.png\" alt=\"\" class=\"wp-image-1115\" srcset=\"https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image-1024x461.png 1024w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image-300x135.png 300w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image-768x346.png 768w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image-1536x691.png 1536w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image-1920x864.png 1920w, https:\/\/bigdolphin.com.vn\/wp-content\/uploads\/2021\/08\/image.png 2000w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Connect speaker and Micro SD card to Maixduino<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. Record audio to wav file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from fpioa_manager import fm\nfrom Maix import I2S\nimport audio\n\n# Register i2s(i2s0) pin\nfm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\nfm.register(19,fm.fpioa.I2S0_WS, force=True)\nfm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n\n# Function to record wav file\ndef recorder(filepath,duration=5):\n    # user setting\n    record_rate   = 16000\n    # in seconds, maximum 10s\n    if duration > 10:\n        duration = 10\n    # default seting\n    record_points = 2048\n    record_ch     = 2\n    # Init i2s(i2s0)\n    rec_dev = I2S(I2S.DEVICE_0)\n    rec_dev.channel_config(rec_dev.CHANNEL_0, rec_dev.RECEIVER, align_mode=I2S.STANDARD_MODE)\n    rec_dev.set_sample_rate(record_rate)\n    print(rec_dev)\n    try:\n        # init audio\n        wav_recorder = audio.Audio(path=filepath, is_create=True, samplerate=record_rate)\n        queue = []\n        record_frame_cnt = record_time*record_rate\/\/record_points\n        # Record and save\n        print(\"[REC WAV] Recording in %d seconds...\" %(record_time))\n        for i in range(record_frame_cnt):\n            tmp = rec_dev.record(record_points*record_ch)\n            if len(queue) > 0:\n                ret = wav_recorder.record(queue[0])\n                queue.pop(0)\n            rec_dev.wait_record()\n            queue.append(tmp)\n            #print(\"[REC] \" + str(i) + \":\" + str(time.ticks()))\n        # Done\n        print(\"[REC WAV] Done!\")\n        wav_recorder.finish()\n    except Exception as e:\n        print(\"[Exception] %s\" % (e))\n\n# Record in 5s\nrecorder(\"\/sd\/record.wav\",5)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Play wav file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from fpioa_manager import fm\nfrom Maix import I2S\nimport audio\n\n# Register i2s(i2s1) pin\nfm.register(34,fm.fpioa.I2S1_OUT_D1, force=True)\nfm.register(35,fm.fpioa.I2S1_SCLK, force=True)\nfm.register(33,fm.fpioa.I2S1_WS, force=True)\n\n# Function to play wav file\ndef player(filepath,vol=80):\n    # Init i2s(i2s1)\n    wav_dev = I2S(I2S.DEVICE_1)\n    ret = None\n    try:\n        # Init audio\n        audio_player = audio.Audio(path = filepath)\n        audio_player.volume(vol)\n        # read audio info\n        wav_info = audio_player.play_process(wav_dev)\n        # config i2s according to audio info\n        wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)\n        wav_dev.set_sample_rate(wav_info[1])\n        print(\"[PLAY WAV] Playing %s...\" %(filepath))\n        # loop to play audio\n        while True:\n            ret = audio_player.play()\n            if ret == None:\n                print(\"[ERROR] Audio format error!\")\n                break\n            elif ret==0:\n                break\n        print(\"[PLAY WAV] Done!\")\n        audio_player.finish()\n    except Exception as e:\n        print(\"[Exception] %s\" % (e))\n    return ret\n \n# Play wav file with volume = 100\nplayer(\"\/sd\/record.wav\",100)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Record and playback<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">import time\nfrom fpioa_manager import fm\nfrom Maix import I2S\nimport audio\n\n# Register i2s(i2s0) pin\nfm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\nfm.register(19,fm.fpioa.I2S0_WS, force=True)\nfm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n\n# Register i2s(i2s1) pin\nfm.register(34,fm.fpioa.I2S1_OUT_D1, force=True)\nfm.register(35,fm.fpioa.I2S1_SCLK, force=True)\nfm.register(33,fm.fpioa.I2S1_WS, force=True)\n\n# Function to record and playback\ndef recorder(filepath,duration=5,playback=False):\n    # user setting\n    record_rate   = 16000\n    # in seconds, maximum 10s\n    if duration > 10:\n        duration = 10\n    record_time   = duration\n    # default seting\n    record_points = 2048\n    record_ch     = 2\n    # Init i2s(i2s0)\n    rec_dev = I2S(I2S.DEVICE_0)\n    rec_dev.channel_config(rec_dev.CHANNEL_0, rec_dev.RECEIVER, align_mode=I2S.STANDARD_MODE)\n    rec_dev.set_sample_rate(record_rate)\n    print(rec_dev)\n    # Init i2s(i2s1)\n    wav_dev = I2S(I2S.DEVICE_1)\n    if playback:\n        # config i2s according to audio info\n        wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)\n        wav_dev.set_sample_rate(record_rate)\n    try:\n        # init audio\n        wav_recorder = audio.Audio(path=filepath, is_create=True, samplerate=record_rate)\n        queue = []\n        record_frame_cnt = record_time*record_rate\/\/record_points\n        print(\"[PLAYBACK] Recording in %d seconds...\" %(record_time))\n        # Record and play\n        for i in range(record_frame_cnt):\n            tmp = rec_dev.record(record_points*record_ch)\n            if playback:\n                wav_dev.play(tmp)\n            if len(queue) > 0:\n                ret = wav_recorder.record(queue[0])\n                queue.pop(0)\n            rec_dev.wait_record()\n            queue.append(tmp)\n            #print(\"[REC] \" + str(i) + \":\" + str(time.ticks()))\n        # Done\n        print(\"[PLAYBACK] Done!\")\n        wav_recorder.finish()\n    except Exception as e:\n        print(\"[Exception] %s\" % (e))\n \n# Record and playback in 5s\nrecorder(\"\/sd\/record.wav\",5,True)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recording sound and playing audio file on Maixduino using Micropython.<\/p>\n","protected":false},"author":2,"featured_media":1116,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[10],"tags":[52,31,30,32,51],"_links":{"self":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/1114"}],"collection":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1114"}],"version-history":[{"count":2,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/1114\/revisions"}],"predecessor-version":[{"id":1118,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/1114\/revisions\/1118"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=\/wp\/v2\/media\/1116"}],"wp:attachment":[{"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigdolphin.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}