diff -u receiver/audio.c /home/alex/Desktop/receiver/audio.c
--- receiver/audio.c	2011-03-27 07:10:57.334740734 +0800
+++ /home/alex/Desktop/receiver/audio.c	2010-12-01 17:11:47.004065090 +0800
@@ -70,11 +70,13 @@
         SetANF(0,0,1);
         //sprintf(c,"setNR %d",NR);
         //writeCommand(c);
+	SetNRvals(0,0,128,50,1.0,1.0);
         SetNR(0,0,NR);
     } else if(widget==buttonANF) {
         ANF=!ANF;
         state=ANF;
         //writeCommand("setBlkANF 1");
+	SetANFvals(0,0,128,50,0.1,0.0);
         SetANF(0,0,1);
         //sprintf(c,"setANF %d",ANF);
         //writeCommand(c);
diff -u receiver/local_audio.c /home/alex/Desktop/receiver/local_audio.c
--- receiver/local_audio.c	2011-03-27 07:10:57.390741035 +0800
+++ /home/alex/Desktop/receiver/local_audio.c	2010-08-30 23:49:50.929568002 +0800
@@ -1,94 +1,186 @@
-#include <unistd.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <linux/soundcard.h>
+#include <semaphore.h>
+#include <pthread.h>
+#include <errno.h>
+#include <poll.h>
+#include <alsa/asoundlib.h>
 #include <semaphore.h>
 
-#define RATE 48000   /* the sampling rate */
-#define SIZE 16      /* sample size: 8 or 16 bits or 24 bits */
-#define CHANNELS 2  /* 1 = mono 2 = stereo */
 
-static int fd;
+static sem_t audio_sem;
+static pthread_t *write_local_audio_thread_id;
+void write_local_audio_thread(void* arg);
+	      
+snd_pcm_t *playback_handle;
 
-static char local_audio_device[32];
-static int sample_rate=48000;
+#define CHANNELS 2
+#define SAMPLE_RATE 48000
+#define SAMPLE_PER_BUFFER 2048
+#define AUDIO_BUFFER_SIZE (SAMPLE_PER_BUFFER * sizeof(short)*CHANNELS*2)
+#define ALSA_BUF_SIZE 2048
+#define ALSA_PERIOD_SIZE 128
 
+static char local_audio_device[32];
 static int insert=0;
-static unsigned char audio_buffer[1024*sizeof(short)*2*2];
-static int write_local_audio_thread_id;
+static int out_index = 0;
+static unsigned char audio_buffer[AUDIO_BUFFER_SIZE];
+static unsigned char buf[ALSA_PERIOD_SIZE*CHANNELS*sizeof(short)];
+// static snd_async_handler_t *pcm_callback;
+static snd_pcm_uframes_t buffer_size = ALSA_BUF_SIZE;
+static snd_pcm_uframes_t period_size = ALSA_PERIOD_SIZE;
+static snd_pcm_sframes_t avail;
+static unsigned int sample_rate = SAMPLE_RATE;
+
 
-static sem_t audio_sem;
 
-void write_local_audio_thread(void* arg);
 
 void set_local_audio_device(char * device) {
     strcpy(local_audio_device,device);
 }
 
 int open_local_audio() {
-    int arg;
-    int status;
-    int rc;
-
-fprintf(stderr,"open_local_audio\n");
-
-    /* open sound device */
-    fd = open(local_audio_device, O_RDWR);
-    if (fd < 0) {
-        perror("open of local audio device failed");
-        exit(1);
-    }
 
-    /* set sampling parameters */
-    arg = SIZE;      /* sample size */
-    status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
-    if (status == -1)
-        perror("SOUND_PCM_WRITE_BITS ioctl failed");
-    if (arg != SIZE)
-        perror("unable to set sample size");
-
-    arg = CHANNELS;  /* mono or stereo */
-    status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
-    if (status == -1)
-        perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
-    if (arg != CHANNELS)
-        perror("unable to set number of channels");
-
-    arg = sample_rate;      /* sampling rate */
-    status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
-    if (status == -1)
-        perror("SOUND_PCM_WRITE_WRITE ioctl failed");
-
-    arg = AFMT_S16_BE;       /* signed big endian */
-    status = ioctl(fd, SOUND_PCM_SETFMT, &arg);
-    if (status == -1)
-        perror("SOUND_PCM_SETFMTS ioctl failed");
-
-    arg=(8<<16)|(14);
-    status=ioctl(fd,SNDCTL_DSP_SETFRAGMENT,&arg); 
-    if (status == -1)
-        perror("SNDCTL_DSP_SETFRAGMENT ioctl failed");
-
-    sem_init (&audio_sem, 0, 0);
-    rc=pthread_create(&write_local_audio_thread_id,NULL,write_local_audio_thread,NULL);
-    if(rc!=0) {
-        fprintf(stderr,"pthread_create failed on write_local_audio_thread: rc=%d\n",rc);
-    }
+	fprintf(stderr,"open_local_audio: alsa\n");
+
+    	/* open sound device */
+	
+	snd_pcm_hw_params_t *hw_params;
+	snd_pcm_sw_params_t *sw_params;
+
+	int err;
+	int rc;
+
+	if ((err = snd_pcm_open (&playback_handle, local_audio_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
+		fprintf (stderr, "cannot open audio device %s (%s)\n", 
+			 local_audio_device,
+			 snd_strerror (err));
+		exit (1);
+	}
+	   
+	if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
+		fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+			 
+	if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
+		fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+	if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
+		fprintf (stderr, "cannot set access type (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+	if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_BE)) < 0) {
+		fprintf (stderr, "cannot set sample format (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+	if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &sample_rate, 0)) < 0) {
+		fprintf (stderr, "cannot set sample rate (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+	if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, CHANNELS)) < 0) {
+		fprintf (stderr, "cannot set channel count (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+	if ((err = snd_pcm_hw_params_set_buffer_size_near (playback_handle, hw_params, &buffer_size)) < 0) {
+		fprintf (stderr, "cannot set buffer size (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+
+	if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
+		fprintf (stderr, "cannot set parameters (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+	snd_pcm_hw_params_free (hw_params);
+
+	/* tell ALSA to wake us up whenever SAMPLES_PER_FRAME or more frames
+	   of playback data can be delivered. Also, tell
+	   ALSA that we'll start the device ourselves.
+	*/
+
+	if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
+		fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+	if ((err = snd_pcm_sw_params_current (playback_handle, sw_params)) < 0) {
+		fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+	if ((err = snd_pcm_sw_params_set_avail_min (playback_handle, sw_params, buffer_size)) < 0) {
+		fprintf (stderr, "cannot set minimum available count (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+	if ((err = snd_pcm_sw_params_set_start_threshold (playback_handle, sw_params, 0U)) < 0) {
+		fprintf (stderr, "cannot set start mode (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+	if ((err = snd_pcm_sw_params (playback_handle, sw_params)) < 0) {
+		fprintf (stderr, "cannot set software parameters (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+
+	if ((err = snd_pcm_prepare (playback_handle)) < 0) {
+		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
+			 snd_strerror (err));
+		exit (1);
+	}
+
+// write something first to prevent xrun
+
+	if ((err = snd_pcm_writei (playback_handle, buf, 2*period_size)) < 0) {
+		fprintf (stderr, "write failed (%s)\n", snd_strerror (err));
+	        exit (1);
+	}
+
+/*
+	if ((err = snd_async_add_pcm_handler(&pcm_callback, playback_handle, playback_callback, NULL)) < 0) {
+		fprintf (stderr, "add pcm handler failed (%s)\n", snd_strerror (err));
+		exit (1);
+	}
+*/	
+
+	fprintf(stderr, "open_local_audio: alsa .... success!!!\n");
+
+	sem_init (&audio_sem, 0, 0);
+	rc=pthread_create(&write_local_audio_thread_id,NULL, write_local_audio_thread,NULL);
+	if(rc!=0) {
+		fprintf(stderr,"pthread_create failed on write_local_audio_thread: rc=%d\n",rc);
+	    }
+
 
     return 0;
 
 }
 
 int close_local_audio() {
-    close(fd);
+//	snd_async_del_handler (pcm_callback);
+	snd_pcm_close (playback_handle);
+	return 0;
 }
 
 int write_local_audio(float* left_samples,float* right_samples,int samples,int increment) {
-    int rc;
-    int bytes;
     int i;
     short sample;
 
@@ -100,33 +192,29 @@
         audio_buffer[insert++]=sample>>8;
         audio_buffer[insert++]=sample;
     }
-
-    if(insert==sizeof(audio_buffer)/2) {
-        sem_post(&audio_sem);
-    } else if(insert==sizeof(audio_buffer)) {
-        insert=0;
-        sem_post(&audio_sem);
-    }
+    sem_post(&audio_sem);
+    if (insert >= AUDIO_BUFFER_SIZE) insert = 0;
 
     return 0;
 }
 
 void write_local_audio_thread(void* arg) {
-    int rc;
+    int i;
 
     while(1) {
-        sem_wait(&audio_sem);
-        rc=write(fd,audio_buffer,sizeof(audio_buffer)/2);
-        if(rc!=sizeof(audio_buffer)/2) {
-            perror("writing first audio_buffer");
-        }
-
-        sem_wait(&audio_sem);
-        rc=write(fd,&audio_buffer[sizeof(audio_buffer)/2],sizeof(audio_buffer)/2);
-        if(rc!=sizeof(audio_buffer)/2) {
-            perror("writing second audio_buffer");
-        }
-    }
-
+        	sem_wait(&audio_sem);
+		avail = snd_pcm_avail_update(playback_handle);
+ 		while (avail >= period_size) {
+                        for (i = 0; i < (ALSA_PERIOD_SIZE * CHANNELS * sizeof(short) ); i++){
+				buf[i] = audio_buffer[out_index++];
+				if (out_index >= AUDIO_BUFFER_SIZE) out_index = 0;
+			}
+     			snd_pcm_writei(playback_handle, buf, period_size);
+    			avail = snd_pcm_avail_update(playback_handle);
+     		}
+	}
 }
 
+
+
+
diff -u receiver/Makefile /home/alex/Desktop/receiver/Makefile
--- receiver/Makefile	2011-03-27 07:10:57.362740888 +0800
+++ /home/alex/Desktop/receiver/Makefile	2010-12-03 23:15:28.077034900 +0800
@@ -3,7 +3,7 @@
 #
 CC=gcc 
 LINK=gcc
-OPTIONS=-g -O3 -msse -msse3 -ffast-math -march=core2 -Wall
+OPTIONS=-g -O3 -msse -msse3 -ffast-math -Wall
 INCLUDES=-I. -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include\
          -I/usr/include/atk-1.0 -I/usr/include/cairo\
          -I/usr/include/pango-1.0 -I/usr/include/glib-2.0\

diff -u receiver/ozy.c /home/alex/Desktop/receiver/ozy.c
--- receiver/ozy.c	2011-03-27 07:10:57.394741035 +0800
+++ /home/alex/Desktop/receiver/ozy.c	2010-09-07 22:57:55.285109000 +0800
@@ -37,6 +37,7 @@
  *   ozy interface
  */
 
+
 #define SMALL_PACKETS
 
 #define USB_TIMEOUT -7
@@ -373,7 +374,7 @@
     int result;
 
     result=0;
-    sprintf(command,"frequency %lld",ddsAFrequency);
+    sprintf(command,"frequency %lld", (ddsAFrequency - LO_OFFSET));
     send_command(command);
     token=strtok(response," ");
     if(token!=NULL) {
@@ -424,7 +425,7 @@
         sampleRate=48000;
         output_sample_increment=1;
         SetSampleRate((double)sampleRate);
-        SetRXOsc(0,0,0.0);
+        SetRXOsc(0,1, LO_OFFSET);			// tweaked by Alex Lee 18 Aug 2010
         setFilter(filter);
         {
             int *m=malloc(sizeof(int));
@@ -436,7 +437,7 @@
         sampleRate=96000;
         output_sample_increment=2;
         SetSampleRate((double)sampleRate);
-        SetRXOsc(0,0,0.0);
+        SetRXOsc(0,1, LO_OFFSET);			// tweaked by Alex Lee 18 Aug 2010
         setFilter(filter);
         {
             int *m=malloc(sizeof(int));
@@ -448,7 +449,7 @@
         sampleRate=192000;
         output_sample_increment=4;
         SetSampleRate((double)sampleRate);
-        SetRXOsc(0,0,0.0);
+        SetRXOsc(0,1, LO_OFFSET);			// tweaked by Alex Lee 18 Aug 2010
         setFilter(filter);
         {
             int *m=malloc(sizeof(int));
diff -u receiver/ozy.h /home/alex/Desktop/receiver/ozy.h
--- receiver/ozy.h	2011-03-27 07:10:57.494741538 +0800
+++ /home/alex/Desktop/receiver/ozy.h	2010-09-07 22:58:12.761109001 +0800
@@ -32,6 +32,9 @@
 extern "C" {
 #endif
 
+// Added by Alex Lee 18 Aug 2010
+#define LO_OFFSET 9000
+
 #define SYNC 0x7F
 #define OZY_BUFFER_SIZE 512
 
@@ -93,6 +96,7 @@
 
 char server_address[64];
 
+
 /* --------------------------------------------------------------------------*/
 /** 
 * @brief Initialize the Ozy interface 
diff -u receiver/port_audio.c /home/alex/Desktop/receiver/port_audio.c
--- receiver/port_audio.c	2011-03-27 07:10:57.410741141 +0800
+++ /home/alex/Desktop/receiver/port_audio.c	2010-08-15 12:14:33.025035002 +0800
@@ -9,31 +9,83 @@
 #define CHANNELS 2
 #define SAMPLE_RATE 48000
 #define SAMPLES_PER_BUFFER 2048
+#define AUDIO_BUFFER_SIZE (SAMPLES_PER_BUFFER*CHANNELS*2)
 
 static PaStream *stream;
 
 static int insert=0;
-static float audio_buffer[SAMPLES_PER_BUFFER*CHANNELS*2];
+static int out_index = 0;
+static float audio_buffer[AUDIO_BUFFER_SIZE];
 static sem_t audio_sem;
 static int audio_device=0;
 
 static int write_port_audio_thread_id;
 
+typedef struct {
+       float left_phase;
+       float right_phase;
+     } audio_out_data_t;
+
+static audio_out_data_t data;
+
+/* This routine will be called by the PortAudio engine when audio is needed.
+** It may called at interrupt level on some machines so don't do anything
+** that could mess up the system like calling malloc() or free().
+*/ 
+static int write_callback( const void *inputBuffer, void *outputBuffer,
+                           unsigned long framesPerBuffer,
+                           const PaStreamCallbackTimeInfo* timeInfo,
+                           PaStreamCallbackFlags statusFlags,
+                           void *userData )
+{
+    /* Cast data passed through stream to our structure. */
+    audio_out_data_t *data = (audio_out_data_t*)userData; 
+    float *out = (float*)outputBuffer;
+    unsigned int i;
+    (void) inputBuffer; /* Prevent unused variable warning. */
+    
+    for( i=0; i<framesPerBuffer; i++ )
+    {
+/*      
+        // test code to generate saw tooth sound locally instead of reading from audio_buffer
+   
+        *out++ = data->left_phase;  //left
+        *out++ = data->right_phase; //right
+        //Generate simple sawtooth phaser that ranges between -1.0 and 1.0.
+        data->left_phase += 0.01f;
+        // When signal reaches top, drop back down.
+        if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
+        // higher pitch so we can distinguish left and right.
+        data->right_phase += 0.03f;
+        if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
+*/
+        *out++ = audio_buffer[out_index++];
+        *out++ = audio_buffer[out_index++];
+        if (out_index >= AUDIO_BUFFER_SIZE) out_index = 0;
+
+    }
+    return 0;
+}
+
 static void write_port_audio_thread(void* arg) {
     int rc;
 
     while(1) {
         sem_wait(&audio_sem);
+
+/*
         rc=Pa_WriteStream(stream,audio_buffer,SAMPLES_PER_BUFFER);
         if(rc!=0) {
             fprintf(stderr,"error writing first audio_buffer %s (rc=%d)\n",Pa_GetErrorText(rc),rc);
         }
-
+*/
         sem_wait(&audio_sem);
+/*
         rc=Pa_WriteStream(stream,&audio_buffer[SAMPLES_PER_BUFFER],SAMPLES_PER_BUFFER);
         if(rc!=0) {
             fprintf(stderr,"error writing second audio_buffer %s (rc=%d)\n",Pa_GetErrorText(rc),rc);
         }
+*/
     }
 
 }
@@ -76,7 +128,7 @@
     outputParameters.suggestedLatency=Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
     outputParameters.hostApiSpecificStreamInfo=NULL;
 
-    rc=Pa_OpenStream(&stream,NULL,&outputParameters,SAMPLE_RATE,SAMPLES_PER_BUFFER,paNoFlag,NULL,NULL);
+    rc=Pa_OpenStream(&stream,NULL,&outputParameters,SAMPLE_RATE,SAMPLES_PER_BUFFER,paNoFlag, write_callback,&data);
     if(rc!=paNoError) {
         fprintf(stderr,"Pa_OpenStream failed: %s\n",Pa_GetErrorText(rc));
         exit(1);
diff -u receiver/subrx.c /home/alex/Desktop/receiver/subrx.c
--- receiver/subrx.c	2011-03-27 07:10:57.366740912 +0800
+++ /home/alex/Desktop/receiver/subrx.c	2010-09-07 23:12:14.617109000 +0800
@@ -377,7 +377,7 @@
         subrxFrequencyDds=f-subrxFrequencyLO;
         updateSubrxDisplay();
         diff=frequencyA-subrxFrequency;
-        SetRXOsc(0,1,(double)diff);
+        SetRXOsc(0,1,(double)diff-LO_OFFSET);
     }
 }

