pyalsaaudio icon indicating copy to clipboard operation
pyalsaaudio copied to clipboard

PCM playback sped up

Open Frodriguez26 opened this issue 7 years ago • 6 comments

` This is a great project! Though I have an issue with the play back speed. I have been searching for an answer for a couple weeks now and I haven't found anything yet.

I am using a slightly modified version of the recordtest.py example from the main github page:

import alsaaudio
import time

f = open("out_test.raw", "wb")
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, cardindex=1

# Set attributes: Mono, 44100 Hz, 16 bit little endian samples
inp.setchannels(1)
inp.setrate(44100)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(160)

loops = 1000000
while loops > 0:
    loops -= 1
    # Read data from device
    l, data = inp.read()

    if l:
        f.write(data)
        time.sleep(.001)

and I am also using a modified version of the playbacktest.py examples which looks like:

import time
import alsaaudio

index = 1
f = open('out_test.raw', 'rb')
# Open the device in playback mode.
out = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, cardindex = index)

# Set attributes: Mono, 44100 Hz, 16 bit little endian frames
out.setchannels(1)
out.setrate(44100)
out.setformat(alsaaudio.PCM_FORMAT_S16_LE)

# The period size controls the internal number of frames per period.
# The significance of this parameter is documented in the ALSA api.
out.setperiodsize(160)

# Read data from stdin
data = f.read(320)
while data:
    out.write(data)
    data = f.read(320)

When I run the recordtest.py, it seems to work fine and I am able to hear the recording with:

aplay -D plughw:1 -f S16_LE -r 44100 out_test.raw

But when I try using playbacktest.py file, it plays the sound very fast. I have tried changing the rate, periodsize and buffer size and none of those change the speed at which the file is being played. I have also tried saving the recording into a wave file then playing it using the playwav.py example and it is still sped up.

I am using a raspberry pi 3 with a usb sound card hooked up to the usb port. Thanks for the help! `

Frodriguez26 avatar May 16 '18 00:05 Frodriguez26

Can confirm this on 0.8.4 as well with similar hardware.

riverar avatar Aug 28 '18 23:08 riverar

Third-ed on 0.8.4 for a PreSonus Audiobox soundcard. Sounds fine on system speakers.

aidanhb avatar Oct 25 '18 23:10 aidanhb

i have raspberry pi zero W, sounds play ok with aplay, very fast with default test... i confirm search fo an answer here..

ozett avatar Dec 04 '18 15:12 ozett

I'll just leave this here, may work or not, (it did on my machine) Specify the device as plughw example: plughw:1,0

ShellAddicted avatar May 17 '19 12:05 ShellAddicted

Same on RPi 4B w/ Ubuntu 20.10 64bit.

@ShellAddicted using plughw sink makes no difference on my end.

Ping @larsimmisch perhaps? Seems this issue has been around since 2018 is still present.

Library installed from pip install

akamensky avatar Jan 07 '21 17:01 akamensky

is this reproducible with v0.9, using the new method to parameterize the pcm object?

ossilator avatar Aug 05 '22 11:08 ossilator

hmm, the plughw hint should have worked if applied correctly, because the most likely reason for the failure is that alsa chose different stream parameters than requested, due to hardware limitations. that shouldn't happen with plughw. this hypothesis can be verified by calling out.dumpinfo().

alternatively, this might have been fixed along with #63, hence my question above.

so i'm closing this as "need more info".

ossilator avatar Feb 01 '24 21:02 ossilator