gpt4 book ai didi

python - 在arduino上运行python脚本

转载 作者:行者123 更新时间:2023-12-03 01:58:21 25 4
gpt4 key购买 nike

我有一个python脚本使用开始时间,结束时间来 slice 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

最佳答案

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

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

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

关于python - 在arduino上运行python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com