Esempio n. 1
0
def play_audio(audio, start=0, stop=None, save=None, batch_size=1024):
    player = pyaudio.PyAudio()
    if isinstance(audio, str):
        input_stream = wave.openfp(open(audio, 'rb'))
    else:
        input_stream = wave.openfp(BytesIO(audio.get_wav_data()))

    output_stream = player.open(
        format=player.get_format_from_width(input_stream.getsampwidth()),
        channels=input_stream.getnchannels(),
        rate=input_stream.getframerate(),
        output=True)
    # play stream
    batch = input_stream.readframes(batch_size)
    save = open(save, 'wb') if save is not None else save
    i = 0
    while batch and (i + 1) * batch_size >= start and (stop is None or (i - 1) * batch_size < stop):
        if stop is not None and i * batch_size >= stop:
            batch = batch[:(stop % batch_size)]
        if start and i * batch_size < start:
            batch = batch[(start % batch_size):]
        output_stream.write(batch)
        if save is not None:
            save.write(batch)
        batch = input_stream.readframes(batch_size)
    return audio
def getEmbedding(filenames):
    #the output is a list of files
    output = []

    #find the shortest file length
    for file in filenames:
        a = wave.openfp(file, mode="rb")
        try:
            shortest
        except NameError:
            var_exists = False
        else:
            var_exists = True
        if not var_exists:
            shortest = a.getnframes()
        else:
            if (shortest > a.getnframes()):
                shortest = a.getnframes()

    #embed each file at the shortest length
    for file in filenames:
        a = wave.openfp(file, mode="rb")
        b = a.readframes(shortest)
        output.append(b)

    return output
def fetchDataAndlabels(train_files,annotation,classes):
    Y = []
    X = []
    for filename in train_files:
        w = wv.read(filename)
        w_ = wave.openfp(filename)
        maxval = 2 ** ((w_.getsampwidth() * (8) - 1))
        fs = w[0]
        audioData = w[1]
        audioData = audioData / maxval
        audioData = (audioData + 1) / 2
        head_tail = os.path.split(filename)
        A = annotation[annotation[:, 0] == head_tail[1], :]
        for row in A:
            mLabel = row[1]
            startA = int(row[2])
            stopA = int(row[3])
            for i in range(startA, (stopA - 4000), 100):
                c = classes.index(mLabel)
                Y.append(c)
                # print(c)
                dat = np.asarray(audioData[i:i + 4000])
                # dat= (dat+np.ones(np.shape(dat)))/2
                dat.resize((1, 250, 16))
                X.append(dat)
                # X=np.concatenate((X,dat),axis=0)
                # print('ok')

    return X,Y
Esempio n. 4
0
    def split_wav(self, mode):
        import io
        import wave
        segments_file = self.recipe_path.joinpath("data", mode, "segments")
        logger.info(f"processing {str(segments_file)} file ...")
        segments = dict()
        with smart_open(segments_file, "r") as f:
            for line in tqdm(f,
                             total=get_num_lines(segments_file),
                             ncols=params.NCOLS):
                split = line.strip().split()
                uttid, wavid, start, end = split[0], split[1], float(
                    split[2]), float(split[3])
                if wavid in segments:
                    segments[wavid].append((uttid, start, end))
                else:
                    segments[wavid] = [(uttid, start, end)]

        wav_scp = self.recipe_path.joinpath("data", mode, "wav.scp")
        logger.info(f"processing {str(wav_scp)} file ...")
        manifest = dict()
        with smart_open(wav_scp, "r") as rf:
            for line in tqdm(rf,
                             total=get_num_lines(wav_scp),
                             ncols=params.NCOLS):
                wavid, cmd = line.strip().split(" ", 1)
                if not wavid in segments:
                    continue
                cmd = cmd.strip().rstrip(' |').split()
                if cmd[0] == 'sph2pipe':
                    cmd[0] = str(SPH2PIPE_PATH)
                p = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
                fp = io.BytesIO(p.stdout)
                with wave.openfp(fp, "rb") as wav:
                    fr = wav.getframerate()
                    nf = wav.getnframes()
                    for uttid, start, end in segments[wavid]:
                        fs, fe = int(fr * start -
                                     SAMPLE_MARGIN), int(fr * end +
                                                         SAMPLE_MARGIN)
                        if fs < 0 or fe > nf:
                            continue
                        wav.rewind()
                        wav.setpos(fs)
                        signal = wav.readframes(fe - fs)
                        p = uttid.find('-')
                        if p != -1:
                            tar_path = self.target_path.joinpath(
                                mode, uttid[:p])
                        else:
                            tar_path = self.target_path.joinpath(mode)
                        tar_path.mkdir(mode=0o755, parents=True, exist_ok=True)
                        wav_file = tar_path.joinpath(uttid + ".wav")
                        with wave.open(str(wav_file), "wb") as wf:
                            wf.setparams(wav.getparams())
                            wf.writeframes(signal)
                        manifest[uttid] = (str(wav_file), fe - fs)
        return manifest
Esempio n. 5
0
def test_wav(h, f):
    import wave
    if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
        return None
    f.seek(0)
    try:
        w = wave.openfp(f, 'r')
    except (EOFError, wave.Error):
        return None
    return 'wav', w.getframerate(), w.getnchannels(), w.getnframes(
    ), 8 * w.getsampwidth()
Esempio n. 6
0
 def process_text_only(self, mode):
     import wave
     logger.info(f"processing text only from \"{mode}\" ...")
     wav_manifest = dict()
     for wav_file in self.target_path.joinpath(mode).rglob("*.wav"):
         uttid = wav_file.stem
         with wave.openfp(str(wav_file), "rb") as wav:
             samples = wav.getnframes()
         wav_manifest[uttid] = (str(wav_file), samples)
     txt_manifest = self.get_transcripts(mode)
     self.make_manifest(mode, wav_manifest, txt_manifest)
Esempio n. 7
0
def test_wav(h, f):
    import wave

    # 'RIFF' <len> 'WAVE' 'fmt ' <len>
    if not h.startswith(b"RIFF") or h[8:12] != b"WAVE" or h[12:16] != b"fmt ":
        return None
    f.seek(0)
    try:
        w = wave.openfp(f, "r")
    except (EOFError, wave.Error):
        return None
    return ("wav", w.getframerate(), w.getnchannels(), w.getnframes(), 8 * w.getsampwidth())
Esempio n. 8
0
def test_wav(h, f):
    import wave
    # 'RIFF' <len> 'WAVE' 'fmt ' <len>
    if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
        return None
    f.seek(0)
    try:
        w = wave.openfp(f, 'r')
    except (EOFError, wave.Error):
        return None
    return ('wav', w.getframerate(), w.getnchannels(),
                   w.getnframes(), 8*w.getsampwidth())
Esempio n. 9
0
 def convertToWave(self):
     output = self.combineChannels()
     outs = []
     for x in range(0, len(output)):
         outs.append(struct.pack('f', output[x]))
     self.name = ''.join([self.name, ".wav"])
     wav = w.openfp(self.name, 'w')
     wav.setparams((1, 4, 44100, len(outs), "NONE", ""))
     out_str = ''.join(outs)
     wav.writeframes(out_str)
     wav.close()
     return
Esempio n. 10
0
 def rebuild(self, mode):
     import wave
     logger.info(f"rebuilding \"{mode}\" ...")
     wav_manifest, txt_manifest = dict(), dict()
     for wav_file in self.target_path.joinpath(mode).rglob("*.wav"):
         uttid = wav_file.stem
         with wave.openfp(str(wav_file), "rb") as wav:
             samples = wav.getnframes()
         wav_manifest[uttid] = (str(wav_file), samples)
         txt_file = str(wav_file).replace('wav', 'txt')
         if Path(txt_file).exists():
             txt_manifest[uttid] = (str(txt_file), '-')
     self.make_manifest(mode, wav_manifest, txt_manifest)
Esempio n. 11
0
def play_audio(audio):
    p = pyaudio.PyAudio()
    f = wave.openfp(BytesIO(audio.get_wav_data()))

    stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                    channels=f.getnchannels(),
                    rate=f.getframerate(),
                    output=True)
    # play stream
    data = f.readframes(1024)
    while data:
        stream.write(data)
        data = f.readframes(1024)
Esempio n. 12
0
def rc4_wave(key, input_filename, output_filename):
    ''' encrypts/decrypts the wave input file to the wave output file file using the key
    (string, string, string) -> None
    '''
    # change key into byte form
    key_b = utf82bytes(key)
    # use wave.openfp() to open inputfile and outputfile
    myfile = wave.openfp(input_filename, 'rb')
    newfile = wave.openfp(output_filename, 'wb')
    # use getparams() to get the header part
    header = myfile.getparams()
    # use getnframes() to get the number of frames
    n = myfile.getnframes()
    # use readframes() to find the plaintext
    plaintext_b = myfile.readframes(n)
    # setparams with header so the outputfile is playable
    newfile.setparams(header)
    # use rc4 to en/decrypt data and write into outputfile
    data = rc4(key_b, plaintext_b)
    newfile.writeframes(data)
    # close files
    myfile.close()
    newfile.close()
Esempio n. 13
0
    def file_open(self):
        try:
            self.file = QFileDialog.getOpenFileName(self,"Open File", os.getcwd(), 'WAV(*.wav)')
            self.wave = wave.openfp(self.file[0], 'rb')
            self.show_info()

            with self.wave:
                self.plotMenu.setEnabled(True)
                self.statusBar().showMessage("Wczytano plik!")
                self.show_info
                self.sound = QSound(self.file[0])
                self.show_player()

        except FileNotFoundError:
            print('Nie udało się otworzyć pliku!')
Esempio n. 14
0
def read_from_byte_string(byte_string, dtype=np.dtype('<i2')):
    """ Parses a bytes string, i.e. a raw read of a wav file

    :param byte_string: input bytes string
    :param dtype: dtype used to decode the audio data
    :return: np.ndarray with audio data with channels x samples
    """
    wav_file = wave.openfp(BytesIO(byte_string))
    channels = wav_file.getnchannels()
    interleaved_audio_data = np.frombuffer(wav_file.readframes(
        wav_file.getnframes()),
                                           dtype=dtype)
    audio_data = np.array(
        [interleaved_audio_data[ch::channels] for ch in range(channels)])
    audio_data = audio_data.astype(np.float32) / np.max(audio_data)
    return audio_data
Esempio n. 15
0
def split_wav(mode, target_dir):
    import io
    import wave

    data_dir = Path(ASPIRE_ROOT, "data", mode).resolve()
    segments_file = Path(data_dir, "segments")
    logger.info(f"processing {str(segments_file)} file ...")
    segments = dict()
    with smart_open(segments_file, "r") as f:
        for line in tqdm(f, total=get_num_lines(segments_file)):
            split = line.strip().split()
            uttid, wavid, start, end = split[0], split[1], float(split[2]), float(split[3])
            if wavid in segments:
                segments[wavid].append((uttid, start, end))
            else:
                segments[wavid] = [(uttid, start, end)]

    wav_scp = Path(data_dir, "wav.scp")
    logger.info(f"processing {str(wav_scp)} file ...")
    manifest = dict()
    with smart_open(wav_scp, "r") as f:
        for line in tqdm(f, total=get_num_lines(wav_scp)):
            wavid, cmd = line.strip().split(" ", 1)
            if not wavid in segments:
                continue
            cmd = cmd.strip().rstrip(' |').split()
            p = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
            fp = io.BytesIO(p.stdout)
            with wave.openfp(fp, "rb") as wav:
                fr = wav.getframerate()
                nf = wav.getnframes()
                for uttid, start, end in segments[wavid]:
                    fs, fe = int(fr * start - SAMPLE_MARGIN), int(fr * end + SAMPLE_MARGIN)
                    if fs < 0 or fe > nf:
                        continue
                    wav.rewind()
                    wav.setpos(fs)
                    signal = wav.readframes(fe - fs)
                    tar_dir = Path(target_dir) / uttid[6:9]
                    Path(tar_dir).mkdir(mode=0o755, parents=True, exist_ok=True)
                    wav_file = str(Path(tar_dir, uttid + ".wav"))
                    with wave.open(wav_file, "wb") as split_wav:
                        split_wav.setparams(wav.getparams())
                        split_wav.writeframes(signal)
                    manifest[uttid] = (wav_file, fe - fs)
    return manifest
Esempio n. 16
0
    def _Load(self):
        # open file finder dialog UI
        root = Tk()
        root.filename = tkFileDialog.askopenfilename()
        if not root.filename:
            print('Invalid file')
            root.destroy()
            return False

        print(root.filename)
        # read the selected file
        audioFile = wave.openfp(root.filename, 'rb')
        self.ui.fs = audioFile.getframerate()  #get fs
        n = audioFile.getnframes()  # get length
        data = np.frombuffer(audioFile.readframes(n), dtype=np.int16)
        self.ui.Recording = data  # insert audio into our recording
        print("Recording Loaded successfully")
        root.destroy()
        return True
Esempio n. 17
0
def reconstruct_manifest(target_dir):
    import wave

    logger.info("reconstructing manifest files ...")
    with open(Path(target_dir, "train.csv"), "w") as f1:
        with open(Path(target_dir, "dev.csv"), "w") as f2:
            for wav_file in Path(target_dir).glob("**/*.wav"):
                uttid = wav_file.stem
                txt_file = str(wav_file).replace("wav", "txt")
                phn_file = str(wav_file).replace("wav", "phn")
                if not Path(txt_file).exists() or not Path(phn_file).exists():
                    continue
                with wave.openfp(str(wav_file), "rb") as wav:
                    samples = wav.getnframes()
                num_frms = get_num_lines(phn_file)
                if 0 < int(uttid[6:11]) < 60:
                    f2.write(f"{uttid},{wav_file},{samples},{phn_file},{num_frms},{txt_file}\n")
                else:
                    f1.write(f"{uttid},{wav_file},{samples},{phn_file},{num_frms},{txt_file}\n")
    logger.info("data preparation finished.")
Esempio n. 18
0
# -*- coding: utf-8 -*-
"""
Created on Sun Oct  9 11:35:30 2016

@author: evert
"""

#from pylab import *
from scipy.io import wavfile
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter
import numpy as np

import struct
import wave

file = wave.openfp('evert.wav', 'wb')

#Wave_write.setparams(tuple)
#The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for the set*() methods. Sets all parameters.

file.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))

for i in range(0, 1000):
    value = np.mod(i, 200) - 100
    packed_value = struct.pack('h', value)
    file.writeframes(packed_value)
    #file.writeframes(packed_value)

file.close()
Esempio n. 19
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wave, Image

f = [wave.openfp('waves/lake%d.wav'%i,'rb') for i in range(1,26)]
# tous les .wav font 10800 frames en mono
nbframes = f[0].getnframes()

w = 60
h = nbframes/3/w
dst = Image.new('RGB',(5*w,5*h))
pix = dst.load()

for i in range(5):
    for j in range(5):
        n = 5*j+i
        dx = w*i
        dy = h*j
        for y in range(h):
            for x in range(w):
                pix[x+dx,y+dy] = (ord(f[n].readframes(1)),ord(f[n].readframes(1)),ord(f[n].readframes(1)))
        
for w in f:
    w.close()

dst.save('dst.png')
# too long to display (unless you display only some of the elements ...)
#If a plot shows up solid color, do (t[:100], (variable)[:100]) to see values better

## DISABLED PLOTTING TO SPEED UP PROGRAM ##########
plt.plot(t, adjustedADSR_Signal)
plt.show()

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
adsr_scaled = np.int16(adjustedADSR_Signal * maxInt)

#######################################################
############ WRITING THE WAV FILES ###################
#######################################################
######## ADSR ENVELOPE ########
adsr = wave.openfp('adjustedADSR.wav', 'wb')
adsr.setnframes(samplesPerSec)
adsr.setsampwidth(2)  #amount of bytes, 8bits/byte
adsr.setnchannels(1)  #mono-1 stereo-2
adsr.setframerate(samplesPerSec)
adsr_data = adsr_scaled
adsr.writeframesraw(adsr_data)
adsr.close()

################################################
############# PLaying Audio in real time #######
################################################
chunk = 1024
adsr_play = wave.open('adjustedADSR.wav', 'rb')
p = pyaudio.PyAudio()
Esempio n. 21
0
maxInt=32767.0 #Max number of 16 bits

t = np.float32(np.arange(0, numOfSecs*samplesPerSec, dtype=float))/samplesPerSec
squareSig=(np.greater(np.sin(2*pi*t*freq),0,))*1

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
scaledSquare=np.int16(squareSig*maxInt)

#######################################################
########## WRITING THE WAV FILES ######################
#######################################################

###### Square wave ################
##Be sure to time numOfSecs=10 to hear it
square=wave.openfp('sampleSquare.wav','wb')
square.setnframes(samplesPerSec)
square.setsampwidth(2)
square.setnchannels(1) #mono
square.setframerate(samplesPerSec)
s = scaledSquare
square.writeframesraw(s)
square.close()

################################################
############# PLaying Audio in real time #######
################################################
chunk=1024
square_play=wave.open('sampleSquare.wav','rb')
p=pyaudio.PyAudio()
upAmp = [
    t / (numOfSecs * samplesPerSec)
    for t in range(int((numOfSecs * samplesPerSec)))
]  #getting equal intervals of 1/Hz
myUpSig = upAmp * np.sin(2 * pi * freq * t)  #for linearity

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
scaledUp = np.int16(myUpSig * maxInt)

#######################################################
########## WRITING THE WAV FILES ######################
#######################################################

###### Ramp of sine function (Amp. Increasing.) ##########
fw2 = wave.openfp('sampleSinRamp.wav', 'wb')
fw2.setnframes(samplesPerSec)
fw2.setsampwidth(2)
fw2.setnchannels(1)  #mono
fw2.setframerate(samplesPerSec)
data2 = scaledUp
fw2.writeframesraw(data2)
fw2.close()

################################################
############# PLaying Audio in real time #######
################################################
chunk = 1024
ramp_play = wave.open('sampleSinRamp.wav', 'rb')
p = pyaudio.PyAudio()
Esempio n. 23
0
"""

#from pylab import *
from scipy.io import wavfile
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter
import numpy as np

import struct
import wave

Fs, snd = wavfile.read('MorseCode/160105_40WPM.wav')

snd = snd[:1e6]

file = wave.openfp('MorseCode/160105_40WPM_C.wav', 'wb')

#Wave_write.setparams(tuple)
#The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for the set*() methods. Sets all parameters.

plt.plot(snd)
plt.show()

file.setparams((1, 2, Fs, 0, 'NONE', 'not compressed'))

for i in range(0, snd.size):
    value = snd[i]
    packed_value = struct.pack('h', value)
    file.writeframes(packed_value)
    #file.writeframes(packed_value)
Esempio n. 24
0
    def reg(self, filename):

        #获取token
        requestData = {
            "grant_type": self.Grant_type,
            "client_id": self.Api_Key,
            "client_secret": self.Secrect_Key
        }

        result = requests.post(url=self.Token_url, data=requestData)

        token_data = json.loads(result.text)

        #self.Print_Response(token_data)

        if 'access_token' in token_data:
            token = token_data['access_token']  #获取的token
            rospy.loginfo('token获取成功\n')
        else:
            rospy.loginfo('token获取失败\n')

        #提交数据
        file_path = self.current_path()
        WAVE_FILE = '%s/%s.%s' % (file_path, filename, self.FORMAT)
        rospy.loginfo('uploading file : %s \n' % WAVE_FILE)

        f = wave.openfp(WAVE_FILE, "rb")
        params = f.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        str_data = f.readframes(
            nframes
        )  #Reads and returns at most n frames of audio, as a string of bytes

        #print str_data,type(str_data)
        f.close()

        speech = base64.b64encode(str_data)

        size = len(str_data)

        #print 'size',size

        RegData = {
            "format": self.FORMAT,
            "rate": framerate,
            "channel": nchannels,
            "cuid": self.USER_ID,
            "token": token,
            "len": size,
            "speech": speech,
            "lan": self.LAN
        }

        HTTP_HEADER = {
            'Content-Type': 'audio/%s;rate=%s' % (self.FORMAT, framerate),
            'Content-length': len(json.dumps(RegData))
        }

        r = requests.post(url=self.Reg_url,
                          data=json.dumps(RegData, sort_keys=True),
                          headers=HTTP_HEADER)

        #print json.dumps(RegData, sort_keys=True),type(json.dumps(RegData, sort_keys=True))

        rospy.loginfo('response header')
        self.Print_Response(r.headers)
        rospy.loginfo('response header\n')

        #处理JSON

        result = json.loads(r.text)
        #self.Print_Response(result)
        rospy.loginfo('result: %s \n' % result['err_msg'])  #,type(result)

        if result[u'err_msg'] == 'success.':
            word = result['result'][0].encode('utf-8')
            if word != '':
                if word[len(word) - 3:len(word)] == ',':
                    rospy.loginfo('识别结果: %s \n' % word[0:len(word) - 3])
                    return word[0:len(word) - 3]
                else:
                    rospy.loginfo(word)
                    return word
            else:
                rospy.loginfo("音频文件不存在或格式错误\n")
                return '音频文件不存在或格式错误'
        else:
            rospy.loginfo(self.error_reason[result[u'err_no']])
            return self.error_reason[result[u'err_no']]
Esempio n. 25
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2, base64, wave, array

url = 'http://www.pythonchallenge.com/pc/hex/bin.html'

auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='pluses and minuses',uri=url,user='******',passwd='fly')
opener = urllib2.build_opener(auth_handler)
f = open('indian.wav','w')
f.write(base64.standard_b64decode(''.join(opener.open(url).readlines()[27:-4])))
f.close()

print 'inverted India => inverted endian'

src = wave.openfp('indian.wav','rb')
dst = wave.openfp('naidni.wav','wb')
dst.setparams(src.getparams())
## Cette ligne renvoie le bon fichier mais avec le son lu à l'envers !
#dst.writeframes(src.readframes(src.getnframes())[::-1])
## La bonne solution est de renverser chaque octet :
a = array.array('i')
a.fromstring(src.readframes(src.getnframes()))
a.byteswap()
dst.writeframes(a.tostring())
src.close()
dst.close()
#print("Maximum value in signal is " + np.max(mySignal).astype('str'))
#print("Minimum value in signal is " + np.min(mySignal).astype('str'))

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
scaled = np.int16(mySignal * maxInt)
scaledOdd = np.int16(myOddSig * maxInt)
scaledUp = np.int16(myUpSig * maxInt)
scaledSquare = np.int16(squareSig * maxInt)

#######################################################
########## WRITING THE WAV FILES ######################
#######################################################

######## Basic sine wave ########
fw = wave.openfp('sampleSin.wav', 'wb')
fw.setnframes(samplesPerSec)
fw.setsampwidth(2)  #amount of bytes, 8bits/byte
fw.setnchannels(1)  #mono-1 stereo-2
fw.setframerate(samplesPerSec)
data = scaled
fw.writeframesraw(data)
fw.close()

###### Ramp of sine function (Amp. Increasing.) ##########
fw2 = wave.openfp('sampleSinRamp.wav', 'wb')
fw2.setnframes(samplesPerSec)
fw2.setsampwidth(2)
fw2.setnchannels(1)  #mono
fw2.setframerate(samplesPerSec)
data2 = scaledUp
Esempio n. 27
0
# too long to display (unless you display only some of the elements ...)
#If a plot shows up solid color, do (t[:100], (variable)[:100]) to see values better

## DISABLED PLOTTING TO SPEED UP PROGRAM ##########
plt.plot(t, myADSR_Signal)
plt.show()

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
adsr_scaled = np.int16(myADSR_Signal * maxInt)

#######################################################
############ WRITING THE WAV FILES ###################
#######################################################
######## ADSR ENVELOPE ########
adsr = wave.openfp('sampleADSR.wav', 'wb')
adsr.setnframes(samplesPerSec)
adsr.setsampwidth(2)  #amount of bytes, 8bits/byte
adsr.setnchannels(1)  #mono-1 stereo-2
adsr.setframerate(samplesPerSec)
adsr_data = adsr_scaled
adsr.writeframesraw(adsr_data)
adsr.close()

################################################
############# PLaying Audio in real time #######
################################################
chunk = 1024
adsr_play = wave.open('sampleADSR.wav', 'rb')
p = pyaudio.PyAudio()
total_cm = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])

fld = 0  #number of fold
for train_fnms, test_fnms in KFold(
        5, True).split(filenames):  #loop to perform 5-fold cross validation
    fld += 1
    print('Number of fold: ', fld)
    train_files = [filenames[i] for i in train_fnms]
    test_files = [filenames[i] for i in test_fnms]

    for filename in train_files:
        ind = ind + 1
        #print(ind)
        w = scipy.io.wavfile.read(filename)
        w_ = wave.openfp(filename)
        maxval = 2**((w_.getsampwidth() * (8) - 1))
        fs = w[0]
        audioData = w[1]
        audioData = audioData / maxval
        audioData = (audioData + 1) / 2
        head_tail = os.path.split(filename)
        A = annotation[annotation[:, 0] == head_tail[1], :]
        for row in A:
            mLabel = row[1]
            startA = int(
                row[2])  #sample from which the respiratory phase begins
            stopA = int(row[3])  #sample at which the respiratory phase stops
            for i in range(
                    startA, (stopA - 4000), 500
            ):  #loop to slide a window of size 4000 with step of 500 samples. There is an overlapping of 3500 samples.
t = np.float32(np.arange(0, numOfSecs * samplesPerSec,
                         dtype=float)) / samplesPerSec

mySignal = np.sin(2 * pi * t * freq)

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
scaled = np.int16(mySignal * maxInt)

#######################################################
########## WRITING THE WAV FILES ######################
#######################################################

######## Basic sine wave ########
fw = wave.openfp('sampleSin.wav', 'wb')
fw.setnframes(samplesPerSec)
fw.setsampwidth(2)  #amount of bytes, 8bits/byte
fw.setnchannels(1)  #mono-1 stereo-2
fw.setframerate(samplesPerSec)
data = scaled
fw.writeframesraw(data)
fw.close()

################################################
############# PLaying Audio in real time #######
################################################
chunk = 1024
sin_play = wave.open('sampleSin.wav', 'rb')
p = pyaudio.PyAudio()
Esempio n. 30
0
maxInt=32767.0 #Max number of 16 bits

t = np.float32(np.arange(0, numOfSecs*samplesPerSec, dtype=float))/samplesPerSec
myOddSig= np.sin(2*pi*t*freq)+np.sin(2*pi*t*freq*3)/3 + np.sin(2*pi*t*freq*5)/5 + np.sin(2*pi*t*freq*7)/7 + np.sin(2*pi*t*freq*9)/9;

########## SCALING TO WRITE WAV FILES #######################
# range -32768 to 32767 and cast to 16-bit integers as so:
scaledOdd=np.int16(myOddSig*maxInt)

#######################################################
########## WRITING THE WAV FILES ######################
#######################################################

###### Odd (1,3,5,7,9) wave ################
##Be sure to time numOfSecs=10 to hear it
odd=wave.openfp('sampleOdd.wav','wb')
odd.setnframes(samplesPerSec)
odd.setsampwidth(2)
odd.setnchannels(1) #mono
odd.setframerate(samplesPerSec)
dataOdd = scaledOdd
odd.writeframesraw(dataOdd)
odd.close()


################################################
############# PLaying Audio in real time #######
################################################
chunk=1024
odd_play=wave.open('sampleOdd.wav','rb')
p=pyaudio.PyAudio()
Esempio n. 31
0
 def reg(self,filename):
 
  #获取token
  requestData = {       "grant_type":           self.Grant_type, 
                        "client_id":            self.Api_Key, 
                        "client_secret":        self.Secrect_Key}
  
  result = requests.post(url = self.Token_url, data = requestData)
  
  token_data = json.loads(result.text)
  
  #self.Print_Response(token_data)
  
  if 'access_token' in token_data:  
   token = token_data['access_token']    #获取的token
   rospy.loginfo('token获取成功\n')
  else:
   rospy.loginfo('token获取失败\n')
   
  #提交数据
  file_path=self.current_path()
  WAVE_FILE = '%s/%s.%s'%(file_path,filename,self.FORMAT)
  rospy.loginfo('uploading file : %s \n'%WAVE_FILE)

  
  f = wave.openfp(WAVE_FILE,"rb")
  params = f.getparams()
  nchannels, sampwidth, framerate, nframes = params[:4]
  str_data = f.readframes(nframes) #Reads and returns at most n frames of audio, as a string of bytes
  
  #print str_data,type(str_data)
  f.close()
  
  speech = base64.b64encode(str_data)

  size = len(str_data)

  #print 'size',size
  
  RegData = {   "format":       self.FORMAT,
                "rate":         framerate,
                "channel":      nchannels,
                "cuid":         self.USER_ID,
                "token":        token,
                "len":          size,
                "speech":       speech,
                "lan":          self.LAN}
                
  HTTP_HEADER=          {  'Content-Type':      'audio/%s;rate=%s'%(self.FORMAT,framerate),
                           'Content-length':    len(json.dumps(RegData))}

  r = requests.post(url = self.Reg_url, data = json.dumps(RegData, sort_keys=True), headers=HTTP_HEADER)
  
  #print json.dumps(RegData, sort_keys=True),type(json.dumps(RegData, sort_keys=True))
  
  rospy.loginfo('response header')
  self.Print_Response(r.headers)
  rospy.loginfo('response header\n')
  
  #处理JSON

  result = json.loads(r.text)
  #self.Print_Response(result)
  rospy.loginfo( 'result: %s \n'%result['err_msg'])#,type(result)

  
  if result[u'err_msg']=='success.':
   word = result['result'][0].encode('utf-8')
   if word!='':
    if word[len(word)-3:len(word)]==',':
     rospy.loginfo('识别结果: %s \n'%word[0:len(word)-3])
     return word[0:len(word)-3]
    else:
     rospy.loginfo(word)
     return word
   else:
    rospy.loginfo("音频文件不存在或格式错误\n")
    return '音频文件不存在或格式错误'
  else:
   rospy.loginfo(self.error_reason[result[u'err_no']])
   return  self.error_reason[result[u'err_no']]
Esempio n. 32
0
#mySignal = np.sin(2.0*np.pi*t*(fc + d*np.sin(2.0*np.pi*t*fm)))    # This is tempting, but it is wrong ...
#mySignal = np.sin(2.0*np.pi*fc*t - d/fm*np.cos(2.0*np.pi*t*fm))   # This is correct
mySignal = np.sin(
    2.0 * np.pi * randoms[0] * t +
    (randoms[1] / randoms[2]) * np.sin(2.0 * np.pi * t * randoms[2])
)  # This sounds almost the same and is a little more convenient

# To actually write it to a wave file, the sound has to be scaled to the
# range -32768 to 32767 and cast to 16-bit integers as so:
scaled = np.int16(mySignal * 32767.0)

#######################################################
############ WRITING THE WAV FILES ###################
#######################################################
######## Fast Sine WAV File ########
arraySine = wave.openfp('arrayInputFM.wav', 'wb')
arraySine.setnframes(fs)
arraySine.setsampwidth(2)  #amount of bytes, 8bits/byte
arraySine.setnchannels(1)  #mono-1 stereo-2
arraySine.setframerate(fs)
arraySine.writeframesraw(scaled)
arraySine.close()

################################################
############# PLaying Audio in real time #######
################################################
chunk = 1024
arraySin_play = wave.open('arrayInputFM.wav', 'rb')
p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(arraySin_play.getsampwidth()),
Esempio n. 33
0
import pyaudio
import numpy as np
import scipy.signal
import math
import numbers
import cmath
import wave

#commnds to make a line breakpoint
#print ("this is a breakin line point",)
#breakin = int(input())
wf = wave.openfp("/home/tony/Downloads/jimi2.wav", "rb")
print ("Please give the gain:",)
gain = np.double(input())
print ("Please give the mix:",)
mix = np.double(input())
#defining audio parameters-------------------------------------
CHUNK = 1024
FORMAT = pyaudio.paInt16
WIDTH = 2
DTYPE = np.int16
MAX_INT = 32768.0
CHANNELS = 2
RATE = 44100
#RATE = 22050
RECORD_SECONDS = 20
#audio streamming------------------------------------------------------------------------
p = pyaudio.PyAudio()
print("* recording")

number_loops=int(RATE / CHUNK * RECORD_SECONDS)
def ReadAudioFile(path):
    audioFile = wave.openfp(path, 'rb')
    fs = audioFile.getframerate()  #get fs
    n = audioFile.getnframes()  # get length
    data = np.frombuffer(audioFile.readframes(n), dtype=np.int16)
    return fs, data