How to join wav audio files using python – a beginner tutorial

Spread the love

How to join wav audio files using python

Web applications often require to share content in form of audio files with their users. This tutorial is about a news website that generates news files in wav audio formats. You will learn to join wav audio files using python and generate a combined audio file.

Audio files needed for this tutorial are:

1. A welcome.wav file for adding it in start of our generated file.  Welcome to our website

2. A beep sound, beep.wav file. This file is added after each news headline to separate the news headlines

3. A thank-you.wav file, that is embedded at the end of .wav file. This file states  Thank you for visiting out website.

News headline .wav files

i) Safeguarding Hardware from Cyber attack

ii) Better Artificially Intelligent Devices

iii) Restoring computer based art

iv) Efficiency for Fast Data Center Operations

join wav audio files using python

The combined generated file

Following tasks are performed in this tutorial

1. Install Python on your computer

2. Install pydub module and use AudioSegment object

3. Download and use FFMPEG module

4. Create project directory, download and add wav files

5. Create a script file to join wav files using python to one file

Install python to join wav audio files using python

Download and install python3 on your computer. On a MAC, Python can be installed using brew.

brew install python3

Pip or pip3 is automatically installed with python installation.

Install pydub module using pip3

Pip module can be found in Python installation’s Scripts directory. It can be used to install different python modules. pydub module is needed for AudioSegment object. That is used to read and join the wav audio files.

pip3 install pydub

Create a project folder

  1. Create a project directory named: join wav audio files using python.
  2. Create a directory News, inside project directory. News directory contains directories for daily news headlines wav audio files.
  3. Create a directory 2019-03-02, inside news directory. Download and copy the four news files inside this directory.
  4. Create a directory named include, inside news directory. Download and copy welcome, beep and thank-you files in this directory.

FFMPEG module to join wav audio files using python

To work with audio segment and join wav files, we need to install ffmpeg module. Download ffmpeg, unzip, install and put the .executable file in project directory.

Script to join wav audio files using python

In project directory, create a python file  join_wav_files.py. Add code below into it.

#!usr/local/bin/python
import glob

import sys

import os

First line is path to executable to python interpreter. Then import glob, sys and os modules.

  1. Glob module is used to find files in a directory.
  2. Sys module is used to access system parameters and methods.
  3. Os module is used to access operating system specific functions.
from pydub import AudioSegment

From pydub module,  AudioSegment object is imported.

dirpath = "news/"
headingsNewsDir = dirpath+"2019-03-01/"

includeDir = dirpath+"/include/"

generatedFile = "combined_news_file.wav"
  1. Dirpath is set to news directory
  2. headingNewsDir directory contains headline wav files
  3. IncludeDir contains welcome, beep and thank-you files
  4. The generatedFile variable is the name to combined_news_file.wav.

Assign welcome, beep and thank-you files

Create a filenames variable. filenames variable uses glob.glob method that returns files name with full path. It contains all news wav files.

filenames = glob.glob(headingsNewsDir+'*.wav')

welcome  = AudioSegment.from_wav(includeDir + "welcome.wav")

thankyou = AudioSegment.from_wav(includeDir + "thankyou.wav")

beep     = AudioSegment.from_wav(includeDir + "beep.wav")

Using from_wav file method of AudioSegment object, we have opened welcome, thank-you and beep.wav files from the include directory.

Add news headline wav files to array

filenameswithbeep = [welcome, beep]

combined = AudioSegment.empty()

for filename in filenames:

  audiofilename = AudioSegment.from_wav(filename)

  filenameswithbeep.extend([audiofilename, beep])

filenameswithbeep.extend([thankyou]

An array filenameswithbeep is created. Welcome.wav and beep.wav files are assigned to array. In next line, a zero duration AudioSegment object is created and assigned to combined variable.

In a for loop all the file names are opened using audioSegment‘s from_wav method. File name with full path is passed to from_wav method. Each filename is added to the filenameswithbeep array using extend method.

Array contains welcome file and all the news headline file names, separated with a beep file name. After loop is finished, thank-you.wav file is appended to the array.

for fname in filenameswithbeep:

    combined += fname

All the file names in filenames with beep array are looped. Each file name is added to the combined audio-segment.

combined.export(headingsNewsDir + generatedFile, format="wav")

In last line, combined audio segment is exported to headingNews directory and saved in wav format.

Removing audio files from directory

for fname in filenames:

  os.remove(fname)

In last, using os.remove with a filename, files can be deleted form the directory.

Download source code from GitHub

You can download source code for the tutorial from GitHub repository.

join wav audio wav files using python - download source code

Summary

In this tutorial, you learned to install required python modules and created a python script to join different .wav audio format files into one file.

Final Generated file:

Please subscribe our newsletter, follow us on twitter of like us on Facebook to stay updated about upcoming articles.

 

Related Articles

 

Previous Article

Next Article

Check Also

fp growth algorithm in python

A Beginner’s Guide to the FP-Growth Algorithm in Python

Spread the loveLast updated:1st May, 2022FP-Growth Algorithm in Python In the world of data mining …