/* dialog_example.cpp * * Dan Spoonhower * /* std includes */ #include #include #include #include #include #include #include #include /* Dialogic includes */ #include #include #include /* Constants */ #define NUMIOTT 180 #define NUMBUFFERS 180 #define BUFFERSIZE 512 /* Event type for our custom event */ #define VCHAN_RECORD 0x90 typedef DX_IOTT* PDX_IOTT; /* A few globals */ int buffer_count; int current_pointer; int chdev; DV_TPT* rec_tpt; CHAR* play_buffer; /* Function prototypes */ int getEvent(); int my_write( int, char*, unsigned); int my_read( int, char*, unsigned); long my_seek( int, long, int); /* Main */ void main( char** argv, int argc) { PDX_IOTT rec_iott, play_iott; DX_EBLK eblk; DX_UIO uio; /* Initialize the UIO (user-defined IO) structure */ uio = *(new DX_UIO); uio.u_write = my_write; uio.u_read = my_read; uio.u_seek = my_seek; dx_setuio( uio); /* Create space for the sampled data */ play_buffer = new char[ BUFFERSIZE * NUMBUFFERS]; buffer_count = 0; /* IO Tranfer for recording */ rec_iott = new DX_IOTT; rec_iott->io_type = IO_DEV|IO_EOT|IO_UIO; /* Device IO | Last IOTT in chain | Use UIO */ rec_iott->io_offset = 0; rec_iott->io_length = BUFFERSIZE * NUMBUFFERS; rec_iott->io_fhandle = 42; /* Our our 'make-believe' fd */ /* IO Termination Parameter for recording */ rec_tpt = new DV_TPT; rec_tpt->tp_type = IO_EOT; rec_tpt->tp_termno = DX_MAXTIME; /* Timed termination */ rec_tpt->tp_length = 50; /* 50 * 100 ms = 5 seconds */ current_pointer = 0; /* Open the first channel on the first board */ chdev = dx_open( "dxxxB1C1", 0); /* Set on board buffer transfer size (bytes) */ dx_setchxfercnt( chdev, 512); /* Make sure the phone in on hook*/ dx_sethook( chdev, DX_ONHOOK, EV_SYNC); /* * In addition to play and record events, we're also interested * in RING events. So set the event mask here: */ dx_setevtmsk(chdev, DM_RINGS); /* Wait, synchronously, for the event */ printf( "Waiting for ring...\n"); dx_getevt( chdev, &eblk, -1); /* Assume its a RING event and continue... */ printf( "Got ring... picking up phone.\n"); dx_sethook( chdev, DX_OFFHOOK, EV_SYNC); printf( "Recording..."); /* Start recording, asynchronously */ dx_rec( chdev, rec_iott, rec_tpt, EV_ASYNC|MD_PCM|RM_SR8); while( getEvent()); printf( "\nDone recording!\n"); /* Now create a IO Transfer structure for playing */ play_iott = new DX_IOTT; play_iott->io_type = IO_DEV|IO_EOT|IO_UIO; play_iott->io_length = current_pointer; play_iott->io_offset = 0; play_iott->io_fhandle = 42; current_pointer = 0; printf( "Playing...0x%X\n", *(int *)play_buffer); dx_setchxfercnt( chdev, 1024); /* Play back (synchronous) */ dx_play( chdev, play_iott, NULL, EV_SYNC|MD_PCM|RM_SR8); printf( "Done playing.\n"); /* hang up */ dx_sethook( chdev, DX_ONHOOK, EV_SYNC); printf( "Done!!\n"); } /* User defined IO functions. These handle IO for recording. */ int my_write( int fd, char* ptr, unsigned count) { printf( "my_write!! fd = %d ptr = 0x%X count = %d buffer_count = %d\n", fd, ptr, count, buffer_count); /* Put a custom event in the Dialogic event queue, with our event type */ sr_putevt( fd, VCHAN_RECORD, count, ptr, 0); return count; } int my_read( int fd, char* ptr, unsigned count) { printf( "my_read! fd = %d ptr = 0x%X count = %d\n", fd, ptr, count); /* Copy the data from our buffer into *ptr */ memcpy( ptr, play_buffer + current_pointer, count); current_pointer += count; return count; } long my_seek( int fd, long offset, int origin) { printf( "my_seek! fd = %d offset = %d origin = 0x%X\n", fd, offset, origin); /* Ignore this call and just return offset. */ return offset; } int getEvent() { int event_result, event_length; long event_dev, event_type; printf( "."); /* Get the next event off the queue */ event_result = sr_waitevt( 20); if( event_result != -1) { printf( "\n"); event_dev = sr_getevtdev( 0); event_type = sr_getevttype( 0); /* Now switch based on event type */ switch( event_type) { case TDX_RECORD: /* Finished recording */ printf( "Got record finished event!\n"); return 0; /* end the loop */ case VCHAN_RECORD: /* Our own event type- contains sampled data */ printf( "Got VCHAN record event on device %d\n", event_dev); /* Get the size of the event data (samples) */ event_length = sr_getevtlen(); printf( "Event length is %d, copying into play_buffer... (%d)\n", event_length, buffer_count++); /* Save the samples for later */ memcpy( play_buffer + current_pointer, sr_getevtdatap(), event_length); current_pointer += event_length; printf( "current_pointer is now 0x%X\n", current_pointer); return 1; /* continue to loop */ } } return 1; }