예제 #1
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
예제 #3
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()
예제 #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
예제 #5
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
예제 #6
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
예제 #7
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()
 def run(self):
     audio = PyAudio()
     wavfile = wave.open(self.audiofile, 'ab')
     wavfile.setnchannels(self.channels)
     wavfile.setsampwidth(audio.get_sample_size(self.format))
     wavfile.setframerate(self.rate)
     wavstream = audio.open(format=self.format,
                            channels=self.channels,
                            rate=self.rate,
                            input=True,
                            frames_per_buffer=self.chunk)
     while self.bRecord:
         wavfile.writeframes(wavstream.read(self.chunk))
     wavstream.stop_stream()
     wavstream.close()
     audio.terminate()
    def run(self):
        audio = PyAudio()
        print("Sound device:", self.dev_idx)
        device_info = audio.get_device_info_by_index(self.dev_idx)
        self.channels = device_info["maxInputChannels"] if (
            device_info["maxOutputChannels"] < device_info["maxInputChannels"]
        ) else device_info["maxOutputChannels"]
        self.rate = int(device_info["defaultSampleRate"])
        print(color.yellow(str(device_info)))
        wavstream = audio.open(format=self.format,
                               channels=self.channels,
                               rate=self.rate,
                               input=True,
                               frames_per_buffer=self.chunk,
                               input_device_index=device_info["index"],
                               as_loopback=True)

        # wavstream = audio.open(format=self.format,
        #                        channels=self.channels,
        #                        rate=self.rate,
        #                        input=True,
        #                        frames_per_buffer=self.chunk)
        # 如果没有外放的话,loopback会没有数据,造成阻塞
        # 循环读取输入流
        while self.bRecord:
            data = wavstream.read(self.chunk)
            self._frames.append(data)

        self._status = 1
        wavstream.stop_stream()
        wavstream.close()
        # 保存到文件
        print("Saveing .... ", self.audiofile)
        with wave.open(self.audiofile, 'wb') as wavfile:
            wavfile.setnchannels(self.channels)
            wavfile.setsampwidth(audio.get_sample_size(self.format))
            wavfile.setframerate(self.rate)
            wavfile.writeframes(b''.join(self._frames))
        audio.terminate()
        self._status = 2
예제 #10
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
예제 #12
0
파일: Recieve.py 프로젝트: dfu3/CryptoBeep
hexString = ""
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)