LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-19-2009, 03:03 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
Question python alsa: How to play smoothly?


Hi!

I'm working on a personal research project. What I need to do is to be able to play waves at will.

I tried this morning to use alsa to have the output straight into the speakers but I just can't get to play waves smoothly without sound breakage.

I tried with different PCM modes but none worked. I also tried with PCM_NORMAL using a separate thread to not block my wave calculations, but that didn't work either.

So, to keep it simple, take a look at this two versions of the same file:

Simple:
http://bazaar.launchpad.net/~eantora.../WavePlayer.py
With threads:
http://bazaar.launchpad.net/~eantora.../WavePlayer.py

You will need the Wave.py from the project too.
http://bazaar.launchpad.net/~eantora...46/bin/Wave.py

The way to call it is very simple:

./WavePlayer.py 440

That should play a simple A4 sine wave on both channels.

Thanks for any help you could provide.
 
Old 11-21-2009, 02:13 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
I got only silence from your program.

Just looking at the code it seems that you call math.sin a whole lot which is unnecessary: just calculate one period and then repeat it.

The following works for me without any breakage, though I use 8-bit samples because I didn't feel like bit-bashing.
Code:
#!/usr/bin/env python

import alsaaudio, sys

from numpy import arange        # like range, but supports floating point
from math import pi, sin

channels = 2
sample_size = 1                     # bytes per sample
frame_size = channels * sample_size # bytes per frame
frame_rate = 44100                   # frames per second
byte_rate = frame_rate * frame_size # bytes per second
# 1 second worth of data per pcm.write() call
# decrease if shorter notes are needed
period_size = frame_rate

pcm = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK)
pcm.setchannels(channels)
pcm.setformat(alsaaudio.PCM_FORMAT_U8)
def quantize(f):                # map (-1..1) -> [0..256)
    return int((f+1)*127)       # depends on PCM format
pcm.setrate(frame_rate)
pcm.setperiodsize(period_size)

def sine_wave(freq):
    wave = [chr(quantize(sin(x))) * channels for x
            in arange(0, 2*pi, 2*pi / (frame_rate / freq))]
    wave_data = "".join(wave)
    (nwaves, extra_bytes) = divmod(period_size * frame_size, len(wave_data))
    pcm.write((wave_data * nwaves) + wave_data[:extra_bytes])

for i in range(10):
    sine_wave(220)
    sine_wave(440)
 
  


Reply

Tags
alsa, python, sound


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can's get HD video to play smoothly on IBM R50 Laptop xmrkite Linux - Laptop and Netbook 9 11-10-2009 04:29 PM
sound mp3 doesn't play smoothly anotherguy Linux - Newbie 7 11-26-2005 07:27 AM
How do you output PCM sounds linux using python and ALSA? jjdb6185 Programming 1 07-13-2005 03:39 PM
python - make curses play nice w/ os.system() datadriven Programming 0 02-17-2005 04:31 PM
Armagetron won't play smoothly anymore.. System-Core Linux - Software 1 12-11-2004 11:34 AM


All times are GMT -5. The time now is 05:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration