Main Page | Class List | File List | Class Members | File Members

audio.c

Go to the documentation of this file.
00001 /*
00002  * Fake audio coding functions for simulating audio coder
00003  */
00004 
00005 #include "standard.h"
00006 #include "bits.h"
00007 #include "config.h"
00008 #include "audio.h"
00009 
00010 extern int debug;
00011 
00012 
00013 
00014 #define DEF_AUD_INPUT_FILE    "audio.in"
00015 #define DEF_AUD_OUTPUT_FILE   "audio.rec"
00016 
00017 audio_codec *new_audio_codec(char *name)
00018 /*
00019  * Sets up an audio codec and returns a pointer to it.
00020  */
00021 {
00022   char s[256];
00023   int x;
00024   audio_codec *a;
00025 
00026   a = (audio_codec *) malloc(sizeof(audio_codec));
00027   if (a == NULL)
00028     error("new_audio_codec","can't malloc audio codec");
00029 
00030   strcpy(a->name, name);
00031 
00032   strcpy(a->input_filename, DEF_AUD_INPUT_FILE);
00033   strcpy(a->output_filename, DEF_AUD_OUTPUT_FILE);
00034 
00035   a->input_file=NULL;
00036 
00037   return a;
00038 }
00039 
00040 
00041 void close_audio_codec(audio_codec *a)
00042 /*
00043  * Closes up the audio codec; closes files and frees variables
00044  */
00045 {
00046   if (a->input_file != NULL) {
00047     printf("%s AUDIO [%s] closing input file.\n", print_time(), a->name);
00048     fclose(a->input_file);
00049   }
00050   free(a);
00051 }
00052 
00053 
00054 void code_audio(audio_codec *a)
00055 /*
00056  * Fakes audio coding, either from file or random
00057  */
00058 {
00059   bytes *b;
00060   int size,r,i,sendok;
00061 
00062   if (get_clock() % 30 == 0) /* encode every 30 ms */
00063   {
00064     /* generate bytes structure to send of size 27 23 or 6*/
00065     r = rand() % 3;
00066     switch (r) {
00067       case 0:
00068         size = 27;
00069         break;
00070       case 1:
00071         size = 23;
00072         break;
00073       case 2:
00074         size = 6;
00075         break;
00076     }
00077 
00078     if (debug) {
00079       printf("%s AUDIO [%s] sent %d bytes\n", print_time(), a->name, size);
00080     }
00081 
00082     b = new_bytes(size);
00083     if (!a->input_file) {
00084       a->input_file=fopen(a->input_filename, "r");
00085       if (!a->input_file) {
00086         warn("code_audio", "input audio file does not exist.\n");
00087         a->input_filename[0]=0;
00088       }
00089     }
00090     for (i=0;i<size;i++) {
00091       if (a->input_filename[0]) {
00092           if (feof(a->input_file))
00093             fseek(a->input_file,0,0);
00094           b->block[i] = fgetc(a->input_file);
00095       }
00096       else {
00097         randomize_byte((b->block)+i);
00098       }
00099     }
00100 
00101     /* send it to the al sending entity */
00102     sendok = al_send_request(a->output, b);
00103     if (!sendok)
00104       error("code_audio","Sending bytes to AL failed");
00105   }
00106 }
00107 
00108 
00109 
00110 void decode_audio(audio_codec *a)
00111 /*
00112  * Sees if there's anything in the AL to receive.  If so, receives
00113  * and decodes it (just writes to the output file)
00114  */
00115 {
00116   byte b;
00117 
00118   FILE *outfile=NULL;
00119 
00120   if (al_indication(a->input)) {
00121     outfile = fopen(a->output_filename, "a");
00122     if (outfile == NULL) {
00123       error("decode_audio", "couldn't open output file");
00124     }
00125   }
00126 
00127   while (al_indication(a->input)) { 
00128     b = al_receive(a->input);
00129         
00130     fputc(b, outfile);
00131   }
00132 
00133   if (outfile) {
00134     fclose(outfile);
00135   }
00136 }

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1