예제 #1
0
def testgetsample(data):
	for i in range(3):
		if audioop.getsample(data[0], 1, i) <> i or \
			  audioop.getsample(data[1], 2, i) <> i or \
			  audioop.getsample(data[2], 4, i) <> i:
			return 0
	return 1
예제 #2
0
def recv():
    global inp, etc, trig_this_time, trig_last_time, sin
    # get audio
    #l,data = inp.read()
    peak = 0
    while 1:

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

            try:
                avg = audioop.getsample(data, 2, i * 3)
                avg += audioop.getsample(data, 2, (i * 3) + 1)
                avg += audioop.getsample(data, 2, (i * 3) + 2)
                avg = avg / 3
                if (avg > 20000):
                    trig_this_time = time.time()
                    if (trig_this_time - trig_last_time) > .05:
                        etc.audio_trig = True
                        trig_last_time = trig_this_time

                if avg > peak:
                    etc.audio_peak = avg
                    peak = avg
                # if the trigger button is held
                if (etc.trig_button):
                    etc.audio_in[i] = sin[i]
                else:
                    etc.audio_in[i] = avg

            except:
                pass
예제 #3
0
def read_wav_file(name):
    """
    This function reads a WAV file named 'name' and returns
    an array containing integral samples.
    """

    # Open wave file for reading
    w = wave.open(name, 'rb')
    nchannels = w.getnchannels()

    # Read the wave as a raw string. Caution, this could use a
    # lot of memory!
    raw = w.readframes(w.getnframes())

    # Convert to a list of samples
    # Mono
    if nchannels == 1:
        data = empty(w.getnframes(), dtype=numpy.int16)
        for i in xrange(0, w.getnframes()):
            data[i] = audioop.getsample(raw,w.getsampwidth(),i)
    elif nchannels == 2:
        data = empty(2*w.getnframes(), dtype=numpy.int16)
        for i in xrange(0, w.getnframes()):
            data[i] = audioop.getsample(raw,w.getsampwidth(),i)

        data = array([data[0::2],data[1::2]], dtype=numpy.int16)
    return data
예제 #4
0
파일: sound.py 프로젝트: mansj/ETC_Mother
def recv() :
    global inp, etc, trig_this_time, trig_last_time, sin
    # get audio
    l,data = inp.read()
    peak = 0
    while l:
        for i in range(0,100) :
            try :
                avg = audioop.getsample(data, 2, i * 3)
                avg += audioop.getsample(data, 2, (i * 3) + 1)
                avg += audioop.getsample(data, 2, (i * 3) + 2)
                avg = avg / 3
                if (avg > 20000) :
                    trig_this_time = time.time()
                    if (trig_this_time - trig_last_time) > .05:
                        etc.audio_trig = True
                        trig_last_time = trig_this_time
                if avg > peak :
                    etc.audio_peak = avg
                    peak = avg
                # if the trigger button is held
                if (etc.trig_button) :
                    etc.audio_in[i] = sin[i] 
                else :
                    etc.audio_in[i] = avg

            except :
                pass
        l,data = inp.read()
예제 #5
0
 def test_getsample(self):
     for w in 1, 2, 4:
         data = packs[w](0, 1, -1, maxvalues[w], minvalues[w])
         self.assertEqual(audioop.getsample(data, w, 0), 0)
         self.assertEqual(audioop.getsample(data, w, 1), 1)
         self.assertEqual(audioop.getsample(data, w, 2), -1)
         self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w])
         self.assertEqual(audioop.getsample(data, w, 4), minvalues[w])
예제 #6
0
 def test_getsample(self):
     for w in 1, 2, 4:
         data = packs[w](0, 1, -1, maxvalues[w], minvalues[w])
         self.assertEqual(audioop.getsample(data, w, 0), 0)
         self.assertEqual(audioop.getsample(data, w, 1), 1)
         self.assertEqual(audioop.getsample(data, w, 2), -1)
         self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w])
         self.assertEqual(audioop.getsample(data, w, 4), minvalues[w])
예제 #7
0
def testgetsample(data):
    if verbose:
        print 'getsample'
    for i in range(3):
        if audioop.getsample(data[0], 1, i) != i or \
                  audioop.getsample(data[1], 2, i) != i or \
                  audioop.getsample(data[2], 4, i) != i:
            return 0
    return 1
예제 #8
0
def testgetsample(data):
    if verbose:
        print 'getsample'
    for i in range(3):
        if audioop.getsample(data[0], 1, i) != i or \
                  audioop.getsample(data[1], 2, i) != i or \
                  audioop.getsample(data[2], 4, i) != i:
            return 0
    return 1
예제 #9
0
def recv():
    global client, inp, etc, trig_this_time, trig_last_time, sin
    if aout_norns:
        # get audio (with 16 bit signed format)
        data_l = 32767 * inp[0].get_array()
        data_r = 32767 * inp[1].get_array()
    else:
        # get audio from alsa
        l, data = inp.read()
        if l > 0:
            ldata = audioop.tomono(data, 2, 1, 0)
            rdata = audioop.tomono(data, 2, 0, 1)
    peak = 0
    # try :
    for i in range(0, 100):
        avg_l = 0
        avg_r = 0
        for j in range(3):
            if aout_norns:
                avg_l += data_l[3 * i + j]
                avg_r += data_r[3 * i + j]
            else:
                if l > 0:
                    avg_l += audioop.getsample(ldata, 2, (i * 3) + j)
                    avg_r += audioop.getsample(rdata, 2, (i * 3) + j)
    # except :
    #     pass

        avg_l = avg_l / 3
        avg_r = avg_r / 3

        avg = (avg_l + avg_r) / 2

        # scale it
        avg = int(avg * etc.audio_scale)
        avg_l = int(avg_l * etc.audio_scale)
        avg_r = int(avg_r * etc.audio_scale)

        if (avg > 20000):
            trig_this_time = time.time()
            if (trig_this_time - trig_last_time) > .05:
                if etc.audio_trig_enable: etc.audio_trig = True
                trig_last_time = trig_this_time
        if avg > peak:
            etc.audio_peak = avg
            peak = avg
        # if the trigger button is held
        if (etc.trig_button):
            etc.audio_in[i] = sin[i]
            etc.audio_left[i] = sin[i]
            etc.audio_right[i] = sin[i]
        else:
            etc.audio_in[i] = avg
            etc.audio_left[i] = avg_l
            etc.audio_right[i] = avg_r
예제 #10
0
    def analyze(self):
        l,data = self.inp.read()
        #print l
        if l:
            i = 0
            for x in self.indizes:
                try:
                    self.wave[i] = audioop.getsample(data,2,i)/32767.0
                    self.sample[i] = audioop.getsample(data,2,i)
                except audioop.error:
                    break
                i = i+1

            self.freq[self.indizes] = 0
            self.bands[[0,1,2]] = 0
            fftw3.execute(self.plan)
            ilc = self.nfreq * (self.low_cut_f/ (self.rate*0.5)) - 1
            ihc = self.nfreq * (self.high_cut_f/ (self.rate*0.5)) - 1
            ilc = max(ilc, 1)
            nlow = int(ilc)
            nmid = int(ihc - ilc)
            nhigh = int(self.nfreq - nmid)

            #print nlow, nmid, nhigh

            for i in range(self.nfreq):
                self.spect[i] = math.sqrt(self.freq[i].real*self.freq[i].real + self.freq[i].imag*self.freq[i].imag) * math.log1p(i+2)
                self.spect_flux[i] = self.spect[i] - self.spect_old[i]
                self.spect_old[i] = self.spect[i]
                self.spect_flux[i] = max(self.spect_flux[i], 0.0)
                if((i < ilc) and (i < ihc)):
                    self.bands[0] += self.spect_flux[i]/nlow
                elif((i<ihc) and (i < self.nfreq)):
                    self.bands[1] += self.spect_flux[i]/nmid
                else:
                    self.bands[2] += self.spect_flux[i]/nhigh

            for i in [0,1,2]:
                temp = max(self.bands[i], 0,0)
                old_temp = self.bands_avg[i]
                self.bands[i] = max((temp - old_temp - self.amp_thres), 0,0)
                if(self.nframe > 1):
                    if(self.bands[i] == 0): self.bands_avg[i] = temp*0.15 + self.bands_avg[i]*0.85
                    if(self.bands[i] > self.bands_s[i]):
                        self.bands_s[i] = self.bands[i]
                    else:
                        self.bands_s[i] = self.bands_s[i] * 0.15 + self.bands[i]*0.85
                else:
                    self.bands_avg[i] = temp
                    self.bands_s[i] = self.bands[i]

            self.nframe = self.nframe + 1
예제 #11
0
	def capture(self):
		midTermBufferSize = int(self.midTermBufferSizeSec * self.Fs)
		allData = []
		maxPwmValue = 0
		minPwmValue = 0
		midTermBuffer = []
		curWindow = []

		while len(allData) < self.duration * self.Fs:
			time.sleep(0.01)
			l, data = self.inp.read()
			if l:
				for i in range(l):
					curWindow.append(audioop.getsample(data, 2, i))		
				if (len(curWindow) + len(midTermBuffer) > midTermBufferSize):
					samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
				else:
					samplesToCopyToMidBuffer = len(curWindow)
				midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer];
				del(curWindow[0:samplesToCopyToMidBuffer])

				pwm = audioop.rms(data, 2)
				if pwm < minPwmValue or minPwmValue == 0:
					minPwmValue = pwm
				if pwm > maxPwmValue or maxPwmValue == 0:
					maxPwmValue = pwm

			if len(midTermBuffer) == midTermBufferSize:
				allData = allData + midTermBuffer
				midTermBuffer = []

		return allData, minPwmValue, maxPwmValue
예제 #12
0
def gendata2():
    if verbose:
        print 'getsample'
    if audioop.getsample('\0\1', 2, 0) == 1:
        return '\0\0\0\1\0\2'
    else:
        return '\0\0\1\0\2\0'
예제 #13
0
 def __init__(self, fname, newrate=0):
     """Initializes an audio file from an uncompressed wavefile on disk.
     The file is converted to mono, and if newrate is positive, then the rate is converted"""
     import wave, audioop
     try:  # see if we have numpy
         from numpy import array
         self.numpy = 1
     except ImportError:
         self.numpy = 0
     # read data
     f = wave.open(fname, 'rb')
     nchans, w, fps, nframes, comptype, compname = f.getparams()
     print "Read audio file %s with %d chans, %d width, %d fps and %d frames" % (
         fname, nchans, w, fps, nframes)
     self.width, self.fps = w, fps
     self.dat = f.readframes(nframes)
     print "  Original data length was %d" % (len(self.dat))
     # convert to mono and (optionally) convert the rate
     self.dat = audioop.tomono(self.dat, w, 0.5, 0.5)
     print "  After mono, data length is %d" % (len(self.dat))
     if newrate > 0:
         self.dat, junk = audioop.ratecv(self.dat, w, 1, fps, newrate, None)
         self.fps = newrate
         print "  Converted to new rate %s, and data length is now %d" % (
             self.fps, len(self.dat))
     # now extract the data into a simple array
     from audioop import getsample
     self.dat = [
         abs(getsample(self.dat, w, i)) for i in range(len(self.dat) // w)
     ]
     print "  Final data length is now of length %s" % (len(self.dat), )
     if self.numpy:
         self.dat = array(self.dat)
예제 #14
0
    def __init__(self, fname, newrate=0):
        """Initializes an audio file from an uncompressed wavefile on disk.
        The file is converted to mono, and if newrate is positive, then the rate is converted"""
        import wave, audioop

        try:  # see if we have numpy
            from numpy import array

            self.numpy = 1
        except ImportError:
            self.numpy = 0
        # read data
        f = wave.open(fname, "rb")
        nchans, w, fps, nframes, comptype, compname = f.getparams()
        print "Read audio file %s with %d chans, %d width, %d fps and %d frames" % (fname, nchans, w, fps, nframes)
        self.width, self.fps = w, fps
        self.dat = f.readframes(nframes)
        print "  Original data length was %d" % (len(self.dat))
        # convert to mono and (optionally) convert the rate
        self.dat = audioop.tomono(self.dat, w, 0.5, 0.5)
        print "  After mono, data length is %d" % (len(self.dat))
        if newrate > 0:
            self.dat, junk = audioop.ratecv(self.dat, w, 1, fps, newrate, None)
            self.fps = newrate
            print "  Converted to new rate %s, and data length is now %d" % (self.fps, len(self.dat))
        # now extract the data into a simple array
        from audioop import getsample

        self.dat = [abs(getsample(self.dat, w, i)) for i in range(len(self.dat) // w)]
        print "  Final data length is now of length %s" % (len(self.dat),)
        if self.numpy:
            self.dat = array(self.dat)
예제 #15
0
def gendata2():
    if verbose:
        print 'getsample'
    if audioop.getsample('\0\1', 2, 0) == 1:
        return '\0\0\0\1\0\2'
    else:
        return '\0\0\1\0\2\0'
예제 #16
0
파일: inputs.py 프로젝트: shish/mic2midi
 def read(self):
     buffer_size = self.rate / 10
     data = self.wav.readframes(buffer_size)
     if not data:
         raise EOFError("End of input file")
     samples = [audioop.getsample(data, self.width, n) for n in range(0, len(data)/self.width)]
     time.sleep(float(buffer_size)/self.rate)
     return len(samples), samples
예제 #17
0
def unpack(data):
    """
    This function extracts a raw byte string into an array of
    16-bit integers.
    """
    undata = empty(len(data)/2, dtype=numpy.int16)
    for i in xrange(0, len(data)/SAMPLE_WIDTH):
        undata[i] = audioop.getsample(data,SAMPLE_WIDTH,N_CHANNELS)
예제 #18
0
def unpack(data):
    """
    This function extracts a raw byte string into an array of
    16-bit integers.
    """
    import audioop

    undata = numpy.empty(len(data) / 2, dtype=numpy.int16)
    for i in xrange(0, len(data) / SAMPLE_WIDTH):
        undata[i] = audioop.getsample(data, SAMPLE_WIDTH, i)

    return undata
 def getFrame(self):
     ''' Retrurn a list of self.frameSize audio samples '''
     frame = []
     # Read data from device
     l,data = self.inp.read()
     if l:
         #print "got %d samples" % (l)
         for n in range(l):
             frame.append(audioop.getsample(data,2,n))
     else:
         print "no data returned from getFrame"
     return frame
 def getFrame(self):
     ''' Retrurn a list of self.frameSize audio samples '''
     frame = []
     # Read data from device
     l, data = self.inp.read()
     if l:
         #print "got %d samples" % (l)
         for n in range(l):
             frame.append(audioop.getsample(data, 2, n))
     else:
         print "no data returned from getFrame"
     return frame
예제 #21
0
파일: main.py 프로젝트: johnlinp/jjpedal
def raw_to_list(samples):
    assert PCM_FORMAT == alsaaudio.PCM_FORMAT_S16_LE
    res = []
    width = 2
    num_samples = len(samples) / width
    for idx in range(num_samples):
        sample = samples[idx * width : (idx + 1) * width]
        num = sample_to_int(sample)
        assert num == audioop.getsample(samples, width, idx)
        assert int_to_sample(num) == sample
        res.append(num)
    return res
예제 #22
0
 def test_getsample(self):
     for w in (1, 2, 3, 4):
         data = packs[w](0, 1, -1, maxvalues[w], minvalues[w])
         self.assertEqual(audioop.getsample(data, w, 0), 0)
         self.assertEqual(audioop.getsample(bytearray(data), w, 0), 0)
         self.assertEqual(audioop.getsample(memoryview(data), w, 0), 0)
         self.assertEqual(audioop.getsample(data, w, 1), 1)
         self.assertEqual(audioop.getsample(data, w, 2), -1)
         self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w])
         self.assertEqual(audioop.getsample(data, w, 4), minvalues[w])
예제 #23
0
    def input(self,data):
        recording = data['recording']
        l = len(recording)
        samples = scipy.zeros(l / 2)
        fft = scipy.zeros(l / 2)
        for i in range(0, l / 2):
            samples[i]=audioop.getsample(recording, 2, i)

        fft = scipy.fft(samples)
        fft = fft[:len(fft) / 2]
        fft = abs(fft)
        fft *= .00005
        data['fft'] = fft
        self.output(data)
예제 #24
0
def callback(in_data, frame_count, time_info, status):
    global inp, etc, trig_this_time, trig_last_time, sin
    data = in_data
    peak = 0
    try:
        #print('range', int(RATE / CHUNK * RECORD_SECONDS))
        #print('data',len(data))
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            avg = audioop.getsample(data, 2, i * 3)
            avg += audioop.getsample(data, 2, (i * 3) + 1)
            avg += audioop.getsample(data, 2, (i * 3) + 2)
            avg = avg / 3
            if (avg > 1000):
                trig_this_time = time.time()
                a = trig_this_time  # - trig_last_time
                #print(a)
                #print(avg)
                if (trig_this_time - trig_last_time) > .05:
                    etc.audio_trig = True
                    trig_last_time = trig_this_time

                if avg > peak:
                    etc.audio_peak = avg
                    peak = avg
                # if the trigger button is held
                #print(etc.trig_button)
                if (etc.trig_button):
                    etc.audio_in[i] = sin[i]
                else:
                    #print(etc.audio_in)
                    #print(len(etc.audio_in))
                    #print(i)
                    etc.audio_in[i] = avg
    except Exception as e:
        print('ERROR!!!  ', e)
        pass
    return (data, pyaudio.paContinue)
예제 #25
0
    def get_all_samples(data):
        allsamples=[]
        index=0
        while True:
            try:
                index=index+1
                sample=audioop.getsample(data,2,index)
                sample_8a = sample & 0xff
                sample_8b = (sample >> 8) & 0xff
                allsamples.append(int(str(sample_8a)))
                allsamples.append(int(str(sample_8b)))
            except:
                break;

        return allsamples;
예제 #26
0
    def voiceInput(self,Bs):
        inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
        inp.setchannels(1)
        inp.setrate(self.Fs)
        inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        inp.setperiodsize(512)

        midTermBufferSize = int((self.Fs)*Bs)

        midTermBuffer = []
        curWindow = []

        while 1:
            l,data = inp.read()
            #TODO NOISE Cancellation

            if l:
                for i in range(len(data)/2):
                    curWindow.append(audioop.getsample(data, 2, i))

                if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
                    samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
                else:
                    samplesToCopyToMidBuffer = len(curWindow)

                midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer]
                del(curWindow[0:samplesToCopyToMidBuffer])


            if len(midTermBuffer) == midTermBufferSize:
                midTermBufferArray = np.int16(midTermBuffer)

                #TODO Noise Filtering
                #filtb, filta = butter_bandpass(lowcut,highcut,Fs,8)
                #midTermBufferArray = filtfilt(filtb , filta , midTermBufferArray)
                #wavfile.write(curWavFileName, Fs, midTermBufferArray)

                print midTermBufferArray
                segmentsArray,ProbOnSet = self.audioSVMSegmentation(midTermBufferArray,0.02,0.02,plot = False)

                x = np.array([])
                for segments in segmentsArray:
                    if x.shape<=0:
                        x = midTermBufferArray[segments[0]*self.Fs:segments[1]*self.Fs]
                        continue
                    else:
                        x = np.hstack((x,midTermBufferArray[segments[0]*self.Fs:segments[1]*self.Fs]))
                return x
def recordAudioSegments(request, RecordPath, BLOCKSIZE):
    print "Press Ctr+C to stop recording"
    RecordPath += os.sep
    d = os.path.dirname(RecordPath)
    if os.path.exists(d) and RecordPath != ".":
        shutil.rmtree(RecordPath)
    os.makedirs(RecordPath)

    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK)
    inp.setchannels(1)
    inp.setrate(16000)
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
    inp.setperiodsize(512)
    midTermBufferSize = int(16000 * BLOCKSIZE)
    midTermBuffer = []
    curWindow = []
    elapsedTime = "%08.3f" % (time.time())
    while 1:
        l, data = inp.read()
        if l:
            for i in range(len(data) / 2):
                curWindow.append(audioop.getsample(data, 2, i))

            if (len(curWindow) + len(midTermBuffer) > midTermBufferSize):
                samplesToCopyToMidBuffer = midTermBufferSize - len(
                    midTermBuffer)
            else:
                samplesToCopyToMidBuffer = len(curWindow)

            midTermBuffer = midTermBuffer + curWindow[
                0:samplesToCopyToMidBuffer]
            del (curWindow[0:samplesToCopyToMidBuffer])

        if len(midTermBuffer) == midTermBufferSize:
            global F
            # allData = allData + midTermBuffer
            curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"
            midTermBufferArray = numpy.int16(midTermBuffer)
            wavfile.write(curWavFileName, 16000, midTermBufferArray)
            # print "AUDIO  OUTPUT: Saved " + curWavFileName
            [Fs2,
             x] = audioBasicIO.readAudioFile('./rec/' + curWavFileName[5:])
            x = audioBasicIO.stereo2mono(x)
            F = stFeatureExtraction(x, Fs2, Fs2 * 0.050, Fs2 * 0.025)
            F = np.mean(F, axis=1)
            print F
            midTermBuffer = []
            elapsedTime = "%08.3f" % (time.time())
def recordAudioSegments(RecordPath, BLOCKSIZE):
    # This function is used for recording audio segments (until ctr+c is pressed)
    # ARGUMENTS:
    # - RecordPath:		the path where the wav segments will be stored
    # - BLOCKSIZE:		segment recording size (in seconds)
    #
    # NOTE: filenames are based on clock() value

    print "Press Ctr+C to stop recording"
    RecordPath += os.sep
    d = os.path.dirname(RecordPath)
    if os.path.exists(d) and RecordPath != ".":
        shutil.rmtree(RecordPath)
    os.makedirs(RecordPath)

    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK)
    inp.setchannels(1)
    inp.setrate(Fs)
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
    inp.setperiodsize(512)
    midTermBufferSize = int(Fs * BLOCKSIZE)
    midTermBuffer = []
    curWindow = []
    elapsedTime = "%08.3f" % (time.time())
    while 1:
        l, data = inp.read()
        if l:
            for i in range(len(data) / 2):
                curWindow.append(audioop.getsample(data, 2, i))

            if (len(curWindow) + len(midTermBuffer) > midTermBufferSize):
                samplesToCopyToMidBuffer = midTermBufferSize - len(
                    midTermBuffer)
            else:
                samplesToCopyToMidBuffer = len(curWindow)

            midTermBuffer = midTermBuffer + curWindow[
                0:samplesToCopyToMidBuffer]
            del (curWindow[0:samplesToCopyToMidBuffer])

        if len(midTermBuffer) == midTermBufferSize:
            # allData = allData + midTermBuffer
            curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"
            midTermBufferArray = numpy.int16(midTermBuffer)
            wavfile.write(curWavFileName, Fs, midTermBufferArray)
            print "AUDIO  OUTPUT: Saved " + curWavFileName
            midTermBuffer = []
            elapsedTime = "%08.3f" % (time.time())
def recordAudioSegments(RecordPath, BLOCKSIZE):	
	# This function is used for recording audio segments (until ctr+c is pressed)
	# ARGUMENTS:
	# - RecordPath:		the path where the wav segments will be stored
	# - BLOCKSIZE:		segment recording size (in seconds)
	# 
	# NOTE: filenames are based on clock() value
	
	print "Press Ctr+C to stop recording"
	RecordPath += os.sep
	d = os.path.dirname(RecordPath)
	if os.path.exists(d) and RecordPath!=".":
		shutil.rmtree(RecordPath)	
	os.makedirs(RecordPath)	

	inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
	inp.setchannels(1)
	inp.setrate(Fs)
	inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
	inp.setperiodsize(512)
	midTermBufferSize = int(Fs*BLOCKSIZE)
	midTermBuffer = []
	curWindow = []
	elapsedTime = "%08.3f" % (time.time())
	while 1:
			l,data = inp.read()		   
		    	if l:
				for i in range(len(data)/2):
					curWindow.append(audioop.getsample(data, 2, i))
		
				if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
					samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
				else:
					samplesToCopyToMidBuffer = len(curWindow)

				midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer];
				del(curWindow[0:samplesToCopyToMidBuffer])
			

			if len(midTermBuffer) == midTermBufferSize:
				# allData = allData + midTermBuffer				
				curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"				
				midTermBufferArray = numpy.int16(midTermBuffer)
				wavfile.write(curWavFileName, Fs, midTermBufferArray)
				print "AUDIO  OUTPUT: Saved " + curWavFileName
				midTermBuffer = []
				elapsedTime = "%08.3f" % (time.time())	
예제 #30
0
def get_wav(fname):
	"""
	Given a filename of a wav, returns a tuple of the wav and its parameters.
	"""
	w = wave.open(fname)
	wp = w.getparams()
	wd = w.readframes(wp[3])
	if wp[0] > 1:
		md = audioop.tomono(wd, wp[1], 1, 1)
	else:
		md = wd

	wav = []
	for i in range(wp[3]):
		wav.append(audioop.getsample(md, wp[1], i))
	wav = numpy.array(wav)

	return wav, wp
예제 #31
0
파일: utilities.py 프로젝트: mjibson/junk
def get_wav(fname):
    """
	Given a filename of a wav, returns a tuple of the wav and its parameters.
	"""
    w = wave.open(fname)
    wp = w.getparams()
    wd = w.readframes(wp[3])
    if wp[0] > 1:
        md = audioop.tomono(wd, wp[1], 1, 1)
    else:
        md = wd

    wav = []
    for i in range(wp[3]):
        wav.append(audioop.getsample(md, wp[1], i))
    wav = numpy.array(wav)

    return wav, wp
예제 #32
0
파일: sound.py 프로젝트: TheTechnobear/OTC
def recv() :
    global inp, etc, trig_this_time, trig_last_time, sin
    # get audio
    l,data = inp.read()
    peak = 0
    while l:
        try :
            ldata = audioop.tomono(data, 2, 1, 0)
            rdata = audioop.tomono(data, 2, 0, 1)
            for i in range(0,100) :
                avg_l = 0
                avg_r = 0

                idx = i * 3
                avg_l +=audioop.getsample(ldata,2,idx)
                avg_r +=audioop.getsample(rdata,2,idx)

                idx += 1
                avg_l +=audioop.getsample(ldata,2,idx)
                avg_r +=audioop.getsample(rdata,2,idx)

                idx += 1
                avg_l +=audioop.getsample(ldata,2,idx)
                avg_r +=audioop.getsample(rdata,2,idx)

                avg_l = avg_l / 3
                avg_r = avg_r / 3
                
                avg = (avg_l + avg_r) / 2


                if (avg > 20000) :
                    trig_this_time = time.time()
                    if (trig_this_time - trig_last_time) > .05:
                        etc.audio_trig = True
                        trig_last_time = trig_this_time
                if avg > peak :
                    etc.audio_peak = avg
                    peak = avg
                # if the trigger button is held
                if (etc.trig_button) :
                    etc.audio_in[i] = sin[i] 
                    etc.audio_left[i] = sin[i] 
                    etc.audio_right[i] = sin[i] 
                else :
                    etc.audio_in[i] = avg
                    etc.audio_left[i] = avg_l
                    etc.audio_right[i] = avg_r

        except :
            pass
        l,data = inp.read()
예제 #33
0
def get_samples(file, buffer = 4096):
    """Renvoie les échantillons du fichier WAVE PCM signé spécifié sous la forme de réels entre -1 et 1"""
    
    with open(file, "rb") as f:
        f=wave.open(f, "rb")
        assert f.getnchannels() == 1, "Only mono wav files are supported."
        width = f.getsampwidth()

        length = f.getnframes()/f.getframerate()
        print(length, "seconds")

        frames = None
        max_amplitude = 1<<(8*width -1)
        while frames != "":
            frames = f.readframes(buffer*width)
            for index in range(len(frames)//width):
                sample = audioop.getsample(frames, width, index)
                yield sample/max_amplitude
예제 #34
0
파일: sample.py 프로젝트: irmen/synthesizer
 def fadein(self, seconds, start_volume=0.0):
     """Fade the start of the sample in from the starting volume (usually zero) in the given time."""
     assert not self.__locked
     faded = Sample.get_array(self.__samplewidth)
     seconds = min(seconds, self.duration)
     i = self.frame_idx(seconds)
     begin = self.__frames[:i]  # we fade this chunk
     end = self.__frames[i:]
     numsamples = len(begin)/self.__samplewidth
     increase = 1-start_volume
     for i in range(int(numsamples)):
         amplitude = i*increase/numsamples+start_volume
         s = audioop.getsample(begin, self.__samplewidth, i)
         faded.append(int(s*amplitude))
     begin = faded.tobytes()
     if sys.byteorder == "big":
         begin = audioop.byteswap(begin, self.__samplewidth)
     self.__frames = begin + end
     return self
예제 #35
0
파일: sample.py 프로젝트: irmen/synthesizer
 def fadeout(self, seconds, target_volume=0.0):
     """Fade the end of the sample out to the target volume (usually zero) in the given time."""
     assert not self.__locked
     faded = Sample.get_array(self.__samplewidth)
     seconds = min(seconds, self.duration)
     i = self.frame_idx(self.duration-seconds)
     begin = self.__frames[:i]
     end = self.__frames[i:]  # we fade this chunk
     numsamples = len(end)/self.__samplewidth
     decrease = 1-target_volume
     for i in range(int(numsamples)):
         amplitude = 1-(i/numsamples)*decrease
         s = audioop.getsample(end, self.__samplewidth, i)
         faded.append(int(s*amplitude))
     end = faded.tobytes()
     if sys.byteorder == "big":
         end = audioop.byteswap(end, self.__samplewidth)
     self.__frames = begin + end
     return self
예제 #36
0
    def start(self):
        import alsaaudio, audioop

        inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE)
        inp.setchannels(1)
        inp.setrate(rate)
        inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        inp.setperiodsize(slen)

        while True:
            l,data = inp.read()
            if l > 0:
                ln = len(data)
                samples = scipy.zeros(ln / 2)
                fft = scipy.zeros(ln / 2)
                for i in range(0, ln / 2):
                    samples[i]=audioop.getsample(data, 2, i)

                self.output({ 'recording': samples, 'rate': rate, 'slen': slen })
            time.sleep(.01)
예제 #37
0
    def start(self):
        import alsaaudio, audioop

        inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE)
        inp.setchannels(1)
        inp.setrate(rate)
        inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        inp.setperiodsize(slen)

        while True:
            l, data = inp.read()
            if l > 0:
                ln = len(data)
                samples = scipy.zeros(ln / 2)
                fft = scipy.zeros(ln / 2)
                for i in range(0, ln / 2):
                    samples[i] = audioop.getsample(data, 2, i)

                self.output({'recording': samples, 'rate': rate, 'slen': slen})
            time.sleep(.01)
예제 #38
0
파일: moosic.py 프로젝트: ivarkrabol/moosic
def freqsFromFrag(frag, sampwidth, rate):
    samples = [audioop.getsample(frag, sampwidth, i) for i in range(len(frag) / sampwidth)]
    freqs = {}
    pos0 = samples[0] > 0
    t0 = 0.0
    t1 = 0.0
    for i in range(1, len(samples)):
        pos1 = samples[i] > 0
        # out('%5s - %5s - %8.2f - %8.2f - %8.2f' % (pos0, pos1, t0, t1, samples[i]))
        if pos0 != pos1:
            t2 = i - 1.0 + samples[i - 1] / (samples[i - 1] - samples[i])
            if t2 > t1 > t0 > 0.0:
                freq = float(rate) / (t2 - t0)
                if not freqs.has_key(freq):
                    freqs[freq] = 1
                else:
                    freqs[freq] += 1
            pos0 = pos1
            t0 = t1
            t1 = t2
    return freqs
예제 #39
0
 def read_processed_chunk(self):
     data = self.read_raw_chunk()
     return [self.noise_cancel(audioop.getsample(data, 2, x)) for x in range(0, self.chunk_size)]
예제 #40
0
def get_samples(data, width=2):
    return list(audioop.getsample(data, width, i) for i in xrange(len(data) / width))
예제 #41
0
def gendata2():
    if audioop.getsample('\0\1', 2, 0) == 1:
        return '\0\0\0\1\0\2'
    else:
        return '\0\0\1\0\2\0'
예제 #42
0
 def test_getsample(self):
     for i in range(3):
         self.assertEqual(audioop.getsample(data[0], 1, i), i)
         self.assertEqual(audioop.getsample(data[1], 2, i), i)
         self.assertEqual(audioop.getsample(data[2], 4, i), i)
예제 #43
0
def gendata2():
    if audioop.getsample('\0\1', 2, 0) == 1:
        return '\0\0\0\1\0\2'
    else:
        return '\0\0\1\0\2\0'
예제 #44
0
 def test_getsample(self):
     for i in range(3):
         self.assertEqual(audioop.getsample(data[0], 1, i), i)
         self.assertEqual(audioop.getsample(data[1], 2, i), i)
         self.assertEqual(audioop.getsample(data[2], 4, i), i)
def neuralNetClassidication(duration, midTermBufferSizeSec, modelName):
    n_dim = 68
    n_classes = 2
    n_hidden_units_one = 280
    n_hidden_units_two = 300
    sd = 1 / numpy.sqrt(n_dim)
    learning_rate = 0.01

    X = tf.placeholder(tf.float32, [None, n_dim])
    Y = tf.placeholder(tf.float32, [None, n_classes])

    W_1 = tf.Variable(tf.random_normal([n_dim, n_hidden_units_one],
                                       mean=0,
                                       stddev=sd),
                      name="W_1")
    b_1 = tf.Variable(tf.random_normal([n_hidden_units_one], mean=0,
                                       stddev=sd),
                      name="b_1")
    h_1 = tf.nn.tanh(tf.matmul(X, W_1) + b_1)

    W_2 = tf.Variable(tf.random_normal(
        [n_hidden_units_one, n_hidden_units_two], mean=0, stddev=sd),
                      name="W_2")
    b_2 = tf.Variable(tf.random_normal([n_hidden_units_two], mean=0,
                                       stddev=sd),
                      name="b_2")
    h_2 = tf.nn.sigmoid(tf.matmul(h_1, W_2) + b_2)

    W = tf.Variable(tf.random_normal([n_hidden_units_two, n_classes],
                                     mean=0,
                                     stddev=sd),
                    name="W")
    b = tf.Variable(tf.random_normal([n_classes], mean=0, stddev=sd), name="b")
    y_ = tf.nn.softmax(tf.matmul(h_2, W) + b)

    saver = tf.train.Saver()

    cost_function = tf.reduce_mean(
        -tf.reduce_sum(Y * tf.log(y_), reduction_indices=[1]))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        cost_function)

    correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(Y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    cost_history = numpy.empty(shape=[1], dtype=float)
    y_true, y_pred = None, None

    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK)
    inp.setchannels(1)
    inp.setrate(Fs)
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
    inp.setperiodsize(512)
    midTermBufferSize = int(midTermBufferSizeSec * Fs)
    allData = []
    midTermBuffer = []
    curWindow = []
    count = 0

    with tf.Session() as sess:
        # sess.run(init)

        saver.restore(sess, modelName)
        while len(allData) < duration * Fs:
            # Read data from device
            l, data = inp.read()
            if l:
                for i in range(l):
                    curWindow.append(audioop.getsample(data, 2, i))
                if (len(curWindow) + len(midTermBuffer) > midTermBufferSize):
                    samplesToCopyToMidBuffer = midTermBufferSize - len(
                        midTermBuffer)
                else:
                    samplesToCopyToMidBuffer = len(curWindow)
                midTermBuffer = midTermBuffer + curWindow[
                    0:samplesToCopyToMidBuffer]
                del (curWindow[0:samplesToCopyToMidBuffer])
            if len(midTermBuffer) == midTermBufferSize:
                count += 1
                [mtFeatures,
                 stFeatures] = aF.mtFeatureExtraction(midTermBuffer, Fs,
                                                      2.0 * Fs, 2.0 * Fs,
                                                      0.020 * Fs, 0.020 * Fs)
                features = numpy.array([mtFeatures[:, 0]])
                y_pred = sess.run(tf.argmax(y_, 1), feed_dict={X: features})
                if y_pred[0] == 0:
                    print "Class A"
                else:
                    print "Class B"
                allData = allData + midTermBuffer

                plt.clf()
                plt.plot(midTermBuffer)
                plt.show(block=False)
                plt.draw()
                midTermBuffer = []
def recordAnalyzeAudio(duration, outputWavFile, midTermBufferSizeSec, modelName, modelType):
	'''
	recordAnalyzeAudio(duration, outputWavFile, midTermBufferSizeSec, modelName, modelType)

	This function is used to record and analyze audio segments, in a fix window basis.

	ARGUMENTS: 
	- duration			total recording duration
	- outputWavFile			path of the output WAV file
	- midTermBufferSizeSec		(fix)segment length in seconds
	- modelName			classification model name
	- modelType			classification model type

	'''

	if modelType=='svm':
		[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, compute_beat] = aT.load_model(modelName)
	elif modelType=='knn':
		[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, compute_beat] = aT.load_model_knn(modelName)
	else:
		Classifier = None

	inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK)
	inp.setchannels(1)
	inp.setrate(Fs)
	inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
	inp.setperiodsize(512)
	midTermBufferSize = int(midTermBufferSizeSec * Fs)
	allData = []
	midTermBuffer = []
	curWindow = []
	count = 0

	while len(allData)<duration*Fs:
		# Read data from device
		l,data = inp.read()
	    	if l:
			for i in range(l):
				curWindow.append(audioop.getsample(data, 2, i))		
			if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
				samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
			else:
				samplesToCopyToMidBuffer = len(curWindow)
			midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer];
			del(curWindow[0:samplesToCopyToMidBuffer])
		if len(midTermBuffer) == midTermBufferSize:
			count += 1						
			if Classifier!=None:
				[mtFeatures, stFeatures, _] = aF.mtFeatureExtraction(midTermBuffer, Fs, 2.0*Fs, 2.0*Fs, 0.020*Fs, 0.020*Fs)
				curFV = (mtFeatures[:,0] - MEAN) / STD;
				[result, P] = aT.classifierWrapper(Classifier, modelType, curFV)
				print classNames[int(result)]
			allData = allData + midTermBuffer

			plt.clf()
			plt.plot(midTermBuffer)
			plt.show(block = False)
			plt.draw()


			midTermBuffer = []

	allDataArray = numpy.int16(allData)
	wavfile.write(outputWavFile, Fs, allDataArray)
예제 #47
0
        imgx = len(freqHistory)
        imgy = len(freqHistory[0])
        print "imgx=%d, imgy=%d" % (imgx, imgy)
        imgArr = numpy.zeros(shape=(imgy, imgx))
        for x in range(0, imgx):
            for y in range(0, imgy):
                imgArr[y, x] = freqHistory[x][y]
        print "plotting image"
        if (im == None):
            pylab.xlabel("time (sec)")
            pylab.ylabel("freq (Hz)")
            pylab.xlim(0, historyLen)
            pylab.ylim(0, 1000)
            im = ax3.imshow(imgArr, aspect='auto')
            im.set_cmap('prism')
        else:
            im = ax3.imshow(imgArr, aspect='auto')
        #			im.set_array(imgArr)
        fig2.canvas.draw()

        t_start = time.time()
        samples = []
    else:
        # Read data from device
        l, data = inp.read()
        if l:
            for n in range(l):
                samples.append(audioop.getsample(data, 2, n))
            #sys.stdout.write('.')
            #sys.stdout.flush()
def recordAudioSegments(RecordPath, Bs, plot=False):	#Bs: BlockSize
	"""RecordPath += os.sep
	d = os.path.dirname(RecordPath)
	if os.path.exists(d) and RecordPath!=".":
		shutil.rmtree(RecordPath)
	os.makedirs(RecordPath)"""

	inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
	inp.setchannels(1)
	inp.setrate(Fs)
	inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
	inp.setperiodsize(512)
	midTermBufferSize = int(Fs*Bs)
	midTermBuffer = []
	curWindow = []
	elapsedTime = "%08.3f" % (time.time())
	while 1:
		l,data = inp.read()
		#TODO NOISE Cancellation
		#print "Now listening:"
		if l:
			for i in range(len(data)/2):
				curWindow.append(audioop.getsample(data, 2, i))

			if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
				samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
			else:
				samplesToCopyToMidBuffer = len(curWindow)

			midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer]
			del(curWindow[0:samplesToCopyToMidBuffer])


		if len(midTermBuffer) == midTermBufferSize:
			midTermBufferArray = numpy.int16(midTermBuffer)
			#curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"
			#print midTermBuffer.__class__

			#TODO Noise Filtering
			#filtb, filta = butter_bandpass(lowcut,highcut,Fs,8)
			#midTermBufferArray = filtfilt(filtb , filta , midTermBufferArray)
			#wavfile.write(curWavFileName, Fs, midTermBufferArray)
			#TODO FeatureExtraction call to segmentation
			#TODO Add call to segment voice i.e. svmSegmentation function
			#adaptfilt.lms()
			try:
				segmentsArray,ProbOnSet = svmSegmentation(midTermBufferArray,Fs,0.05,0.05,plot = False)
			except:
				continue

			x = numpy.array([])
			for segments in segmentsArray:
				if x.shape<=0:
					x = midTermBufferArray[segments[0]*Fs:segments[1]*Fs]
					continue
				else:
					x = numpy.hstack((x,midTermBufferArray[segments[0]*Fs:segments[1]*Fs]))

			if plot == 'seg':
				plotSegments(midTermBufferArray,Fs,segmentsArray,ProbOnSet)
				break
			Features = FeatureExtraction(midTermBufferArray,Fs,x.shape[0],x.shape[0])
			#guiWrite(gui,"text")
			features = Features.T.tolist()
			for axis0 in xrange(0,features.__len__()):
				normalizedFeatures = []
				for axis1 in xrange(0,features[axis0].__len__()):
					normalizedFeatures.append((features[axis0][axis1]-minlist[axis1])/denom[axis1])
				#print normalizedFeatures
			plotdata = []
			res = neuralNetwork.forward(normalizedFeatures)


			fig, ax =plt.subplots()
			ar = numpy.arange(7)
			print res
			barPlot = ax.bar(ar,res,0.35)
			plt.show()


			print "Happiness:",res[0],"Anger:",res[1],"Fear:",res[2],"Boredom:",res[3],"Disgust:",res[4],"Sadness:",res[5],"Neutral:",res[6]
			if plot == 'energy':
				plotEnergy(Features[1])
			#print "AUDIO  OUTPUT: Saved " + curWavFileName
			midTermBuffer = []
			elapsedTime = "%08.3f" % (time.time())
예제 #49
0
	def run(self):
	# Adapted from pyAudioAnalysis
	# [email protected]:tyiannak/pyAudioAnalysis.git 
	# 
	# Funtion records an audio segment of len RECORDING LEN
	# and stores it in record_path  
		RecordPath = RECORD_PATH

		RecordPath += os.sep
		d = os.path.dirname(RecordPath)
		if os.path.exists(d) and RecordPath!=".":
			shutil.rmtree(RecordPath)   
		os.makedirs(RecordPath) 

		inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
		inp.setchannels(1)
		inp.setrate(Fs)
		inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
		inp.setperiodsize(512)
		midTermBuffer = []
		curWindow = []
		elapsedTime = "%08.3f" % (time.time())

		# Main event loop
		while True:
			while len(get_occupants()) == 0:
				logging.debug('Waiting for occupants')
				time.sleep(WAIT_FOR_OCCUPANT_TIME)

			logging.debug("Starting recording")

			#*******************************************************************

			while len(midTermBuffer) < midTermBufferSize:
				l,data = inp.read()
				if l:
					for i in range(len(data)/2):
						curWindow.append(audioop.getsample(data, 2, i))

					if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
						samplesToCopyToMidBuffer = midTermBufferSize \
												   - len(midTermBuffer)
					else:
						samplesToCopyToMidBuffer = len(curWindow)

					midTermBuffer = midTermBuffer \
									+ curWindow[0:samplesToCopyToMidBuffer];
					del(curWindow[0:samplesToCopyToMidBuffer])

			#*******************************************************************

			logging.debug("Finnished recording")
			end_datetime = datetime.datetime.now()

			# allData = allData + midTermBuffer             
			curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"                
			midTermBufferArray = numpy.int16(midTermBuffer)
			wavfile.write(curWavFileName, Fs, midTermBufferArray)
			midTermBuffer = []
			
			elapsedTime = "%08.3f" % (time.time())  

			# See who's in the room now
			occupants = get_occupants()
			if len(occupants) == 0:
				# Delete the recording, nobody present
				logging.debug('No occupants, deleting: ' + str(recording))
				os.remove(recording)
			else:
				# Enqueue the file to be processed
				unprocessed_files.put((curWavFileName, occupants))
				logging.debug('Enqueuing ' + str(curWavFileName) + ' : ' \
							  + str(unprocessed_files.qsize()) + ' items in queue')
			pass
예제 #50
0
from numpy import *
import wave 
import audioop

wav_in=wave.open('phb.wav','r')
contents=wav_in.readframes(wav_in.getnframes())
# contents now holds all the samples in a long text string.
samples_list=[]; ctr=0

while 1:
    try:
        samples_list.append(audioop.getsample(contents,2,ctr))
        # "2" refers to no. of bytes/sample, ctr is sample no. to take.
        ctr+=1
    except:
        # We jump to here when we run out of samples
        break

out_array=array(samples_list)
out_fft=fft.fft(out_array)

for i in out_fft:
    print i

예제 #51
0
파일: b.py 프로젝트: leshy/fourier-to-midi
time_before = datetime.datetime.now()
left_over = 0

while not SongOver:
    try:
        # Read data from device
        l, data = inp.read()

        if l > 0:
            mySamples = zeros(64)
            myFFT = zeros(64)
            myFFTAbs = zeros(16)

            # copy the samples
            for iLoop in range(0, 64):
                mySamples[iLoop] = audioop.getsample(data, 2, iLoop)
                myBigSamples[myBigSamplePos] = mySamples[iLoop]
                myBigSamplePos += 1

            # perform fft
            myFFT = fft(mySamples)

            s = ""
            for iLoop in range(0, 64):
                s += str(mySamples[iLoop]) + ","
            #s += "\n"
            fSamples.write(s)

            for iLoop in range(0, 16):
                myFFTAbs[iLoop] = abs(myFFT[iLoop])
예제 #52
0
파일: whistle.py 프로젝트: tribela/whistle
 def read(self):
     data = self.stream.read(1024)
     samples = [audioop.getsample(data, 2, n) for n in range(0, 1024)]
     return samples
예제 #53
0
def get_samples(data):
    return list(audioop.getsample(data, 2, i) for i in xrange(len(data) / 4))
def recordAnalyzeAudio(duration, outputWavFile, midTermBufferSizeSec,
                       modelName, modelType):
    '''
	recordAnalyzeAudio(duration, outputWavFile, midTermBufferSizeSec, modelName, modelType)

	This function is used to record and analyze audio segments, in a fix window basis.

	ARGUMENTS: 
	- duration			total recording duration
	- outputWavFile			path of the output WAV file
	- midTermBufferSizeSec		(fix)segment length in seconds
	- modelName			classification model name
	- modelType			classification model type

	'''
    if modelType == 'neuralnet':
        neuralNetClassidication(duration, midTermBufferSizeSec, modelName)
    else:

        if modelType == 'svm':
            [
                Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin,
                stStep, computeBEAT
            ] = aT.loadSVModel(modelName)
        elif modelType == 'knn':
            [
                Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin,
                stStep, computeBEAT
            ] = aT.loadKNNModel(modelName)
        else:
            Classifier = None

        inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK)
        inp.setchannels(1)
        inp.setrate(Fs)
        inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        inp.setperiodsize(512)
        midTermBufferSize = int(midTermBufferSizeSec * Fs)
        allData = []
        midTermBuffer = []
        curWindow = []
        count = 0
        # a sequence of samples
        # process a sequence
        # speed
        # emergency vehicle detection what have they done? emergency vehicle classification patents
        # plot features!!!
        # patents extracted!
        # latex literature review
        # writing a paper
        #
        while len(allData) < duration * Fs:
            # Read data from device
            l, data = inp.read()
            if l:
                for i in range(l):
                    curWindow.append(audioop.getsample(data, 2, i))
                if (len(curWindow) + len(midTermBuffer) > midTermBufferSize):
                    samplesToCopyToMidBuffer = midTermBufferSize - len(
                        midTermBuffer)
                else:
                    samplesToCopyToMidBuffer = len(curWindow)
                midTermBuffer = midTermBuffer + curWindow[
                    0:samplesToCopyToMidBuffer]
                del (curWindow[0:samplesToCopyToMidBuffer])
            if len(midTermBuffer) == midTermBufferSize:
                count += 1
                if Classifier != None:
                    [mtFeatures, stFeatures
                     ] = aF.mtFeatureExtraction(midTermBuffer, Fs, 2.0 * Fs,
                                                2.0 * Fs, 0.020 * Fs,
                                                0.020 * Fs)
                    curFV = (mtFeatures[:, 0] - MEAN) / STD
                    [result, P] = aT.classifierWrapper(Classifier, modelType,
                                                       curFV)
                    print classNames[int(result)]
                allData = allData + midTermBuffer

                plt.clf()
                plt.plot(midTermBuffer)
                plt.show(block=False)
                plt.draw()

                midTermBuffer = []

        allDataArray = numpy.int16(allData)
        wavfile.write(outputWavFile, Fs, allDataArray)
예제 #55
0
def list_samples(sample_bytes, params):
    return [
        audioop.getsample(sample_bytes, params.nchannels, i)
        for i in range(params.nframes)
    ]
예제 #56
0
start = time.time()
while True:
    if time.time() - send_t > 1:
        s.sendto(b'', ('255.255.255.255', 18294))
        send_t = time.time()

    try:
        m = s.recvfrom(1024)
    except timeout:
        m = None
    
    if m and len(m[0]) > 0:
        t = time.time()
        print '%0.3f' % (t - start), m[1], len(m[0])
        
        s.sendto(sine_packet, m[1])
        
        start = t
        m = audioop.alaw2lin(m[0][8:], 2)
        for i in range(len(m) / 2):
            sample = audioop.getsample(m, 2, i)
            outfile.write('%6d\n' % sample)
        
        if not stream_started:
            stream.start()
        
        stream.write(m)
        outfile2.write(m)