python - 如何通过 BeagleBone 上的 GPIO 引脚生成声音信号

标签 python audio beagleboard

我正在寻找有关如何在 BeagleBone 上生成合成声音信号的指针/提示,类似于观察tone()函数在 Arduino 上返回的情况。最终,我想在 GPIO 引脚上连接压电元件或扬声器,并听到其中发出的声波。有什么指点吗?

最佳答案

这就是我如何使用 Python 和 PyBBIO 在 Beaglebone 上解决这个问题的方法。 :

#!/usr/bin/python
# Circuit:
# * A Piezo is connected to pin 12 on header P8.        - GPIO1_12
# * A LED is connected to pin 14 on header P8.          - GPIO0_26
# * A button is connected to pin 45 on header P8.       - GPIO2_6
#   Use a pull-down resistor (around 10K ohms) between pin 45 and ground. 
#       3.3v for the other side of the button can be taken from pins 3 or 4 
#       on header P9. Warning: Do not allow 5V to go into the GPIO pins.
# * GND - pin 1 or 2, header P9.

def setup(): # this function will run once, on startup
    pinMode(PIEZO, OUTPUT) # set up pin 12 on header P8 as an output - Piezo
    pinMode(LED, OUTPUT) # set up pin 14 on header P8 as an output - LED
    pinMode(BUTTON, INPUT) # set up pin 45 on header P8 as an input - Button

def loop(): # this function will run repeatedly, until user hits CTRL+C
    if (digitalRead(BUTTON) == HIGH): 
        # was the button pressed? (is 3.3v making it HIGH?) then do:
            buzz()
    delay(10) # don't "peg" the processor checking pin

def delay(j):  #need to overwrite delay() otherwise, it's too slow
    for k in range(1,j):
            pass

def buzz():    #this is what makes the piezo buzz - a series of pulses
               # the shorter the delay between HIGH and LOW, the higher the pitch
    limit = 500     # change this value as needed; 
                    # consider using a potentiometer to set the value
    for j in range(1, limit):
            digitalWrite(PIEZO, HIGH)
            delay(j)
            digitalWrite(PIEZO, LOW)
            delay(j)
            if j==limit/2:
                    digitalWrite(LED, HIGH)
    digitalWrite(LED, LOW) # turn it off

run(setup, loop)

关于python - 如何通过 BeagleBone 上的 GPIO 引脚生成声音信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14007965/

相关文章:

python - python 中的社区检测算法

python - 以列格式将一个表的输出合并到另一个表

iphone - 将部分音频文件复制到 iOS 中的新文件中

signal-processing - Beaglebone 上的 DSP

python - Docker 构建在 `add-apt-repository: not found` 上失败

python - 这两个python语句是一样的吗?

java - Android:- 将录制的音频文件转换为 float 组

ios - 如何使用 Swift 在 iOS 中将音频 mp3 文件转换为原始音频类型?

linux - Beaglebone 中的中断

unix - 无法连接到 beaglebone.local