Score:0

PulseAudio: How to output the audio from PulseAudio to a wave file

bh flag

I'm using PulseAudio to do some tests on our Call Center platform on Linux.

I'm using command python -m playwright codegen 'https://my-call-center/site/login' -o test.py to open an chrome GUI for testing call.

I have run PulseAudio with profile as below (cat ~/.config/pulse/default.pa):

.include /etc/pulse/default.pa

load-module module-pipe-sink file=/tmp/fifo.out
load-module module-pipe-source channels=2 file=/tmp/fifo.in

set-default-source fifo_input
set-default-sink fifo_output

This will create pipe file /tmp/fifo.out and when I call on the website, the chrome will use /tmp/fifo.out as output (as a speaker). I use cat /tmp/fifo.out | tee output.out to got the audio content.

But the output.out file is too large and couldn't be listen. After I removed all the \x00\x00 in this file (with python), I could be able to hear some words in the file but still not well. (I use Audacity APP to import the file to listen.)

So, I want to know how can I correctly output the audio from PulseAudio into a wave format file.


pulseaudio version 15.99.1

Pilot6 avatar
cn flag
Are you using Ubuntu? Which release?
Robin.Cai avatar
bh flag
Ubuntu 22.04.1 LTS, jammy
Score:0
bh flag

By lots of trials. From the practical law and some experience, I properly handled this problem.

Just need read the fifo.out file as solid frequence and read solid chunk of data.

The script are below:

import os
import time

stream_out_file = '/tmp/fifo.out'
output_file = 'py_t.wav'

if not output_file.endswith('.wav'):
    output_file = output_file + '.wav'

# 经计算 16bit 2ch 44.1kHz 下,每秒读取数据 176400B
# 所以,200ms 有数据 35280B,20ms 有 3528B
# 系统 buf 大小 64k,为 65536B,约 18.57ms (无用)
stream = os.open(stream_out_file, os.O_RDONLY )
with open(output_file, 'wb') as wav_fd:
    start = time.time_ns()                   # start time
    _next = time.time_ns() + 20 * 1000*1000 # next time is after 20ms
    while True:
        if time.time_ns() > _next:   # 每 20ms 读取一次
            _next += 20 * 1000*1000 # next time
            s = os.read(stream, 3528)
            print("read length", len(s))
            wav_fd.write(s)
        else: # 睡 2ms
            time.sleep(0.002)

The numbers above are calculated from the pulseaudio configure. My configures are as below:

.include /etc/pulse/default.pa

load-module module-pipe-sink channels=2 rate=44100 format=s16le file=/tmp/fifo.out
load-module module-pipe-source channels=2  file=/tmp/fifo.in

set-default-source fifo_input
set-default-sink fifo_output

As to why, I can't explain it.

Robin.Cai avatar
bh flag
This doesn't work well enough. Hope someone can give me direction.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.