From db838694d8f5c0a244ee9ae4576a653dede33f49 Mon Sep 17 00:00:00 2001 From: J C Dutton Date: Mon, 2 Jan 2006 00:41:10 +0000 Subject: [PATCH] Summary: Add first attempt at sound capture. Currently display the trace in specjt, but does not seem to decode it. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/trunk@40 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- audio_init.f90 | 6 +- g.py | 3 +- g1 | 6 +- start_threads.c | 419 +++++++++++++++++++++++++++++++++++++++++++++++- wrapkarn.c | 14 ++ wsjt.py | 3 +- 6 files changed, 444 insertions(+), 7 deletions(-) diff --git a/audio_init.f90 b/audio_init.f90 index 5be81d28d..2b1915481 100644 --- a/audio_init.f90 +++ b/audio_init.f90 @@ -58,7 +58,11 @@ subroutine audio_init(ndin,ndout) m3=SetThreadPriority(Thread2,THREAD_PRIORITY_BELOW_NORMAL) m4=ResumeThread(Thread2) #else - call start_threads + print*,'Audio INIT called.' + ierr=start_threads(ndevin,ndevout,y1,y2,nmax,iwrite,iwave,nwave, & + 11025,NSPB,TRPeriod,TxOK,ndebug,Transmitting, & + Tsec,ngo,nmode,tbuf,ibuf,ndsec) + #endif return diff --git a/g.py b/g.py index 1037f1f7e..5bffa113e 100644 --- a/g.py +++ b/g.py @@ -16,7 +16,8 @@ def ftnstr(x): #------------------------------------------------------ filetime def filetime(t): - i=t.rfind(".") +# i=t.rfind(".") + i=6 t=t[:i][-6:] t=t[0:2]+":"+t[2:4]+":"+t[4:6] return t diff --git a/g1 b/g1 index 3f90097b5..38bb0e595 100755 --- a/g1 +++ b/g1 @@ -1 +1,5 @@ -python f2py.py -c --quiet --opt="-O -cpp -DLinux -fno-second-underscore" init_rs.o encode_rs.o decode_rs.o -m Audio --"f77exec=/home/joe/bin/g95" --f90exec="/home/joe/bin/g95" -L//usr/lib/gcc-lib/i386-redhat-linux/3.2.2/ -lpthread -lg2c only: ftn_init ftn_quit audio_init spec getfile azdist0 astro0 : a2d.f90 abc441.f90 astro0.f90 audio_init.f90 azdist0.f90 blanker.f90 decode1.f90 decode2.f90 decode3.f90 ftn_init.f90 ftn_quit.f90 get_fname.f90 getfile.f90 horizspec.f90 hscroll.f90 i1tor4.f90 pix2d.f90 pix2d65.f90 rfile.f90 savedata.f90 spec.f90 wsjtgen.f90 runqqq.f90 wsjt1.f fsubs1.f fsubs.f astro.f astropak.f jtaudio.c ptt_linux.c igray.c wrapkarn.c start_threads.c cutil.c fivehz.f90 +G95=/usr/bin/g95 +COMPILER=//usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/ +python f2py.py -c --quiet --opt="-O -cpp -DLinux -fno-second-underscore" init_rs.o encode_rs.o decode_rs.o -m Audio --f77exec=$G95 --f90exec=$G95 -L$COMPILER -lpthread -lg2c -lasound only: ftn_init ftn_quit audio_init spec getfile azdist0 astro0 : a2d.f90 abc441.f90 astro0.f90 audio_init.f90 azdist0.f90 blanker.f90 decode1.f90 decode2.f90 decode3.f90 ftn_init.f90 ftn_quit.f90 get_fname.f90 getfile.f90 horizspec.f90 hscroll.f90 i1tor4.f90 pix2d.f90 pix2d65.f90 rfile.f90 savedata.f90 spec.f90 wsjtgen.f90 runqqq.f90 wsjt1.f fsubs1.f fsubs.f astro.f astropak.f jtaudio.c ptt_linux.c igray.c wrapkarn.c start_threads.c cutil.c fivehz.f90 + + diff --git a/start_threads.c b/start_threads.c index fbd145fa2..bdc963441 100644 --- a/start_threads.c +++ b/start_threads.c @@ -1,15 +1,428 @@ #include #include #include +#include +#include + +#if 0 +#define ALSA_LOG +#define ALSA_LOG_BUFFERS +#endif +#define BUFFER_TIME 2000*1000 + + +typedef struct alsa_driver_s { + snd_pcm_t *audio_fd; + int capabilities; + int open_mode; + int has_pause_resume; + int is_paused; + int32_t output_sample_rate, input_sample_rate; + double sample_rate_factor; + uint32_t num_channels; + uint32_t bits_per_sample; + uint32_t bytes_per_frame; + uint32_t bytes_in_buffer; /* number of bytes writen to audio hardware */ + int16_t *app_buffer_y1; + int16_t *app_buffer_y2; + int *app_buffer_offset; + int app_buffer_length; + snd_pcm_uframes_t buffer_size; + snd_pcm_uframes_t period_size; + int32_t mmap; +} alsa_driver_t; + +alsa_driver_t alsa_driver_playback; +alsa_driver_t alsa_driver_capture; +void *alsa_buffers[2]; + +static snd_output_t *jcd_out; + +/* + * open the audio device for writing to + */ +static int ao_alsa_open(alsa_driver_t *this_gen, int32_t *input_rate, snd_pcm_stream_t direction ) { + alsa_driver_t *this = (alsa_driver_t *) this_gen; + char *pcm_device; + snd_pcm_hw_params_t *params; + snd_pcm_sw_params_t *swparams; + snd_pcm_access_mask_t *mask; + snd_pcm_uframes_t period_size_min; + snd_pcm_uframes_t period_size_max; + snd_pcm_uframes_t buffer_size_min; + snd_pcm_uframes_t buffer_size_max; + snd_pcm_format_t format; + uint32_t buffer_time=BUFFER_TIME; + snd_pcm_uframes_t buffer_time_to_size; + int err, dir; + int open_mode=1; /* NONBLOCK */ + /* int open_mode=0; BLOCK */ + int32_t rate=*input_rate; + this->input_sample_rate=*input_rate; + + snd_pcm_hw_params_alloca(¶ms); + snd_pcm_sw_params_alloca(&swparams); + err = snd_output_stdio_attach(&jcd_out, stdout, 0); + + this->num_channels = 2; + pcm_device="default"; +#ifdef ALSA_LOG + printf("audio_alsa_out: Audio Device name = %s\n",pcm_device); + printf("audio_alsa_out: Number of channels = %d\n",this->num_channels); +#endif + + if (this->audio_fd) { + printf("audio_alsa_out:Already open...WHY!"); + snd_pcm_close (this->audio_fd); + this->audio_fd = NULL; + } + + this->bytes_in_buffer = 0; + /* + * open audio device + */ + err=snd_pcm_open(&this->audio_fd, pcm_device, direction, open_mode); + if(err <0 ) { + printf ("audio_alsa_out: snd_pcm_open() of %s failed: %s\n", pcm_device, snd_strerror(err)); + printf ("audio_alsa_out: >>> check if another program already uses PCM <<<\n"); + return 0; + } + /* printf ("audio_alsa_out: snd_pcm_open() opened %s\n", pcm_device); */ + /* We wanted non blocking open but now put it back to normal */ + //snd_pcm_nonblock(this->audio_fd, 0); + snd_pcm_nonblock(this->audio_fd, 1); + /* + * configure audio device + */ + err = snd_pcm_hw_params_any(this->audio_fd, params); + if (err < 0) { + printf ("audio_alsa_out: broken configuration for this PCM: no configurations available: %s\n"), + snd_strerror(err); + goto close; + } + /* set interleaved access */ + if (this->mmap != 0) { + mask = alloca(snd_pcm_access_mask_sizeof()); + snd_pcm_access_mask_none(mask); + snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_INTERLEAVED); + snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED); + snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_COMPLEX); + err = snd_pcm_hw_params_set_access_mask(this->audio_fd, params, mask); + if (err < 0) { + printf ( "audio_alsa_out: mmap not availiable, falling back to compatiblity mode\n"); + this->mmap=0; + err = snd_pcm_hw_params_set_access(this->audio_fd, params, + SND_PCM_ACCESS_RW_NONINTERLEAVED); + } + } else { + err = snd_pcm_hw_params_set_access(this->audio_fd, params, + SND_PCM_ACCESS_RW_NONINTERLEAVED); + } + + if (err < 0) { + printf ( "audio_alsa_out: access type not available: %s\n", snd_strerror(err)); + goto close; + } + /* set the sample format S16 */ + /* ALSA automatically appends _LE or _BE depending on the CPU */ + format = SND_PCM_FORMAT_S16; + err = snd_pcm_hw_params_set_format(this->audio_fd, params, format ); + if (err < 0) { + printf ( "audio_alsa_out: sample format non available: %s\n", snd_strerror(err)); + goto close; + } + /* set the number of channels */ + err = snd_pcm_hw_params_set_channels(this->audio_fd, params, this->num_channels); + if (err < 0) { + printf ( "audio_alsa_out: Cannot set number of channels to %d (err=%d:%s)\n", + this->num_channels, err, snd_strerror(err)); + goto close; + } +#if SND_LIB_VERSION >= 0x010009 + /* Restrict a configuration space to contain only real hardware rates */ + err = snd_pcm_hw_params_set_rate_resample(this->audio_fd, params, 0); +#endif + /* set the stream rate [Hz] */ + dir=0; + err = snd_pcm_hw_params_set_rate_near(this->audio_fd, params, &rate, &dir); + if (err < 0) { + printf ( "audio_alsa_out: rate not available: %s\n", snd_strerror(err)); + goto close; + } + this->output_sample_rate = (uint32_t)rate; + if (this->input_sample_rate != this->output_sample_rate) { + printf ( "audio_alsa_out: audio rate : %d requested, %d provided by device/sec\n", + this->input_sample_rate, this->output_sample_rate); + } + buffer_time_to_size = ( (uint64_t)buffer_time * rate) / 1000000; + err = snd_pcm_hw_params_get_buffer_size_min(params, &buffer_size_min); + err = snd_pcm_hw_params_get_buffer_size_max(params, &buffer_size_max); + dir=0; + err = snd_pcm_hw_params_get_period_size_min(params, &period_size_min,&dir); + dir=0; + err = snd_pcm_hw_params_get_period_size_max(params, &period_size_max,&dir); +#ifdef ALSA_LOG_BUFFERS + printf("Buffer size range from %lu to %lu\n",buffer_size_min, buffer_size_max); + printf("Period size range from %lu to %lu\n",period_size_min, period_size_max); + printf("Buffer time size %lu\n",buffer_time_to_size); +#endif + this->buffer_size = buffer_time_to_size; + if (buffer_size_max < this->buffer_size) this->buffer_size = buffer_size_max; + if (buffer_size_min > this->buffer_size) this->buffer_size = buffer_size_min; + this->period_size=this->buffer_size/8; + this->buffer_size = this->period_size*8; +#ifdef ALSA_LOG_BUFFERS + printf("To choose buffer_size = %ld\n",this->buffer_size); + printf("To choose period_size = %ld\n",this->period_size); +#endif + +#if 0 + /* Set period to buffer size ratios at 8 periods to 1 buffer */ + dir=-1; + periods=8; + err = snd_pcm_hw_params_set_periods_near(this->audio_fd, params, &periods ,&dir); + if (err < 0) { + xprintf (this->class->xine, XINE_VERBOSITY_DEBUG, + "audio_alsa_out: unable to set any periods: %s\n", snd_strerror(err)); + goto close; + } + /* set the ring-buffer time [us] (large enough for x us|y samples ...) */ + dir=0; + err = snd_pcm_hw_params_set_buffer_time_near(this->audio_fd, params, &buffer_time, &dir); + if (err < 0) { + xprintf (this->class->xine, XINE_VERBOSITY_DEBUG, + "audio_alsa_out: buffer time not available: %s\n", snd_strerror(err)); + goto close; + } +#endif +#if 1 + /* set the period time [us] (interrupt every x us|y samples ...) */ + dir=0; + err = snd_pcm_hw_params_set_period_size_near(this->audio_fd, params, &(this->period_size), &dir); + if (err < 0) { + printf ( "audio_alsa_out: period time not available: %s\n", snd_strerror(err)); + goto close; + } +#endif + dir=0; + err = snd_pcm_hw_params_get_period_size(params, &(this->period_size), &dir); + + dir=0; + err = snd_pcm_hw_params_set_buffer_size_near(this->audio_fd, params, &(this->buffer_size)); + if (err < 0) { + printf ( "audio_alsa_out: buffer time not available: %s\n", snd_strerror(err)); + goto close; + } + err = snd_pcm_hw_params_get_buffer_size(params, &(this->buffer_size)); +#ifdef ALSA_LOG_BUFFERS + printf("was set period_size = %ld\n",this->period_size); + printf("was set buffer_size = %ld\n",this->buffer_size); +#endif + if (2*this->period_size > this->buffer_size) { + printf ( "audio_alsa_out: buffer to small, could not use\n"); + goto close; + } + + /* write the parameters to device */ + err = snd_pcm_hw_params(this->audio_fd, params); + if (err < 0) { + printf ( "audio_alsa_out: pcm hw_params failed: %s\n", snd_strerror(err)); + goto close; + } + /* Check for pause/resume support */ + this->has_pause_resume = ( snd_pcm_hw_params_can_pause (params) + && snd_pcm_hw_params_can_resume (params) ); + printf( "audio_alsa_out:open pause_resume=%d\n", this->has_pause_resume); + this->sample_rate_factor = (double) this->output_sample_rate / (double) this->input_sample_rate; + this->bytes_per_frame = snd_pcm_frames_to_bytes (this->audio_fd, 1); + /* + * audio buffer size handling + */ + /* Copy current parameters into swparams */ + err = snd_pcm_sw_params_current(this->audio_fd, swparams); + if (err < 0) { + printf ( "audio_alsa_out: Unable to determine current swparams: %s\n", snd_strerror(err)); + goto close; + } + /* align all transfers to 1 sample */ + err = snd_pcm_sw_params_set_xfer_align(this->audio_fd, swparams, 1); + if (err < 0) { + printf ( "audio_alsa_out: Unable to set transfer alignment: %s\n", snd_strerror(err)); + goto close; + } + /* allow the transfer when at least period_size samples can be processed */ + err = snd_pcm_sw_params_set_avail_min(this->audio_fd, swparams, this->period_size); + if (err < 0) { + printf ( "audio_alsa_out: Unable to set available min: %s\n", snd_strerror(err)); + goto close; + } + if (direction == SND_PCM_STREAM_PLAYBACK) { + /* start the transfer when the buffer contains at least period_size samples */ + err = snd_pcm_sw_params_set_start_threshold(this->audio_fd, swparams, 0); + } else { + err = snd_pcm_sw_params_set_start_threshold(this->audio_fd, swparams, -1); + } + if (err < 0) { + printf ( "audio_alsa_out: Unable to set start threshold: %s\n", snd_strerror(err)); + goto close; + } + + if (direction == SND_PCM_STREAM_PLAYBACK) { + /* never stop the transfer, even on xruns */ + err = snd_pcm_sw_params_set_stop_threshold(this->audio_fd, swparams, 0); + } else { + err = snd_pcm_sw_params_set_stop_threshold(this->audio_fd, swparams, this->buffer_size); + } + if (err < 0) { + printf ( "audio_alsa_out: Unable to set stop threshold: %s\n", snd_strerror(err)); + goto close; + } + + /* Install swparams into current parameters */ + err = snd_pcm_sw_params(this->audio_fd, swparams); + if (err < 0) { + printf ( "audio_alsa_out: Unable to set swparams: %s\n", snd_strerror(err)); + goto close; + } +#ifdef ALSA_LOG + snd_pcm_dump_setup(this->audio_fd, jcd_out); + snd_pcm_sw_params_dump(swparams, jcd_out); +#endif + + return this->output_sample_rate; + +close: + snd_pcm_close (this->audio_fd); + this->audio_fd=NULL; + return 0; +} + +int playback_callback(alsa_driver_t *alsa_driver_playback) { + alsa_driver_t *this = alsa_driver_playback; + printf("playback callback\n"); + //snd_pcm_writen(this->audio_fd, alsa_buffers, this->period_size); +} + +int capture_callback(alsa_driver_t *alsa_driver_capture) { + alsa_driver_t *this = alsa_driver_capture; + int result; +#ifdef ALSA_LOG + printf("capture callback %d samples\n", this->period_size); +#endif + snd_pcm_status_t *pcm_stat; + snd_pcm_status_alloca(&pcm_stat); +#ifdef ALSA_LOG + snd_pcm_status(this->audio_fd, pcm_stat); + snd_pcm_status_dump(pcm_stat, jcd_out); +#endif + alsa_buffers[0]=this->app_buffer_y1 + *(this->app_buffer_offset); + alsa_buffers[1]=this->app_buffer_y2 + *(this->app_buffer_offset); + result = snd_pcm_readn(this->audio_fd, alsa_buffers, this->period_size); + *(this->app_buffer_offset) += this->period_size; + if ( *this->app_buffer_offset >= this->app_buffer_length ) + this->app_buffer_length=0; /* FIXME: implement proper wrapping */ +#ifdef ALSA_LOG + printf("result=%d\n",result); + snd_pcm_status(this->audio_fd, pcm_stat); + snd_pcm_status_dump(pcm_stat, jcd_out); +#endif +} + +int capture_xrun(alsa_driver_t *alsa_driver_capture) { + alsa_driver_t *this = alsa_driver_capture; + snd_pcm_status_t *pcm_stat; + snd_pcm_status_alloca(&pcm_stat); + printf("capture xrun\n"); + snd_pcm_status(this->audio_fd, pcm_stat); + snd_pcm_status_dump(pcm_stat, jcd_out); +} + +void ao_alsa_loop(void *iarg) { + int playback_nfds; + int capture_nfds; + struct pollfd *pfd; + int nfds; + int capture_index; + unsigned short playback_revents; + unsigned short capture_revents; + playback_nfds = snd_pcm_poll_descriptors_count ( + alsa_driver_playback.audio_fd); + capture_nfds = snd_pcm_poll_descriptors_count ( + alsa_driver_capture.audio_fd); + pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * + (playback_nfds + capture_nfds)); + + nfds=0; +#if 0 + snd_pcm_poll_descriptors (alsa_driver_playback.audio_fd, + &pfd[0], + playback_nfds); + nfds += playback_nfds; +#endif + snd_pcm_poll_descriptors (alsa_driver_capture.audio_fd, + &pfd[nfds], + capture_nfds); + capture_index = nfds; + nfds += capture_nfds; + while(1) { + if (poll (pfd, nfds, 100000) < 0) { + printf("poll failed\n"); + return; + } + //snd_pcm_poll_descriptors_revents(alsa_driver_playback.audio_fd, &pfd[0], playback_nfds, &playback_revents); + snd_pcm_poll_descriptors_revents(alsa_driver_capture.audio_fd, &pfd[capture_index], capture_nfds, &capture_revents); + //if ((playback_revents & POLLERR) || ((capture_revents) & POLLERR)) { + if (((capture_revents) & POLLERR)) { + printf("pollerr\n"); + capture_xrun(&alsa_driver_capture); + return; + } +#if 0 + if (playback_revents & POLLOUT) { + playback_callback(&alsa_driver_playback); + } +#endif + if (capture_revents & POLLIN) { + capture_callback(&alsa_driver_capture); + } + } + + return; +} + extern void decode1_(int *iarg); -void start_threads_(void) +int start_threads_(int *ndevin, int *ndevout, short y1[], short y2[], + int *nbuflen, int *iwrite, short iwave[], + int *nwave, int *nfsample, int *nsamperbuf, + int *TRPeriod, int *TxOK, int *ndebug, + int *Transmitting, double *Tsec, int *ngo, int *nmode, + double tbuf[], int *ibuf, int *ndsec) { pthread_t thread1,thread2; int iret1,iret2; int iarg1=1,iarg2=2; + //int32_t rate=11025; + int32_t rate=*nfsample; + alsa_driver_capture.app_buffer_y1=y1; + alsa_driver_capture.app_buffer_y2=y2; + alsa_driver_capture.app_buffer_offset=iwrite; + alsa_driver_capture.app_buffer_length=nsamperbuf; - // iret1 = pthread_create(&thread1,NULL,a2d_,&iarg1); - iret2 = pthread_create(&thread2,NULL,decode1_,&iarg2); + printf("start threads called\n"); + iret1 = pthread_create(&thread1,NULL,decode1_,&iarg1); +/* Open audio card. */ + ao_alsa_open(&alsa_driver_playback, &rate, SND_PCM_STREAM_PLAYBACK); + ao_alsa_open(&alsa_driver_capture, &rate, SND_PCM_STREAM_CAPTURE); + +/* + * Start audio io thread + */ + iret2 = pthread_create(&thread2, NULL, ao_alsa_loop, NULL); + snd_pcm_prepare(alsa_driver_capture.audio_fd); + snd_pcm_start(alsa_driver_capture.audio_fd); + + /* snd_pcm_start */ + //iret2 = pthread_create(&thread2,NULL,a2d_,&iarg2); } diff --git a/wrapkarn.c b/wrapkarn.c index 349a5f7c8..636a95521 100644 --- a/wrapkarn.c +++ b/wrapkarn.c @@ -67,3 +67,17 @@ void rs_decode_(int *recd0, int *era0, int *numera0, int *decoded, int *nerr) *nerr=decode_rs_int(rs,recd,era_pos,numera); for(i=0; i<12; i++) decoded[i]=recd[11-i]; } + + +#ifndef WIN32 +void rs_encode__(int *dgen, int *sent) +{ + rs_encode_(dgen, sent); +} + +void rs_decode__(int *recd0, int *era0, int *numera0, int *decoded, int *nerr) +{ + rs_decode_(recd0, era0, numera0, decoded, nerr); +} +#endif + diff --git a/wsjt.py b/wsjt.py index e95a49d17..0179b23af 100644 --- a/wsjt.py +++ b/wsjt.py @@ -1239,7 +1239,8 @@ def update(): msg4.configure(text=t,bg='red') t=g.ftnstr(Audio.gcom2.decodedfile) - i=t.rfind(".") + i=0 +# i=t.rfind(".") t=t[:i] lab3.configure(text=t) if mode.get() != g.mode or first: