-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.0.3 fixed issues with encoding, multiple channels
- Loading branch information
1 parent
58146e0
commit ec8f23b
Showing
6 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import wave | ||
import numpy as np | ||
|
||
def convert_to_mono(input_wav): | ||
# Open the input WAV file | ||
with wave.open(input_wav, 'rb') as input_file: | ||
# Get the parameters of the input file | ||
params = input_file.getparams() | ||
|
||
# Check if the file is stereo | ||
if params.nchannels > 1: | ||
# Read the audio data | ||
frames = input_file.readframes(-1) | ||
audio_data = np.frombuffer(frames, dtype=np.int16) | ||
|
||
# Take the average of the channels to convert to mono | ||
mono_audio_data = np.mean(audio_data.reshape(-1, params.nchannels), axis=1) | ||
|
||
# Create a new WAV file for mono audio | ||
with wave.open(input_wav, 'wb') as output_file: | ||
# Set the parameters for the output file | ||
output_file.setparams((1, params.sampwidth, params.framerate, len(mono_audio_data), params.comptype, params.compname)) | ||
|
||
# Write the mono audio data to the output file | ||
output_file.writeframes(np.int16(mono_audio_data)) | ||
|
||
print(f'{input_wav} converted to mono') | ||
else: | ||
print(f'{input_wav} is already a mono audio file.') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import wave | ||
import struct | ||
|
||
def re_encode(file_name): | ||
|
||
with wave.open(file_name, 'rb') as original_file: | ||
|
||
# Get the original audio parameters | ||
params = original_file.getparams() | ||
|
||
# Check if the sample width is already 16-bit | ||
if params.sampwidth == 2: | ||
|
||
print("The file already has 16-bit samples.") | ||
|
||
elif params.sampwidth == 1: | ||
|
||
# Open a new WAV file with 16-bit samples | ||
file_name = file_name + '_16bit.wav' | ||
|
||
with wave.open(file_name, 'wb') as new_file: | ||
# Set the new audio parameters | ||
new_file.setparams(params) | ||
new_file.setsampwidth(2) | ||
new_file.setnchannels(1) | ||
|
||
# Read and convert each sample | ||
for _ in range(params.nframes): | ||
sample = original_file.readframes(1) | ||
sample_value = struct.unpack("<B", sample)[0] | ||
new_sample_value = (sample_value - 128) * 256 | ||
new_sample = struct.pack("<h", new_sample_value) | ||
new_file.writeframes(new_sample) | ||
|
||
print("Conversion completed. Saved as " + file_name) | ||
else: | ||
print("Unsupported sample width.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters