Linux and python: Combining multiple wave files to one wave file

2024/10/15 5:15:48

I am looking for a way that I can combine multiple wave files into one wave file using python and run it on linux. I don't want to use any add on other than the default shell command line and default python modules. For example, if I have a.wav and b.wav. I want to create a c.wav which start with the content from a.wav then b.wav. I've found wave module, that I can open a wave file and write into a new file. Since i'm really new in this audio world. I still can't figure out how to do it. Below is my code

import struct, wavewaveFileA = wave.open('./a.wav', 'r')
waveFileB = wave.open('./b.wav', 'r')
waveFileC = wave.open('./c.wav', 'w')lengthA = waveFileA.getnframes()
for i in range(0,lengthA):waveFileC.writeframes(waveFileA.readframes(1))lengthB = waveFileB.getnframes()
for i in range(0,lengthB):waveFileC.writeframes(waveFileB.readframes(1))waveFileA.close()
waveFileB.close()
waveFileC.close()

When i run this code, I got this error:

wave.Error: # channels not specified

Please can any one help me?

Answer

You need to set the number of channels, sample width, and frame rate:

waveFileC.setnchannels(waveFileA.getnchannels())
waveFileC.setsampwidth(waveFileA.getsampwidth())
waveFileC.setframerate(waveFileA.getframerate())

If you want to handle a.wav and b.wav having different settings, you'll want to use something like pysox to convert them to the same settings, or for nchannels and sampwidth you may be able to tough through it yourself.

https://en.xdnf.cn/q/117869.html

Related Q&A

How does the in operator determine membership? [duplicate]

This question already has answers here:Set "in" operator: uses equality or identity?(5 answers)Closed 7 years ago.How does the in operator work for Python? In the example below I have two n…

Python Automatically ignore unicode string

Ive been searching to automatically import some files but since Im on Windows i got the unicode error (because of the "C:\Users\..."). Ive been looking to correct this error and found some h…

How to obtain currency rates from this website converter widget python

How can I implement the currency rates on this website and keep the currencies up to date so that i can access them in python from this website and input and output values and currencies types. I need …

Trying to add sums from a csv file in python

I need to add sums of a csv file. The program is a test for a travel reservation system and the file reads like this:availableSTART,reservations,cancellations,availableEND 20,1,0,18I need to subtract r…

Numerical patterns in Python3 [duplicate]

This question already has answers here:How to print without a newline or space(30 answers)Closed 7 years ago.Im fairly new to programming, i have to start learning it for Uni.I have to make a pattern a…

setsockopt before connect for reactor.connectTCP

I have a small python client which needs a setsockopt after create_socket, but before connect. The non-twisted python code is as follows. How can this be expressed in a twisted environment?create_sock…

Manage quotation marks in XPath (lxml)

I want to extract web elements from the table MANUFACTURING AT A GLANCE in the given website. But the name of the row has (single quote). This is interfering with my syntax. How do I overcome this iss…

exception capture in threads and parent

How do you nicely capture exceptions in Python threads?If you have a threaded python app sys.excepthook does not capture errors in children.When a child raises an exception the parent will continue to…

Writing a program that compares 2 numbers in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Run Python3 without activating the virtual environment

My objective is to run Python 3 code on the AWS Lambda Service, which currently only supports Python 2.7. These are the steps I have done.Since I work on a Mac, setup a docker image similar to the AWS …