void generate_tone() { int nsamples; GR_EVENT event; int freq = 0; // struct audio_buf_info info; // char buf[20]; while(!generator_paused) { if(generator_freq != freq) { freq = generator_freq; nsamples = make_wave_buffer(generator_freq); //add wave types here - tbd } // we need to use ioctl to get available space and ensure we dont write too much to improve response // times while changing freq/notes. to be done // ioctl(dspz.dsp, SNDCTL_DSP_GETOSPACE, &info); // sprintf(buf, "f:%d, s/f: %d", info.fragstotal, info.fragsize); // sprintf(buf, "afs: %d, ab: %d", info.fragments, info.bytes); // generator_draw_text(buf); dsp_write( &dspz, wave_buffer, sizeof(short)*nsamples); // dont write block if(GrPeekEvent(&event)) { repeat: GrGetNextEventTimeout(&event, 1000); if (event.type != GR_EVENT_TYPE_TIMEOUT) { pz_event_handler(&event); if (GrPeekEvent(&event)) goto repeat; } } } if (generator_quit_app) // menu button in the middle of a play generator_button_menu();//get out of the app // better way to get out of the event handler - tbd }
int winwave_play_alloc(struct auplay_st **stp, const struct auplay *ap, struct auplay_prm *prm, const char *device, auplay_write_h *wh, void *arg) { struct auplay_st *st; int i, err; if (!stp || !ap || !prm) return EINVAL; st = mem_zalloc(sizeof(*st), auplay_destructor); if (!st) return ENOMEM; st->ap = ap; st->wh = wh; st->arg = arg; err = write_stream_open(st, prm, find_dev(device)); if (err) goto out; /* The write runs at 100ms intervals * prepare enough buffers to suite its needs */ for (i = 0; i < 5; i++) dsp_write(st); out: if (err) mem_deref(st); else *stp = st; return err; }
void dtmf_play( dsp_st *dspz, char key ) { int bp; int f1p = 0; int f2p = 0; int freq1 = F_0; int freq2 = F_0; int ontime = 66; int nsamps = 44100; int ntimes = 1; int c; short * buf; switch( key ) { case( 'c' ): case( 'r' ): case( 'b' ): dtmf_play( dspz, 'S' ); /* trigger 2600hz wink */ break; } /* freq 1 */ switch( key ) { case( '1' ): case( '2' ): case( '3' ): case( 'A' ): freq1 = F_697; break; case( '4' ): case( '5' ): case( '6' ): case( 'B' ): freq1 = F_770; break; case( '7' ): case( '8' ): case( '9' ): case( 'C' ): freq1 = F_852; break; case( '*' ): case( '0' ): case( '#' ): case( 'D' ): freq1 = F_941; break; case( 's' ): case( 'S' ): freq1 = F_2600; break; case( 't' ): freq1 = F_1850; break; case( 'n' ): case( 'd' ): case( 'q' ): freq1 = F_700; break; case( 'c' ): case( 'b' ): freq1 = F_700; break; case( 'r' ): freq1 = F_1100; break; } /* freq 2 */ switch( key ) { case( '1' ): case( '4' ): case( '7' ): case( '*' ): freq2 = F_1209; break; case( '2' ): case( '5' ): case( '8' ): case( '0' ): freq2 = F_1336; break; case( '3' ): case( '6' ): case( '9' ): case( '#' ): freq2 = F_1477; break; case( 'A' ): case( 'B' ): case( 'C' ): case( 'D' ): freq2 = F_1633; break; case( 's' ): case( 'S' ): freq2 = F_0; break; case( 't' ): freq2 = F_0; break; case( 'n' ): case( 'd' ): case( 'q' ): freq2 = F_0; break; case( 'c' ): freq2 = F_1100; break; case( 'r' ): case( 'b' ): freq2 = F_1700; break; } /* adjust timings */ if( key == 'q' ) ontime = 33; if( key == ',' ) ontime = 250; /* pause */ if( key == 'A' || key == 'B' || key == 'C' || key == 'D' ) ontime = 500; if( key == 's' || key == 't' ) ontime = 500; if( key == 'S' ) ontime = 90; if( key == 'c' || key == 'r' || key == 'b' ) ontime = 1000; /* adjust repeats */ if( key == 'q' ) ntimes = 5; if( key == 'd' ) ntimes = 2; nsamps = ontime * 44100 / 1000; buf = (short *)malloc( sizeof( short ) * nsamps ); for( c=0 ; c<ntimes ; c++ ) { // play the two tones for( bp = 0 ; bp < nsamps ; bp++ ) { /* copy over and add the two waveforms */ buf[bp] = (waveforms[freq1].samples[f1p++]) + (waveforms[freq2].samples[f2p++]); buf[bp] = buf[bp] << 6; /* adjust sample pointers */ if( f1p >= waveforms[freq1].nsamples ) f1p = 0; if( f2p >= waveforms[freq2].nsamples ) f2p = 0; } #ifdef __linux__ dsp_write( dspz, buf, nsamps*sizeof(short)); #endif /* zero it out */ for( bp=0 ; bp < nsamps ; bp++ ) buf[bp] = 0; if( key == 'S' ) { nsamps = nsamps * 2 / 3; /* 60ms instead of 90 */ } #ifdef __linux__ dsp_write( dspz, buf, nsamps*sizeof(short)); #endif } free( buf ); }