How to install pyaudio in Axon?

How to setup pyaudio in Axon

  1. sudo pip install pipwin
  2. Add this line in ~/.bashrc file export PATH="$HOME/.local/bin:$PATH" and run command source ~/.bashrc
  3. sudo apt-get install portaudio19-dev
  4. sudo pip install pyaudio

If you get an error like, then below python setup should be followed :point_down:

error: externally-managed-environment
externally-managed-environment then the 
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.

  1. The below command will create .vevn hidden folder for Python setup.

     python -m venv .venv
    
  2. source /path/to/folder/.venv

    e.g.

    source ~/.venv/bin/activate
    
  3. You can see that you are in python environment.

    (.venv) vicharak@vicharak:~/$  
    
  4. Now, Run below command :

    pip install pipwin 
    
    sudo apt-get install portaudio19-dev
    
  5. Install pyaudio using pip :point_down:

    pip install pyaudio
    

For Info, Click on the below link :

User may need to pass the available audio device when using command to record :

Using arecord

arecord -D hw:0,0 -f cd -t wav -d 10 test.wav
  1. Using Pyaudio
p = pyaudio.PyAudio()

print("Available input devices:")
for i in range(p.get_device_count()):
    info = p.get_device_info_by_index(i)
    if info["maxInputChannels"] > 0:
        print(f"{i}: {info['name']}")

p.terminate()

You can get output like this :

0: rockchip-es8388: dailink-multicodecs ES8323 HiFi-0 (hw:0,0)

0 = index number

Voice Recording Code :point_down:

(.venv) vicharak@vicharak:~$ cat record_.py
import pyaudio
import wave

**index = 0**  # Replace with input device index

p = pyaudio.PyAudio()

stream = p.open(format=pyaudio.paInt16,
                channels=1,
                rate=44100,
                input=True,
                input_device_index=index,
                frames_per_buffer=1024)

frames = []

print("Recording...")

for _ in range(0, int(44100 / 1024 * 5)):
    data = stream.read(1024)
    frames.append(data)

print("Done.")

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

wf = wave.open("pyaudio_test.wav", 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
wf.setframerate(44100)
wf.writeframes(b''.join(frames))
wf.close()

Run Recorded File :point_down:

ffplay pyaudio_test.wav