在arduino上运行python脚本

时间:2015-11-20 11:02:00

标签: python audio arduino

我有一个python脚本来使用开始,结束时间来切片wav文件。我想在arduino中执行它。我可以直接进入arduino吗?如果没有,你能建议我怎么做?

import wave
import pygame
import time
import sys


def slice(infile, outfilename, start_ms, end_ms):
    width = infile.getsampwidth() #Returns sample width in bytes
    rate = infile.getframerate()  #Returns sampling frequency
    fpms = rate / 1000 # frames per ms
    length = (end_ms - start_ms) * fpms
    start_index = start_ms * fpms

    out = wave.open(outfilename, "w")
    out.setparams((infile.getnchannels(), width, rate, length, infile.getcomptype(), infile.getcompname()))

    infile.rewind()             #Rewind the file pointer to the beginning of the audio stream
    anchor = infile.tell()      #Return current file pointer position
    infile.setpos(anchor + start_index)  #Set the file pointer to the specified position
    out.writeframes(infile.readframes(length)) #Write audio frames and make sure nframes is correct


if __name__ == "__main__":

       slice(wave.open("song1.wav", "r"), "out.wav", int(sys.argv[1]), int(sys.argv[2]))

       pygame.mixer.init()
       pygame.mixer.music.load("out.wav")
       pygame.mixer.music.play()
       while pygame.mixer.music.get_busy() == True:
             continue

1 个答案:

答案 0 :(得分:0)

如果您使用的是arduino mega,可以查看PyMite

对于arduino uno,有一篇文章(Is there a way to "compile" Python code onto an Arduino (Uno)?),关于为arduino编译python,其中有一个提到了两个项目pyMCU,另一个是由SOer开发的,你可以给它试一试

但正如他们所说,将其移植到C ++是更容易的方法