Exemplo n.º 1
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()

    z_str = fp.readframes(nframes)

    fp.close()

    dtype_map = {1: numpy.int8, 2: numpy.int16, 3: 'special', 4: numpy.int32}
    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' % sampwidth)

    if sampwidth == 3:
        xs = numpy.fromstring(z_str, dtype=numpy.int8).astype(numpy.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = numpy.fromstring(z_str, dtype=dtype_map[sampwidth])

    wave = Wave(ys, framerate)
    return wave
Exemplo n.º 2
0
def read_wave(filename='sound.wav'):
    #Reads a wave file
    fp = open_wave(filename,'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()

    z_str = fp.readframes(nframes)

    fp.close()

    dtype_map={1:np.int8,2:np.int16,3:'special',4:np.int32}

    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' %sampwidth)
    if sampwidth==3:
        xs=np.fromstring(z_str,dtype=np.int8).astype(np.int32)
        ys=(xs[2::3]*256+xs[1::3]8256+xs[0::3])

    else:
        ys=np.fromstring(z_str,dtype=dtype_map[sampwidth])

    if nchannels==2:
        ys=ys[::2]

    wave = Wave(ys,framerate=framerate)
    wave.normalize()
    return wave
def read_wave(filename='sound.wav'):
    # Reads a wave file
    # filename: string
    # return: Wave

    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()	# number of audio channels (1 for mono, 2 for stereo)
    nframes = fp.getnframes()		# number of audio frames
    sampwidth = fp.getsampwidth()	# sample width in bytes
    framerate = fp.getframerate()	# sampling frequency
    
    z_str = fp.readframes(nframes)	# reads and returns at most nframes of audio as a string of bytes
    
    fp.close()

    dtype_map = {1:np.int8, 2:np.int16, 3:'special', 4:np.int32}
    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' % sampwidth)
    
    if sampwidth == 3:
        xs = np.fromstring(z_str, dtype=np.int8).astype(np.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = np.fromstring(z_str, dtype=dtype_map[sampwidth])

    # if it's in stereo, just pull out the first channel
    if nchannels == 2:
        ys = ys[::2]

    #ts = np.arange(len(ys)) / framerate
    wave = Wave(ys, framerate=framerate)
    wave.normalize()

    return wave
Exemplo n.º 4
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()
    
    z_str = fp.readframes(nframes)
    
    fp.close()

    # TODO: generalize this to handle other sample widths
    assert sampwidth == 2
    fmt = 'h'
    dtype = numpy.int16

    ys = numpy.fromstring(z_str, dtype=dtype)
    wave = Wave(ys, framerate)
    return wave
Exemplo n.º 5
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()
    
    z_str = fp.readframes(nframes)
    
    fp.close()

    dtype_map = {1:numpy.int8, 2:numpy.int16, 3:'special', 4:numpy.int32}
    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' % sampwidth)
    
    if sampwidth == 3:
        xs = numpy.fromstring(z_str, dtype=numpy.int8).astype(numpy.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = numpy.fromstring(z_str, dtype=dtype_map[sampwidth])

    # if it's in stereo, just pull out the first channel
    if nchannels == 2:
        ys = ys[::2]

    wave = Wave(ys, framerate)
    return wave
Exemplo n.º 6
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.
    filename: string
    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()
    
    z_str = fp.readframes(nframes)
    
    fp.close()

    dtype_map = {1:np.int8, 2:np.int16, 3:'special', 4:np.int32}
    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' % sampwidth)
    
    if sampwidth == 3:
        xs = np.fromstring(z_str, dtype=np.int8).astype(np.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = np.fromstring(z_str, dtype=dtype_map[sampwidth])

    # if it's in stereo, just pull out the first channel
    if nchannels == 2:
        ys = ys[::2]

    #ts = np.arange(len(ys)) / framerate
    wave = Wave(ys, framerate=framerate)
    wave.normalize()
    return wave
Exemplo n.º 7
0
def play_midi(code:int):
    wavefile = WAV_FILE_TEMPLATE.format(code)
    if PYGAME:
        pygame.mixer.init()
        pygame.mixer.music.load(wavefile)
        pygame.mixer.music.play()
    elif UNIX:
        with open_wave(wavefile, 'rb') as fd, open_oss('/dev/dsp', 'w') as dsp:
            nc, sw, fr, nf, comptype, compname = fd.getparams()
            dsp.setparameters(AFMT_S16_NE, nc, fr)
            data = fd.readframes(nf)
            dsp.write(data)
    else:
        winsound.PlaySound(wavefile, winsound.SND_FILENAME)
Exemplo n.º 8
0
    def __init__(self, filename="sound.wav", framerate=11025):
        self.filename = filename
        self.framerate = framerate
        self.nchannels = 1
        self.sampwidth = 2
        self.bits = self.sampwidth * 8
        self.bound = 2**(self.bits - 1) - 1

        self.fmt = "h"
        self.dtype = np.int16

        self.fp = open_wave("wavs/" + self.filename, "w")
        self.fp.setnchannels(self.nchannels)
        self.fp.setsampwidth(self.sampwidth)
        self.fp.setframerate(self.framerate)
Exemplo n.º 9
0
    def __init__(self,filename='sound.wav',framerate=11025):
        self.filename = filename
        self.framerate=framerate
        self.nchannels=1
        self.sampwidth = 2
        self.bits = self.sampwidth*8
        self.bound = 2**(self.bits-1)-1

        self.fmt='h'
        self.dtype = np.int16

        self.fp = open_wave(self.filename,'w')
        self.fp.setnchannels(self.nchannels)
        self.fp.setsampwidth(self.sampwidth)
        self.fp.setframerate(self.framerate)
Exemplo n.º 10
0
    def write(self, filename='sound.wav'):
        """
        Write a wave file.
        """
        nchannels = 1
        sampwidth = 2  # 16 bits
        bits = sampwidth * 8;
        bound = 2 ** (bits - 1) - 1

        fp = open_wave(filename, 'w')
        fp.setnchannels(nchannels)
        fp.setsampwidth(sampwidth)
        fp.setframerate(self.framerate)

        zs = quantize(self.ys, bound, np.int16)
        fp.writeframes(zs.tostring())
        fp.close()
Exemplo n.º 11
0
    def __init__(self, filename='sound.wav', framerate=11025):
        """Opens the file and sets parameters.
        filename: string
        framerate: samples per second
        """
        self.filename = filename
        self.framerate = framerate
        self.nchannels = 1
        self.sampwidth = 2
        self.bits = self.sampwidth * 8
        self.bound = 2**(self.bits-1) - 1

        self.fmt = 'h'
        self.dtype = np.int16

        self.fp = open_wave(self.filename, 'w')
        self.fp.setnchannels(self.nchannels)
        self.fp.setsampwidth(self.sampwidth)
        self.fp.setframerate(self.framerate)
Exemplo n.º 12
0
    def __init__(self, filename='sound.wav', framerate=11025):
        """Opens the file and sets parameters.

        filename: string
        framerate: samples per second
        """
        self.filename = filename
        self.framerate = framerate
        self.nchannels = 1
        self.sampwidth = 2
        self.bits = self.sampwidth * 8
        self.bound = 2**(self.bits-1) - 1

        self.fmt = 'h'
        self.dtype = np.int16

        self.fp = open_wave(self.filename, 'w')
        self.fp.setnchannels(self.nchannels)
        self.fp.setsampwidth(self.sampwidth)
        self.fp.setframerate(self.framerate)
Exemplo n.º 13
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()
    
    z_str = fp.readframes(nframes)
    
    fp.close()

    dtype_map = {1:np.int8, 2:np.int16, 3:'special', 4:np.int32}
    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' % sampwidth)
    
    if sampwidth == 3:
        xs = np.fromstring(z_str, dtype=np.int8).astype(np.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = np.fromstring(z_str, dtype=dtype_map[sampwidth])

    # if it's in stereo, just pull out the first channel
    ysLeft = ys[::2]
    ysRight = ys[1::2]


    #ts = np.arange(len(ys)) / framerate
    waveLeft = Wave(ysLeft, framerate=framerate)
    waveRight = Wave(ysRight, framerate=framerate)
    waveLeft.normalize()
    waveRight.normalize()
    return waveLeft, waveRight
def read_wave(filename='sound.wav'):
    # Reads a wave file
    # filename: string
    # return: Wave

    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels(
    )  # number of audio channels (1 for mono, 2 for stereo)
    nframes = fp.getnframes()  # number of audio frames
    sampwidth = fp.getsampwidth()  # sample width in bytes
    framerate = fp.getframerate()  # sampling frequency

    z_str = fp.readframes(
        nframes
    )  # reads and returns at most nframes of audio as a string of bytes

    fp.close()

    dtype_map = {1: np.int8, 2: np.int16, 3: 'special', 4: np.int32}
    if sampwidth not in dtype_map:
        raise ValueError('sampwidth %d unknown' % sampwidth)

    if sampwidth == 3:
        xs = np.fromstring(z_str, dtype=np.int8).astype(np.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = np.fromstring(z_str, dtype=dtype_map[sampwidth])

    # if it's in stereo, just pull out the first channel
    if nchannels == 2:
        ys = ys[::2]

    #ts = np.arange(len(ys)) / framerate
    wave = Wave(ys, framerate=framerate)
    wave.normalize()

    return wave
Exemplo n.º 15
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()

    z_str = fp.readframes(nframes)

    fp.close()

    dtype_map = {1: numpy.int8, 2: numpy.int16}
    assert sampwidth in dtype_map

    ys = numpy.fromstring(z_str, dtype=dtype_map[sampwidth])
    wave = Wave(ys, framerate)
    return wave
Exemplo n.º 16
0
def read_wave(filename='sound.wav'):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, 'r')

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()
    
    z_str = fp.readframes(nframes)
    
    fp.close()

    dtype_map = {1:numpy.int8, 2:numpy.int16}
    assert sampwidth in dtype_map
    
    ys = numpy.fromstring(z_str, dtype=dtype_map[sampwidth])
    wave = Wave(ys, framerate)
    return wave
Exemplo n.º 17
0
def read_wave(filename="sound.wav"):
    """Reads a wave file.

    filename: string

    returns: Wave
    """
    fp = open_wave(filename, "r")

    nchannels = fp.getnchannels()
    nframes = fp.getnframes()
    sampwidth = fp.getsampwidth()
    framerate = fp.getframerate()

    z_str = fp.readframes(nframes)

    fp.close()

    dtype_map = {1: np.int8, 2: np.int16, 3: "special", 4: np.int32}
    if sampwidth not in dtype_map:
        raise ValueError("sampwidth %d unknown" % sampwidth)

    if sampwidth == 3:
        xs = np.fromstring(z_str, dtype=np.int8).astype(np.int32)
        ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
    else:
        ys = np.fromstring(z_str, dtype=dtype_map[sampwidth])

    # if it's in stereo, just pull out the first channel
    if nchannels == 2:
        ys = ys[::2]

    ts = np.arange(len(ys)) / framerate
    wave = Wave(ys, ts, framerate)
    wave.normalize()
    return wave
Exemplo n.º 18
0
from wave import open as open_wave
import numpy as np
from filter import ofb
import wave
#import matplotlib.pyplot as plt
#import glob
#from struct import pack
#import scipy.io.wavfile
from wave import open as open_wave

maskFile = input("Please choose a mask sound from the following: ocean, creek, woods, rain \n")

#for filename in glob.glob('*.wav'):
#myAudioLeft, myAudioRight = thinkdsp.read_wave("ocean.wav")
fp = open_wave("../mask/wav/unfiltered/"+maskFile+".wav", 'r')

z_str = fp.readframes(fp.getnframes())
fp.close()
ys = np.fromstring(z_str, dtype=np.int16)

# if it's in stereo, just pull out the first channel
myAudioLeft = ys[::2]
myAudioRight = ys[1::2]


audio_len = myAudioLeft.size
fs = 44100
fade_len = 3*fs
#maxVol=2**15-1.0
kaiser = np.kaiser(2*fade_len, 5)
Exemplo n.º 19
0
plt.show()
ex6.savefig('lab1fig6-1.pdf', bbox_inches='tight')

# Exercise 7: opening and displaying .wav file.

print('\n#### Exercise 7: plotting a .wav file.\n')

print('''
	\n This code will download the required file and it will delete it at the end
	\n I could\'t achieve the same goal using librosa. 
	\n That doesn\'t mean this library is not able to do it.
''')

file_path = './ex7.wav'

if not os.path.exists(file_path):
    URL.urlretrieve('https://tinyurl.com/196959apples', './ex7.wav')

fp = open_wave(file_path, 'r')

strframes = fp.readframes(fp.getnframes())
fp.close()
os.remove(file_path)

ys = np.fromstring(strframes, dtype=np.int16)
ts = np.arange(len(ys)) / fp.getframerate()

plt.plot(ts, ys, 'b-')
plt.xlabel('time(s)', fontsize=16)
plt.ylabel('amplitude', fontsize=16)
plt.show()
Exemplo n.º 20
0
from wave import open as open_wave
import numpy
import pandas as pd

dimension = numpy.zeros(410)
for i in range(1, 10):
    waveFile = open_wave(
        "/Users/ashwini/Downloads/heartdisease-data/training-a/a000" + str(i) +
        ".wav", 'rb')
    nframes = waveFile.getnframes()
    wavFrames = waveFile.readframes(nframes)
    ys = numpy.fromstring(wavFrames, dtype=numpy.int16)
    dimension[i] = ys.shape[0]

for i in range(10, 100):
    waveFile = open_wave(
        "/Users/ashwini/Downloads/heartdisease-data/training-a/a00" + str(i) +
        ".wav", 'rb')
    nframes = waveFile.getnframes()
    wavFrames = waveFile.readframes(nframes)
    ys = numpy.fromstring(wavFrames, dtype=numpy.int16)
    dimension[i] = ys.shape[0]

for i in range(100, 410):
    waveFile = open_wave(
        "/Users/ashwini/Downloads/heartdisease-data/training-a/a0" + str(i) +
        ".wav", 'rb')
    nframes = waveFile.getnframes()
    wavFrames = waveFile.readframes(nframes)
    ys = numpy.fromstring(wavFrames, dtype=numpy.int16)
    dimension[i] = ys.shape[0]