2

The question is related to this discussion ([link][1]).

@chris,

Your answer helps a lot. Thanks. My use case is the following: Upon an event on my Python script running on my PC, I need to send a command to the RPi to record 5 seconds from a USB microphone plugged to my RPi.

The Python script (I lost credit to ; sorry about that) runs well on RPi when I run it manually from PuTTY windows.

import pyaudio
import wave

form_1 = pyaudio.paInt16 # 16-bit resolution
chans = 1 # 1 channel
#samp_rate = 44100 # 44.1kHz sampling rate
samp_rate = 16000 # 16kHz sampling rate

chunk = 4096 # 2^12 samples for buffer
record_secs = 3 # seconds to record
dev_index = 2 # device index found by p.get_device_info_by_index(ii)
wav_output_filename = 'test1.wav' # name of .wav file

audio = pyaudio.PyAudio() # create pyaudio instantiation

# create pyaudio stream
stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
                    input_device_index = dev_index,input = True, \
                    frames_per_buffer=chunk)
print("recording")
frames = []

# loop through stream and append audio chunks to frame array
for ii in range(0,int((samp_rate/chunk)*record_secs)):
    data = stream.read(chunk)
    frames.append(data)

print("finished recording")

# stop the stream, close it, and terminate the pyaudio instantiation
stream.stop_stream()
stream.close()
audio.terminate()

# save the audio frames as .wav file
wavefile = wave.open(wav_output_filename,'wb')
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b''.join(frames))
wavefile.close()

On my PC I have the following script thanks to you @chris :

from paramiko import SSHClient, AutoAddPolicy

def Connect(ip, username='pi', pw='raspberry'): 
    '''ssh into the pi'''
    print('connecting to {}@{}...'.format(username, ip))
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(ip, username=username, password=pw)
    print('connection status =', ssh.get_transport().is_active())
    return ssh

def SendCommand(ssh, command, pw='password'):
    '''send a terminal/bash command to the ssh'ed-into machine '''
    print('sending a command... ', command)
    stdin, stdout, stderr = ssh.exec_command( command )

    if "sudo" in command:
        stdin.write(pw+'\n')

    stdin.flush()

    out = stdout.read()
    print(f'stout =  {out}')
    print(f'sterr = {stderr.read()}')

myssh = Connect(ip='192.168.x.y') #replace x and y by right value
SendCommand(myssh, command='sudo python ~/TestDirectory/TestAudio1.py')

But when I run it on my PC from Spyder I have the following output:

connecting to pi@192.168.x.y... connection status = True sending a command... sudo python ~/TestDirectory/TestAudio1.py stout = b'recording\nfinished recording\n' sterr = b"ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835.pcm.front.0:CARD=0'\n [etc etc] No such file or directory\nALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa\nconnect(2) call to /tmp/jack-0/default/jack_0 failed (err=No such file or directory)\nattempt to connect to server failed\n"

So the script starts well since I see "stout = b'recording\nfinished recording\n'" but then I have the sterr. Any clue where this issue is coming from? Thanks in advance for your help

EDIT April 16th 2019 Eventually I found out that, despite the big error message, the audio file was actually correctly created. So no worries anymore

  • How do I "close" the question? –  projectdrone2 Apr 16 at 20:11
  • You can finish the question by making an answer. Then after two days you can accept it yourself. – Ingo Apr 17 at 19:31
1

Eventually I found out that, despite the big error message, the audio file was actually correctly created. So no worries anymore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.