2012-03-07 35 views
1

我在Arduino上运行这段代码。使用Arduino,有没有办法将音频传递给我的PC扬声器?为了让音频能够通过Arduino通过PC扬声器播放,我需要添加些什么?

//This imports the audio class 
#include <PCM.h> 

//This is the sound being played 
const unsigned char sound1[] PROGMEM = {129, 127, 126, 127, 128, 128, 128, 12}; 

//constant variables 
const int knockSensor = A0; 
const int threshold1 = 10; 

//This create a variable 
int sensorReading = 0; 

void setup() { 
    Serial.begin(9600); 
} 

void loop() { 
    sensorReading = analogRead(knockSensor); 
    if (sensorReading >= threshold1) { 
     Serial.println(threshold1); 
     startPlayback(sound1, sizeof(sound1)); 
    } 

    delay(200); 
} 
+0

Arduino如何物理连接到PC扬声器(直接或间接)? – 2012-03-08 18:19:37

回答

2

假设Arduino板没有物理连接到您的PC,您应该通过串行将Arduino正在读取的值发送到PC。在PC上运行的程序将通过串口获得该值,然后可以使用操作系统将该数据作为声音播放。如果你使用的Linux很容易,通过将数据写入/ dev/audio

+0

嗨亚历克斯,我走出了Windows 7.板通过串口连接,但我怎么会通过串口发送音频。 – user1048682 2012-03-08 23:44:32

+0

我在Windows 7上。 – user1048682 2012-03-08 23:45:41

+1

你不能通过串口发送真实的音频信号。但是您可以发送您正在阅读的数据,因此计算机软件将能够收集这些数据以创建计算机可以理解的音频数据。然后你就可以播放这些数据。一个简单的解决方案是使用这些数据创建一个WAV文件,然后使用windows函数SndPlaySound(检查WIN32 API文档)来播放该文件。 – Alex 2012-03-09 10:30:13

相关问题