Skip to content

Instantly share code, notes, and snippets.

@mryssng
Last active February 17, 2020 08:55
Show Gist options
  • Save mryssng/45ac27b5f7e10ecc7742d0a3abfaa026 to your computer and use it in GitHub Desktop.
Save mryssng/45ac27b5f7e10ecc7742d0a3abfaa026 to your computer and use it in GitHub Desktop.
WAVE file read / write.
# -*- coding: utf-8 -*-
"""
Created on 2020/02/13 
Description: WAVE file read / write.
@author: Jam Ytse
"""
import wave
from scipy import fromstring, int16
import numpy as np
def wave_read2ch(filename):
"""2ch WAVE file read.
Extended description of function.
Args:
filename (str): The file name of wave file
Returns:
int: Frame rate of wav file.
float: Normalized coef.
ndarray: Left ch data read from wav file.
ndarray: Right ch data read from wav file.
"""
# Read sound data
wav_obj = wave.open(filename, 'rb')
# Get properties
ch = wav_obj.getnchannels()
width = wav_obj.getsampwidth()
fr = wav_obj.getframerate()
fn = wav_obj.getnframes()
# Show properties
print("Channel: ", ch)
print("Sample width: ", width)
print("Frame Rate: ", fr)
print("Frame num: ", fn)
print("Params: ", wav_obj.getparams())
print("Total time: ", 1.0 * fn / fr)
# Convert Wave_read object to bytes object
data = wav_obj.readframes(wav_obj.getnframes())
wav_data = fromstring(data, dtype=int16)
# Seperate right and left ch
if (wav_obj.getnchannels() == 2):
# Left ch
in_left = np.array(wav_data[::2])
# Right ch
in_right = np.array(wav_data[1::2])
# Close wavw file
wav_obj.close()
# Convert int16 to float16
_min = np.min(in_right)
_max = np.max(in_right)
max_right = max(abs(_min), abs(_max))
_min = np.min(in_left)
_max = np.max(in_left)
max_left = max(abs(_min), abs(_max))
# max_left = max(abs(in_left), abs(min(in_left)))
max_total = max(max_right, max_left)
# Normalize. Range -1.0 to +1.0
in_left = in_left / max_total * 0.8
in_right = in_right / max_total * 0.8
return fr, max_total, in_left, in_right
def wave_write2ch(filename, fr, norm, left, right):
"""2ch WAVE File Write.
Write a numpy array as a WAV file.
Args:
filename (str): The file name of output wave file.
fr (int): The sample rate (in samples/sec).
norm (float): Normalized coef.
left(ndarray): The sound data of left channel.
right(ndarray): The sound data of right channel.
Returns:
None.
Note:
Writes a simple uncompressed WAV file.
"""
out_left = left * norm * 0.9
out_right = right * norm * 0.9
out = np.array([out_left, out_right]).transpose()
out_data = out.astype(int16)
wav_obj = wave.open(filename, 'wb')
wav_obj.setnchannels(2) # stereo
wav_obj.setsampwidth(2) # 16bits
wav_obj.setframerate(fr)
wav_obj.writeframes(out_data.tostring())
wav_obj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment