trungtran
Posts: 9
Joined: Mon Oct 06, 2014 11:25 pm

Play audio problems

Tue Oct 28, 2014 12:13 am

I'm working on a parking sensors project.
Whenever I run my program, the camera displays live feed on the screen and speaker play an audio sound. However, the audio give me an distortion noise. I lower the quality of the audio file, change from .wav to .ogg but it still doesn't play the audio that I provided.
I couldn't figure out what cause this problem
Can anyone help me out ? Thanks
Model B Raspberry Pi running on python and I used pygame library

DirkS
Posts: 10602
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Play audio problems

Tue Oct 28, 2014 10:15 am

Are you getting just noise or do you hear the sound file with distortion?

Gr.
Dirk.

trungtran
Posts: 9
Joined: Mon Oct 06, 2014 11:25 pm

Re: Play audio problems

Thu Oct 30, 2014 11:22 pm

I only get noise, which very frustrated
I applied this code into my python program, it does play the audio I wanted to but I get really bad delay on the camera feed. I wanted to display live image on the display and play audio as the same time

pygame.mixer.music.load("/home/pi/Desktop/Project/GreenSound.ogg")
pygame.mixer.music.play()

clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
clock.tick(10)
I'm not sure what is the proper input I should use.

trungtran
Posts: 9
Joined: Mon Oct 06, 2014 11:25 pm

Re: Play audio problems

Thu Oct 30, 2014 11:24 pm

Here is a partial of my code that I have problem with:

import pygame
import pygame.camera


BLACK = (0,0,0)
GREEN = (0,255,0)
YELLOW = (255,255,0)
RED = (255,0,0)

pygame.init() #initialize pygame
lcd = pygame.display.set_mode() #Initialize a screen
pygame.mouse.set_visible(True) #Don't display the mouse
lcd.fill(BLACK) #Set background color for the screen
pygame.display.update() #Actually display it

pygame.camera.init() #Initialize the camera

size = (800, 440) #Size of the camera on the screen

camera = pygame.camera.Camera('/dev/video0', size, 'RGB') #Load a camera

camera.start() #start capturing

textSize = pygame.font.Font(None, 35) #set text size
surface = pygame.Surface(size) #Images as object

while True:
lcd.fill(BLACK)
camera.get_image(surface) #Captures image as surface
lcd.blit(surface, (0,0)) #Project camera image above everything else

distance = 5.0
x = 0
y = 440
width = 800
height = 100

#Just the rectangle changes color
if (distance >= 2):
#Display the text
text = textSize.render('%.1fft'%distance,True, BLACK)#Set Text
rect = text.get_rect(center = (350,460)) #Center the text with the Rectangle
pygame.draw.rect(lcd, GREEN, (x,y,width,height), 0) #Draw the rectangle
lcd.blit(text,rect) #Project the text ontop of the

pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("/home/pi/Desktop/Projects/GreenSound.ogg")
pygame.mixer.music.play(0)

pygame.display.update()

deivid
Posts: 46
Joined: Thu Oct 23, 2014 7:08 am

Re: Play audio problems

Sun Nov 02, 2014 5:18 am

First, test if your problem is your linux install or your python script:

navigate to the directory containing your .wav file and play it with aplay

Second:
Are you playing the sound through HDMI or the analog jack? Or an external usb sound card?

trungtran
Posts: 9
Joined: Mon Oct 06, 2014 11:25 pm

Re: Play audio problems

Sun Nov 02, 2014 5:37 am

I tested the audio file by itself on the pi. It works fine. Play the audio as I expected. I tested the audio by using aplay command and the code without the camera part.
The audio output connected through a analog jack. I'll include a link to my current project.
The problem is whenever the program (include camera code and audio code) compile, the audio give me funny noise.
Here is the link to my current problem:
http://tinypic.com/player.php?v=348pw0y&s=8
I really appriciate your time. Thanks

deivid
Posts: 46
Joined: Thu Oct 23, 2014 7:08 am

Re: Play audio problems

Sun Nov 02, 2014 6:09 am

run in one shell:

Code: Select all

raspivid -p -t 0
to see the camera on the display.

then run

Code: Select all

aplay FILE
or, if you are trying to listen to a microphone:

Code: Select all

arecord | aplay

trungtran
Posts: 9
Joined: Mon Oct 06, 2014 11:25 pm

Re: Play audio problems

Sun Nov 02, 2014 2:12 pm

Anyhow can you show me how to insert those commands into my original python script. I just want to run them together. I tested each component separately it works, but when they all run together the audio doesn't give me the right sound. You think any problem with my coding that need to be fixed ?

Return to “General discussion”