int main() { int s; while (scanf("%d\n", &s) != EOF) printf("%d\n", (int) cst_ulaw_to_short((int) s)); return 0; }
cst_wave *lpc_resynth(cst_lpcres *lpcres) { cst_wave *w; int i,j,r,o,k; int ci,cr; float *outbuf, *lpccoefs; int pm_size_samps; /* Get a new wave to build the signal into */ w = new_wave(); cst_wave_resize(w,lpcres->num_samples,1); w->sample_rate = lpcres->sample_rate; /* outbuf is a circular buffer with past relevant samples in it */ outbuf = cst_alloc(float,1+lpcres->num_channels); /* unpacked lpc coefficients */ lpccoefs = cst_alloc(float,lpcres->num_channels); for (r=0,o=lpcres->num_channels,i=0; i < lpcres->num_frames; i++) { pm_size_samps = lpcres->sizes[i]; /* Unpack the LPC coefficients */ for (k=0; k<lpcres->num_channels; k++) { lpccoefs[k] = (float)((((double)lpcres->frames[i][k])/65535.0)* lpcres->lpc_range) + lpcres->lpc_min; } /* Note we don't zero the lead in from the previous part */ /* seems like you should but it makes it worse if you do */ /* memset(outbuf,0,sizeof(float)*(1+lpcres->num_channels)); */ /* resynthesis the signal */ for (j=0; j < pm_size_samps; j++,r++) { outbuf[o] = (float)cst_ulaw_to_short(lpcres->residual[r]); cr = (o == 0 ? lpcres->num_channels : o-1); for (ci=0; ci < lpcres->num_channels; ci++) { outbuf[o] += lpccoefs[ci] * outbuf[cr]; cr = (cr == 0 ? lpcres->num_channels : cr-1); } w->samples[r] = (short)(outbuf[o]); o = (o == lpcres->num_channels ? 0 : o+1); } } cst_free(outbuf); cst_free(lpccoefs); return w; }
cst_wave *lpc_resynth_sfp(cst_lpcres *lpcres) { /* The fixed point spike excited, without floats */ cst_wave *w; int i,j,r,o,k; int ci,cr; int *outbuf, *lpccoefs; int pm_size_samps, ilpc_min, ilpc_range; //int pp = 0; /* Get a new wave to build the signal into */ w = new_wave(); cst_wave_resize(w,lpcres->num_samples,1); w->sample_rate = lpcres->sample_rate; /* outbuf is a circular buffer with past relevant samples in it */ outbuf = cst_alloc(int,1+lpcres->num_channels); /* unpacked lpc coefficients */ lpccoefs = cst_alloc(int,lpcres->num_channels); ilpc_min = (int)(lpcres->lpc_min*32768.0); /* assume range is never > abs(16) */ ilpc_range = (int)(lpcres->lpc_range*2048.0); for (r=0,o=lpcres->num_channels,i=0; i < lpcres->num_frames; i++) { pm_size_samps = lpcres->sizes[i]; /* Unpack the LPC coefficients */ for (k=0; k<lpcres->num_channels; k++) lpccoefs[k]=((lpcres->frames[i][k]/2*ilpc_range)/2048+ilpc_min)/2; /* resynthesis the signal */ for (j=0; j < pm_size_samps; j++,r++) { outbuf[o] = (int)cst_ulaw_to_short(lpcres->residual[r]); cr = (o == 0 ? lpcres->num_channels : o-1); for (ci=0; ci < lpcres->num_channels; ci++) { outbuf[o] += (lpccoefs[ci]*outbuf[cr])/16384; cr = (cr == 0 ? lpcres->num_channels : cr-1); } w->samples[r] = (short)outbuf[o]; //pp = outbuf[o]; o = (o == lpcres->num_channels ? 0 : o+1); } } cst_free(outbuf); cst_free(lpccoefs); return w; }
cst_wave *reconstruct_wave(cst_wave *sig, cst_sts *sts, cst_track *lpc) { cst_lpcres *lpcres; int i,j,r; int start; int num_samples; /* FILE *ofd; */ for (num_samples = 0, i=0; i < lpc->num_frames; i++) num_samples += sts[i].size; lpcres = new_lpcres(); lpcres_resize_frames(lpcres,lpc->num_frames); lpcres->num_channels = lpc->num_channels-1; start = (int)((float)sig->sample_rate * lpc->times[0]/2); num_samples += start; for (i=0; i<lpc->num_frames; i++) { lpcres->frames[i] = sts[i].frame; lpcres->sizes[i] = sts[i].size; } lpcres_resize_samples(lpcres,num_samples); lpcres->lpc_min = lpc_min; lpcres->lpc_range = lpc_range; lpcres->sample_rate = sig->sample_rate; for (r=start,i=0; i<lpc->num_frames; i++) for (j=0; j<sts[i].size; j++,r++) lpcres->residual[r] = sts[i].residual[j]; #if 0 /* Debug dump */ ofd = fopen("lpc_resid.lpc","w"); for (s=0,i=0; i<lpcres->num_frames; i++) { fprintf(ofd,"%d %d %d\n",i,0,lpcres->sizes[i]); for (j=0; j < lpcres->num_channels; j++) fprintf(ofd,"%d ",lpcres->frames[i][j]); fprintf(ofd,"\n"); for (j=0; j < lpcres->sizes[i]; j++,s++) fprintf(ofd,"%d ",lpcres->residual[s]); fprintf(ofd,"\n"); } fclose(ofd); ofd = fopen("lpc_resid.res","w"); for (i=0; i < r; i++) fprintf(ofd,"%d\n",cst_ulaw_to_short(lpcres->residual[i])); fclose(ofd); #endif return lpc_resynth(lpcres); }
static int play_wave_from_socket(snd_header *header,int audiostream) { /* Read audio from stream and play it to audio device, converting */ /* it to pcm if required */ int num_samples; int sample_width; cst_audiodev *audio_device; int q,i,n,r; unsigned char bytes[CST_AUDIOBUFFSIZE]; short shorts[CST_AUDIOBUFFSIZE]; cst_file fff; fff = cst_fopen("/tmp/awb.wav",CST_OPEN_WRITE|CST_OPEN_BINARY); if ((audio_device = audio_open(header->sample_rate,1, (header->encoding == CST_SND_SHORT) ? CST_AUDIO_LINEAR16 : CST_AUDIO_LINEAR8)) == NULL) { cst_errmsg("play_wave_from_socket: can't open audio device\n"); return -1; } if (header->encoding == CST_SND_SHORT) sample_width = 2; else sample_width = 1; num_samples = header->data_size / sample_width; /* we naively let the num_channels sort itself out */ for (i=0; i < num_samples; i += r/2) { if (num_samples > i+CST_AUDIOBUFFSIZE) n = CST_AUDIOBUFFSIZE; else n = num_samples-i; if (header->encoding == CST_SND_ULAW) { r = read(audiostream,bytes,n); for (q=0; q<r; q++) shorts[q] = cst_ulaw_to_short(bytes[q]); r *= 2; } else /* if (header->encoding == CST_SND_SHORT) */ { r = read(audiostream,shorts,n*2); if (CST_LITTLE_ENDIAN) for (q=0; q<r/2; q++) shorts[q] = SWAPSHORT(shorts[q]); } if (r <= 0) { /* I'm not getting any data from the server */ audio_close(audio_device); return CST_ERROR_FORMAT; } for (q=r; q > 0; q-=n) { n = audio_write(audio_device,shorts,q); cst_fwrite(fff,shorts,2,q); if (n <= 0) { audio_close(audio_device); return CST_ERROR_FORMAT; } } } audio_close(audio_device); cst_fclose(fff); return CST_OK_FORMAT; }