Go Down

Topic: vu meter (sound from serial port) help (Read 447 times) previous topic - next topic

SWGLRD420

Hey i've got a question about serial communication in general and application in making a vu-meter which source would be phyton made program which serves as an audio source to be sent over serial port.I just dont understand how much data i can send through serial port in a unit of time.
I made a phyton program which collects audio data and draws a graph inside the app.So i used that data to turn on led's in an arduino program.But it didnt work at all,data was sent too fast ( i think) and i couldnt find out the way to make arduino reconise it as a number.Sorry for my bad techical terms im just getting into this.So if you could please explain it to me.Thank you

slipstick

Does "phyton" perhaps mean python?

If you post your Arduino code it would be easier to see what you are talking about. But you can't send any normal audio over a serial port.

Steve

SWGLRD420

#2
Feb 20, 2018, 09:52 pm Last Edit: Feb 22, 2018, 02:24 pm by SWGLRD420
Oh sorry yes I meant python and here it is
Arduino code:

Code: [Select]
int ledNum=7;
int led[]={3,5,6,9,10,11,12};
int val=0;
int maxval=0;
int value=0;
void setup()
{
int i = 0;
 for(i = 0; i < ledNum; i++)
 {
 pinMode(led[i], OUTPUT);
 }  
Serial.begin(9600);
}
void loop(){
 
if(Serial.available()>0){
val=Serial.read();
delay(10);
 if(val>maxval){
   maxval=val;
 }
value=map(val,0,maxval,0,7);
 for(int i=0;i<ledNum;i++){
 if(value>i){
 digitalWrite(led[i],HIGH);
 }
 else if(value<=i)
 {
 digitalWrite(led[i],LOW);
 }

 }
 }
}



Pyhton code(im using stereo mix as a default recording device,audio source for this sketch)

Code: [Select]
import pyaudio
import numpy as np
import serial
import time

ser1 = serial.Serial('COM3', 9600)
p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16,channels=1,rate=44100,input=True,
             frames_per_buffer=1024)


x=0
for x in range(int(15*44100/2048)):
   time.sleep(.01)
   podatci=np.frombuffer(stream.read(2**11),dtype=np.int16)
   sredina=(np.average(np.abs(podatci*2)))*2
   broj=(200*sredina/2**16)
   ljestve="#"*int(broj)
   print("%d %s"%(broj,ljestve))
   ser1.write(broj)
 
stream.stop_stream()
stream.close()
p.terminate()

   

SWGLRD420

Does "phyton" perhaps mean python?

If you post your Arduino code it would be easier to see what you are talking about. But you can't send any normal audio over a serial port.

Steve
and I dont need normal audio i just need to send it to turn on like max 12 leds on vu meter

davetcc

#4
Feb 21, 2018, 07:45 am Last Edit: Feb 21, 2018, 07:50 am by davetcc
Over the serial port at 9600, the best case is about 960 bytes/sec. it can go a lot faster than that, use one of the higher rates.

Edited:
You need to use code tags to post the code. All the array manipulation brackets have been lost. 
Long time Arduino user who enjoys DIY audio and AV equipment.

SWGLRD420

Here you go I edited post.And when I increase baud rate at 115200 led's just blink.What should I do?

davetcc

#6
Feb 23, 2018, 11:14 am Last Edit: Feb 23, 2018, 11:28 am by davetcc
It's difficult to be complete because the code is both in python and C++/ino, but I'll help where I can:

FIrstly, in the arduino sketch loop() after reading you do a sleep(10), what's the sleep's purpose, you've read data from the serial port, it should just be rendered immediately. You already do the conflation into 10milli tranches in the python code from what I can see.

Python is outputting a number, but Arduino Serial.read is reading a single byte. You can prove by debugging. I would try printing the return value of the Serial.read() for example As you are using the serial port, you'd have to connect a display of some kind or use some other means of debugging.


I would use the serial monitor on the python program and see what it outputs. Then write an appropriate parser in the sketch or find a library that can do it.
Long time Arduino user who enjoys DIY audio and AV equipment.

davetcc

Also probably take a look through the communications area on this forum and consider the use of one of the libraries mentioned:

http://forum.arduino.cc/index.php?board=12.0
Long time Arduino user who enjoys DIY audio and AV equipment.

Go Up