Introduction
In this lab, you will learn how to use the aplaymidi command, a powerful Linux utility for playing MIDI files. The aplaymidi command allows you to send MIDI data to ALSA MIDI devices, making it a useful tool for audio production and music creation on Linux systems. You will explore the basic usage of aplaymidi, learn how to play MIDI files, and discover advanced options for this command.
Understand the aplaymidi Command
In this step, we will learn about the aplaymidi command, a powerful Linux utility for playing MIDI files. The aplaymidi command allows you to send MIDI data to ALSA MIDI devices, making it a useful tool for audio production and music creation on Linux systems.
First, let's explore the basic usage of the aplaymidi command. To display the available MIDI devices on your system, run the following command:
aplaymidi -l
Example output:
## aplaymidi -l
Port Client name Port name
14:0 Midi Through Midi Through Port-0
20:0 TiMidity TiMidity port 0
20:1 TiMidity TiMidity port 1
This command lists the available MIDI devices on your system, including their client name and port name. You can use this information to identify the MIDI device you want to use for playback.
Next, let's try playing a MIDI file using the aplaymidi command. Assuming you have a MIDI file named example.mid in your ~/project directory, you can play it using the following command:
aplaymidi -p 20:0 ~/project/example.mid
This command will send the MIDI data from the example.mid file to the MIDI device with the port name TiMidity port 0.
The -p option specifies the MIDI device port to use for playback. You can use the port information obtained from the aplaymidi -l command to select the appropriate MIDI device.
Play MIDI Files Using aplaymidi
In this step, we will learn how to use the aplaymidi command to play MIDI files on your Linux system.
First, let's ensure we have a MIDI file available to use. You can download a sample MIDI file or use the example.mid file from the previous step.
To play a MIDI file using aplaymidi, run the following command:
aplaymidi -p 20:0 ~/project/example.mid
Replace ~/project/example.mid with the path to your MIDI file.
The -p 20:0 option specifies the MIDI device port to use for playback. In this case, we're using the "TiMidity port 0" device, which we identified in the previous step.
You should hear the MIDI file being played through your system's audio output.
If you want to play the MIDI file in the background, you can use the & operator to run the command in the background:
aplaymidi -p 20:0 ~/project/example.mid &
This will allow you to continue using the terminal while the MIDI file is playing.
To stop the playback, you can use the kill command with the process ID (PID) of the aplaymidi process. First, find the PID using the ps command:
ps aux | grep aplaymidi
This will show the PID of the running aplaymidi process. Then, use the kill command to stop the playback:
kill [PID]
Replace [PID] with the actual PID of the aplaymidi process.
Explore Advanced aplaymidi Options
In this final step, we will explore some advanced options available with the aplaymidi command.
One useful option is the ability to send MIDI data to multiple MIDI devices simultaneously. This can be achieved using the -p option with a comma-separated list of device ports. For example, to send MIDI data to both "TiMidity port 0" and "TiMidity port 1", you can use the following command:
aplaymidi -p 20:0,20:1 ~/project/example.mid
This will play the MIDI file through both MIDI devices at the same time.
Another advanced option is the ability to adjust the playback volume of the MIDI file. You can use the -v option followed by a value between 0 and 127 to set the volume level. For example, to play the MIDI file at 75% volume, you can use:
aplaymidi -p 20:0 -v 95 ~/project/example.mid
You can also combine the volume option with the multiple device option to adjust the volume for each device individually:
aplaymidi -p 20:0,20:1 -v 95,127 ~/project/example.mid
In this example, the MIDI file will be played at 75% volume on the "TiMidity port 0" device and 100% volume on the "TiMidity port 1" device.
Finally, you can use the --wait option to keep the aplaymidi process running until the MIDI playback is complete. This can be useful when you want to ensure the entire MIDI file is played before your script or program continues:
aplaymidi -p 20:0 --wait ~/project/example.mid
With these advanced options, you can fine-tune the MIDI playback experience and integrate the aplaymidi command into your audio production or music creation workflows.
Summary
In this lab, we first learned about the aplaymidi command, a Linux utility for playing MIDI files. We explored how to display the available MIDI devices on the system and how to use the aplaymidi command to play a MIDI file by specifying the target MIDI device. We then delved deeper into playing MIDI files using aplaymidi, covering the basic command structure and options to select the appropriate MIDI device for playback. Finally, we explored advanced aplaymidi options, such as controlling the playback volume and looping the MIDI file.



