How to Use pacat to Record and Play Audio in Linux

You can use pacat to record and play audio in Linux to capture and play raw encoded audio files on a PulseAudio sound server. We’re here to help you figure out what it is capable of and how to use it.

pacat: The Basics

pacat is a Linux command which allows users to record and playback encoded raw audio files under PulseAudio sound server. This command supports most audio formats (libsndfile supported formats work here too). You can switch different options to perform various activities with audio.

PulseAudio is installed in most Linux OS systems. It is the sound server for WIN32 and POSIX (Portable Operating System Interface). If you have removed this program or your system doesn’t have pacat installed, don’t worry—we will teach you in this guide.

The main usage of pacat command is to record and playback audio streams, and we’re here to help you figure out how it’s done.

Prerequisite

To follow this tutorial, you need:

  • A Linux OS
  • Pulseaudio package to use pacat
  • Sudo privilege (optional)

Installation

You can run the following command to install pacat if your system doesn’t have one installed yet.

Input:

Record and Play Audio in Linux 1

The PulseAudio package contains pacat by default. You can verify the installation using the $ pacat –version command.

Record and Play Audio in Linux

How to Use pacat to Record and Play Audio in Linux

The pacat command provides different options that fit your needs. You can get that with the following command.

Input:

$ pacat --help

Some of pacat’s basic options are as follows:

pacat View Sink and Shorts 

Before you start using pacat itself, you must list the available links and sources to record. You can view the available sinks with the following command:

$ pactl list sinks short
Record and Play Audio in Linux

To list the sources, you can use the following command:

$ pactl list sources short
Record and Play Audio in Linux

As you can see, a short description of sinks and sources are listed. You can remove the short flag to see detailed information like this:

$ pactl list sinks

It gives more detail about each source and sinks.

Use pacat To Record Audio

Recording with pacat is a fairly easy task with the -r or –record flag. You can also specify the file you want to write to, otherwise, it will write to stdout. You can record and write a .wav file using the following command.

Syntax:

$ parec [file_name]

Example:

$ parec example.wav

In the following command, pacat will record sound until you shut down the process with “Ctrl + C” command. You can also choose a specific monitor instead of a source using the -d flag. Either provide ID or name of active sink or other media.

Syntax:

$ parec -d [id_or_name_of_sink] [file_name]

Example:

$ parec -d 1 example.wav

Use pacat To Playback Audio

Now that we’ve recorded the output, we can use -p or –playback flag. You can even provide the sink ID or name.

Syntax:

$ pacat -p  
$ pacat --playback 

With this command, audio will be played from STDIN. You can provide a file name like this:

Syntax:

$ pacat -p [file_name]

Example:

$pacat -p example.wav

Moreover, you can also choose the device and file name like this.

Syntax:

$ pacat -d [sink_id_or_name] [file_name]

Example:

$ pacat -d 0 example.wav

pacat: Set Volume

This is just a simple task with –volume flag followed by the amount you wanted. Its range is from 0 – silent to 65536 – max. 

Syntax:

$ pacat -d [sink] [file_name] --volume=[amount]

Example:

$ pacat -d 2 example.wav --volume=40000

Command Cheat Sheet

To simplify your understanding of the different commands you can also use the following pacat tool on Linux We’ve broken them down to a streamlined list below. 

  • -h or – help: Shows the list of all options.
  • --version: To show the current version installed.
  • -r or --record: To record and write the recorded information to a given file. It will save to STDIN if no file name is provided.
  • -p or --playback: Reads the audio file and plays it back. STDIN is the default if no file name is provided.
  • -v or --verbose: Verbose mode is enabled.
  • -s or --server=[server_ip]: Connect to the server with its IP.
  • -d or --device=[sink_source]: Selects the device to record or play audio. You can select them with their name or ID.
  • --monitor-stream=[index]: Sink of a given index is used to record.
  • --volume=[amount]: Choose the volume for the given audio. The minimum is 0 and the maximum is 65536.
  • --rate=[sample_rate]: Play or record the audio at a given sample rate Its default is 44100Hz.
  • --format=[format_name]: Play or record audio with specified format only. Default is s16ne.
  • --channels=[channel_number]: Play or record audio at given channel numbers.
  • --no-remix: Don’t upmix or downmix channel.

And just like that, you’ve figured out what the pacat tool is able to do, and how to use it the right way. If you have any questions on how to use the pacat command on Linux OS, don’t forget to leave a comment down below.

If this guide helped you, please share it.

Leave a Reply
Related Posts