Esempio n. 1
0
 def savewav(self, filename):
     wf = wave.open(filename, 'wb')
     wf.setnchannels(1)
     wf.setsampwidth(2)
     wf.setframerate(self.SAMPLING_RATE)
     wf.writeframes(np.array(self.Voice_String).tostring())
     wf.close()
Esempio n. 2
0
def write_wav(data, filename, framerate, amplitude, length):
    wavfile = wave.open(filename,'w')
    nchannels = 1
    sampwidth = 2
    framerate = framerate
    nframes = length
    comptype = "NONE"
    compname = "not compressed"
    wavfile.setparams((nchannels,
                        sampwidth,
                        framerate,
                        nframes,
                        comptype,
                        compname))
    frames = []
    for s in data:
        mul = int(s * amplitude)
        print("I am here")
        print(mul)
        frames.append(struct.pack('h', mul))

    frames = ''.join(frames)
    wavfile.writeframes(frames)
    wavfile.close()
    print("%s written" %(filename)) 
def save_file(filename):

    wavfile = wave.open(filename, "w")

    # wav parameters

    # Number of channels
    nchannels = 1
    # Sample width in bytes
    sampwidth = 2
    # Number of frames or samples
    nframes = len(data)

    comptype = "NONE"
    compname = "not compressed"

    wavfile.setparams(
        (nchannels, sampwidth, samples, nframes, comptype, compname))

    for sample in data:
        """
        struct - Takes the data from my "data" list and packs it as binary data or else
        the file is not readable by an audio player.

        h - Means 16 bit number

        32767.0 - The floating point numbers are not represented right and will not work
        when writing to the wave file. Convert the floating point numbers to fixed point
        to get full scale audio.
        """
        wavfile.writeframes(struct.pack('h', int(sample * 32767.0)))

    wavfile.close()

    return
Esempio n. 4
0
    def load(self, originalname):
        # Load + convert audio
        print("Loading sound file <{}>...".format(originalname))
        name = originalname.split(".")[0]
        ext = originalname.split(".")[-1].lower()

        audiofile = originalname
        if ext == MP3:
            newfilename = name + ".wav"
            old = AudioSegment.from_mp3(audiofile)
            old.export(newfilename, format=WAV)
            self.filename = newfilename
        elif ext == PCM:
            print "This is a PCM file!"
            pcmfile = open(audiofile, 'rb')
            pcmdata = pcmfile.read()
            pcmfile.close()

            wavfile = wave.open(name + '.wav', 'wb')
            wavfile.setparams((1, 2, 44100, 16, 'NONE', 'NONE'))
            wavfile.writeframes(pcmdata)
            wavfile.close()
            self.filename = name + ".wav"
        else:
            self.filename = audiofile
Esempio n. 5
0
def get_wav_info(wav_file):
    wav = wave.open(wav_file, 'r')
    frames = wav.readframes(-1)
    sound_info = pylab.frombuffer(frames, 'int16')
    frame_rate = wav.getframerate()
    wav.close()
    return sound_info, frame_rate
Esempio n. 6
0
def duration(wav_file):
    wav = wave.open(wav_file, 'r')
    frames = wav.getnframes()
    rate = wav.getframerate()
    dur = frames / float(rate)
    #print 'duration: %f' % duration
    wav.close()
    return dur
Esempio n. 7
0
def write_text_grid_from_hash(grid_name, seg, sample_rate, len_signal, levels):
    try:
        w = None
        w = open(grid_name, "w", encoding="utf8")
        if w is None:
            return
        #chapka
        w.write(
            "File type = \"ooTextFile\"\nObject class = \"TextGrid\"\n\nxmin = 0.0\n"
        )

        xmax = len_signal / sample_rate
        w.write("xmax = {0}\ntiers? <exists>\nsize = {1}\nitem []:\n".format(
            float(xmax), len(seg)))

        size_total = 0
        ind_level = 1
        for level_name in levels:
            try:
                cur_intervals = seg[level_name]
                w.write("\titem [{0}]\n".format(ind_level))
                w.write("\t\tclass = \"IntervalTier\"\n")
                w.write("\t\tname = \"{0}\"\n".format(level_name))
                w.write("\t\txmin = {0}\n".format(
                    float(cur_intervals[0]["frm"] / sample_rate)))
                w.write("\t\txmax = {0}\n".format(float(xmax)))
                if len(cur_intervals):
                    w.write("\t\tintervals: size = {0}\n".format(
                        len(cur_intervals)))
                    j_ind = 1
                    for item in cur_intervals:
                        w.write("\t\tintervals [{0}]:\n".format(j_ind))
                        w.write("\t\t\txmin = {0}\n".format(
                            float(item["frm"] / sample_rate)))
                        w.write("\t\t\txmax = {0}\n".format(
                            float(item["to"] / sample_rate)))
                        w.write("\t\t\ttext = \"{0}\"\n".format(item["nm"]))
                        j_ind += 1
                else:
                    w.write("\t\tintervals: size = 1\n")
                    w.write("\t\tintervals [1]:\n")
                    w.write("\t\t\txmin = 0\n")
                    w.write("\t\t\txmax = {0}\n".format(
                        float(item["to"] / sample_rate)))
                    w.write("\t\t\ttext = \"\"\n")
                a = 1
            except Exception as err:
                print(err)

    except Exception as err:
        print(err)
    finally:
        if w is not None:
            w.close()
Esempio n. 8
0
def capture_audio(callback, final_callback):
    import pyaudio
    import wave as wv
    from array import array

    FORMAT = pyaudio.paInt16
    RATE = 16000
    CHUNK = 1024
    COUNTCHUNK = (RATE + CHUNK - 1) / CHUNK
    RECORD_SECONDS = 1
    FILE_NAME = "/tmp/mic_rec.wav"

    audio = pyaudio.PyAudio()

    stream = audio.open(format=FORMAT,
                        rate=RATE,
                        channels=1,
                        input=True,
                        frames_per_buffer=CHUNK)

    #starting recording
    wave = []
    callback_free = [True]
    print("started")
    last = time.time()
    lastloudsoundtime = time.time() - 100000

    while True:
        data = stream.read(CHUNK)
        data_chunk = array('h', data)
        vol = max(data_chunk)
        # print("Volume {}".format(vol))
        wave.append(data)
        if len(wave) > COUNTCHUNK:
            wave.pop(0)
        if (vol >= 2500):
            lastloudsoundtime = time.time()
        if (time.time() - last > 0.05
                and time.time() - lastloudsoundtime < 0.8):
            last = time.time()
            if len(wave) == COUNTCHUNK:
                #writing to file
                wavfile = wv.open(FILE_NAME, 'wb')
                wavfile.setnchannels(1)
                wavfile.setsampwidth(audio.get_sample_size(FORMAT))
                wavfile.setframerate(RATE)
                wavfile.writeframes(b''.join(wave))
                wavfile.close()
                if callback_free[0]:
                    callback_free[0] = False
                    callback(callback_free, FILE_NAME, final_callback)
                    # print("Something is said")
        else:
            pass
Esempio n. 9
0
def read_wav_data(filename):
    wav = wave.open(filename, "rb")  # 打开一个wav格式的声音文件流
    num_frame = wav.getnframes()  # 获取帧数
    num_channel = wav.getnchannels()  # 获取声道数
    framerate = wav.getframerate()  # 获取帧速率
    num_sample_width = wav.getsampwidth()  # 获取实例的比特宽度,即每一帧的字节数
    str_data = wav.readframes(num_frame)  # 读取全部的帧
    wav.close()  # 关闭流
    wave_data = np.fromstring(str_data, dtype=np.short)  # 将声音文件数据转换为数组矩阵形式
    wave_data.shape = -1, num_channel  # 按照声道数将数组整形,单声道时候是一列数组,双声道时候是两列的矩阵
    wave_data = wave_data.T  # 将矩阵转置
    return wave_data, framerate
Esempio n. 10
0
def save_wave_file(filename, data):
    # define of params
    framerate = 8000
    channels = 1
    sampwidth = 2
    '''save the date to the wav file'''
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes("".join(data))
    wf.close()
    return 0
Esempio n. 11
0
    def stop(self, path='output.wav'):
        sd.stop()
        duration = int(math.ceil(time.time() - self._start_time))
        data = self._recording[:duration * sd.default.samplerate]
        # save audio
        audio = pyaudio.PyAudio()
        format = pyaudio.paInt16

        wavfile = wave.open(path, 'wb')
        wavfile.setnchannels(CHANNELS)
        wavfile.setsampwidth(audio.get_sample_size(format))
        wavfile.setframerate(SAMPLERATE)
        wavfile.writeframes(data.tostring())
        wavfile.close()
Esempio n. 12
0
def write_wav(data, filename, framerate, amplitude):
    wavfile = wave.open(filename,'w')
    nchannels = 1
    sampwidth = 2
    framerate = framerate
    nframes = len(data)
    comptype = "NONE"
    compname = "not compressed"
    wavfile.setparams((nchannels,
                        sampwidth,
                        framerate,
                        nframes,
                        comptype,
                        compname))
    frames = []
    for s in data:
        mul = int(s * amplitude)
        frames.append(struct.pack('h', mul))

    frames = ''.join(frames)
    wavfile.writeframes(frames)
    wavfile.close()
    print("%s written" %(filename)) 
frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

stream.stop_stream()
stream.close()
audio.terminate()

wavfile = wave.open(FILE_NAME, 'w')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(frames))
wavfile.close()

wavfile = wave.open(FILE_NAME, 'r')
samplingFrequency, signalData = scipy.io.wavfile.read('ses.wav')
plot.subplot(211)
plot.plot(signalData)

plot.subplot(212)
plot.specgram(signalData, Fs=samplingFrequency)
plot.show()

#3.Kısım - Real time mikrofon ses verileriyle sinyal işleme
import pyaudio
import struct
import numpy as np
import matplotlib.pyplot as plt
Esempio n. 14
0
hexString += (hex(binVal)[2:-1])

decryptMess = binascii.unhexlify(hexString)

print("original text: " +decryptMess)

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
#-------------------------- ----------------------------
#plotting sound below
#------------------------------------------------------

spf = wave.open('input.wav','r')

signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16')
fs = spf.getframerate()

#If Stereo
if spf.getnchannels() == 2:
    print 'Just mono files'
    sys.exit(0)