Example #1
0
    def stopClicked(self):
        try:

            sounddevice.stop()
            self.recordvoice = self.recordvoice[self.recordvoice != 0]
            print(self.recordvoice)
            sounddevice.get_stream()
            print(len(self.recordvoice))
            write('output.wav', self.fs, np.array([0, 0, 0, 0, 0, 0]))
            write('output.wav', self.fs, self.recordvoice)
            print('done')
            test, fs = lr.load('output.wav')
            sounddevice.play(self.recordvoice, 22050)
            print(fs)
            print(test)
            test = np.array(test)
            test = test[test != 0]
            test = np.mean(lr.feature.mfcc(test, sr=22050).T, axis=0)
            print(test)
            emoIndex = self.classifier.predict([test])[0]
            print(emoIndex)
            wavio.write('output.wav', self.recordvoice, self.fs, sampwidth=2)
            r = sr.Recognizer()
            with sr.AudioFile('output.wav') as source:
                audio = r.listen(source)
                said = ''

                try:
                    said = r.recognize_google(
                        audio,
                        language=self.translateLanguageCodes[
                            self.comboBox.currentIndex()])
                    self.textEdit.setText(said)

                except Exception as e:
                    print(e)
            t = self.translator.translate(
                text=said,
                src=self.translateLanguageCodes[self.comboBox.currentIndex()],
                dest=self.translateLanguageCodes[
                    self.comboBox_2.currentIndex()])
            self.textEdit_2.setText(t.text)
            if emoIndex == 0:
                self.emoBox.setCurrentIndex(2)
                self.setStyleSheet(self.colors[2])
            else:
                self.emoBox.setCurrentIndex(1)
                self.setStyleSheet(self.colors[1])
        except Exception as e:
            print(e)
Example #2
0
 def run(self):
     # thread main
     play_te(self.fname, self.te, blocking=False)
     self.stream = sounddevice.get_stream()
     while self.stream.active:
         # poll because the underlying portaudio c-library segfaults on multithreaded access
         sleep(0.05)
         if self.stop_requested:
             sounddevice.stop()
             return
Example #3
0
def speak():
    # Cast die
    alea = (int)(random()*len(samples))

    # Play sound
    data, fs = sf.read(dir+samples[alea], dtype='float32')      
    sd.play(data, fs)

    # Move mouths and eyes
    while sd.get_stream().active:
        with sd.Stream(sd.default.samplerate, 0, sd.default.device, 2) as stream:
            amp = stream.read(128)[0] # increase blocksize for better accuracy
            #print(amp)
            L = []
            R = []
            for i in range(len(amp)):         
                L.append(amp[i][0])
                R.append(amp[i][1])
            amp_L = round(max(L)*mouthVel_L, 1)
            amp_R = round(max(R)*mouthVel_R, 1)      

            print('Playing:', samples[alea], 'L:', amp_L, 'R:', amp_R)
            # Left audio channel
            if(amp_L > pause):
                arduino.write('ml'.encode())
                arduino.write(str(amp_L).encode())
                arduino.write('\n'.encode())
                sleep(dly)
                # Move eyes
                arduino.write('ex'.encode())
                arduino.write(str(amp_L).encode())
                arduino.write('\n'.encode())
                sleep(dly)
            # Right audio channel
            if(amp_R > pause):
                arduino.write('mr'.encode())
                arduino.write(str(amp_R).encode())
                arduino.write('\n'.encode())
                sleep(dly)
            # Blink
            # if(amp_L < pause and amp_R < pause):            
            if(random() < .05):
                arduino.write('b'.encode())            
                arduino.write('\n'.encode())
                sleep(.15)
            # Reset eyes
            if(amp_L < pause):
                resetEyes()
        
        broadcast('playing:' + samples[alea])
        resetMotors()
    resetEyes()
Example #4
0
def speak():
    # Cast die
    alea = (int)(random() * len(samples))
    sout('Alea est:' + str(alea))

    # Play sound
    data, fs = sf.read(dir + samples[alea], dtype='float32')
    sd.play(data, fs)

    # Broadcast
    mouth = socket(AF_INET, SOCK_DGRAM)
    mouth.setblocking(False)
    mouth.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
    while sd.get_stream().active:
        sout('playing:' + samples[alea])
        mouth.sendto(bytes('playing:' + samples[alea], encoding='utf-8'),
                     ('255.255.255.255', 12345))
    mouth.close()
Example #5
0
import sounddevice as sd
import soundfile as sf

filename = '/home/pi/robokerho/samples/Hurjajuttu/Puhe 006HalvintaKaljaa.wav'
data, fs = sf.read(filename, dtype='float32')
mouthVel = 90  #scale according to mouth motor
sd.play(data, fs)

while sd.get_stream().active:
    with sd.Stream() as stream:
        amp = round(stream.read(1024)[0].max(), 1) * mouthVel
        print(amp)
Example #6
0
 def active(self):
     try:
         stream = sd.get_stream()
     except RuntimeError:
         return False
     return stream.active
Example #7
0
 def _stream(self):
     return sd.get_stream()
Example #8
0
 def active(self):
     return sd.get_stream().active