[TK1 21.3] Audio synthesis

Good day,

I’m trying to synthesize audio and play this over audio jack trough a headset on the jetson tk1 r21.3. I’ve tried to get multiple audio libraries to work for me. I got the hardest with portaudio (portaudio19-dev) and running multiple examples they provided.

They all seem to work until I run them a couple of times (output provided below). In the release note about r21.3 there is a mention about hdmi to dvi converters causing audio devices not to show up. I to have this issue and tried with a hdmi >hdmi. The audio over hdmi works like a charm and never crashes with that monitor connected. However if I select to play over jack (like I want to) the sound crashes again afther a couple of times. Now with an underrun error. (hdmi audio stil working fine)

Is there a project/tutorial/api/driver that can help me getting c++ synthesized audio outputted trough jack?

Output from first couple of times with dvi >hdmi (and good audio)

PortAudio Test: output sine wave. SR = 44100, BufSize = 64
ALSA lib pcm_dsnoop.c:618:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
ALSA lib pcm_dmix.c:961:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
Play for 5 seconds.
Stream Completed: No Message
Test finished.

Output afther trying about 4/5 times hdmi>dvi (no audio)

PortAudio Test: output sine wave. SR = 44100, BufSize = 64
ALSA lib pcm_dsnoop.c:618:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
ALSA lib pcm_dmix.c:961:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1736
Expression 'AlsaOpen( &alsaApi->baseHostApiRep, params, streamDir, &self->pcm )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1904
Expression 'PaAlsaStreamComponent_Initialize( &self->playback, alsaApi, outParams, StreamDirection_Out, NULL != callback )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2175
Expression 'PaAlsaStream_Initialize( stream, alsaHostApi, inputParameters, outputParameters, sampleRate, framesPerBuffer, callback, streamFlags, userData )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2840
Test finished.

On another tread i found: https://devtalk.nvidia.com/default/topic/805132/?comment=4468537

this brought back normal audio, even conected trough hdmi->dvi converter.
And even listed my audio devices in de sound interface (under system settings)

I now have the underrun problem when running the example on both hdmi->hdmi and hdmi->dvi.

play -n synth sin 1000 gain 1

works perfecly fine.

I am starting to suspect the example does something “wrong”.
will read up on underrun and try to fix the example

edit:
With the fix from Rahool and a larger frames per buffer (from 64 to 2048) the example peax_sine_c++.cpp in the portaudio tgz (pa_stable_v19_20130130.tgz) works. And the audio devices are listen even with a hdmi to dvi converter.
Running the example to fast in succession to the previous run, kills the audio. I suspect this has something to do with multiple processes accessing alsa at the same time (Before stream is properly closed). Can someone confirm/deny this?

Steps i took to get audio working most of the time:
Flash JetPackTK1-1.1 complete

sudo apt-get update
sudo apt-get upgrade
sudo reboot

http://jetsonhacks.com/2015/03/10/after-lt4-21-3-flash-setup-nvidia-jetson-tk1/

sudo apt-get install git
git clone -b JetsonTK1-L4T-21.3 https://github.com/jetsonhacks/postFlash.git
cd postFlash
./configureSystem.sh
sudo reboot

backup etc/pulse ./default.pa.orig

sudo gedit etc/pulse ./default.pa.orig
save as default.pa.hdmi
sudo reboot

download portaudio (pa_stable_v19_20130130.tgz)
extract in home/portaudio

sudo apt-get install libasound-dev
cd portaudio
./configure && make
sudo make install

paex_sine_c++.cpp edit frame per buffer to 2048

g++ -o audiotest paex_sine_c++.cpp -lportaudio
sudo apt-get build-dep jackd2
cd/home
apt-get source jackd2
cd jackd2-1.9.9.5+20130622git7de15e7a
sudo dpkg-buildpackage -us -uc

plased patch in jackd2***/linuxhttps://gist.github.com/rogerallen/0346a1812deda2a380da

cd linux
patch -p1 <jackd2.patch
./waf-ligth build
sudo ./waf-light install
sudo ldconfig
 sudo vi /etc/security/limits.conf

added:
@audio - rtprio 99
@audio - memlock 250000
@audio - nice -10

sudo reboot
sudo apt-get sox
PULSE_LATENCY_MSEC=30
export PULSE_LATENCY_MSEC