00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "rs232.h"
00033
00034
00035 #define RS232_PORT "COM1"
00036
00037 #define RS232_BAUD_RATE CBR_115200
00038
00039 #define RS232_BYTE_SIZE 8
00040
00041 #define RS232_PARITY RS232_PARITY_NOPARITY
00042
00043 #define RS232_STOP_BIT RS232_STOP_BIT_ONE
00044
00045 #define BYTE unsigned char
00046 #define WORD unsigned short
00047 #define DWORD unsigned long
00048
00049
00050
00051
00052 #define WAVE_FORMAT_DVI_ADPCM 0x0011
00053
00054 typedef struct __attribute__((__packed__))
00055 {
00056 BYTE chunk_id[4];
00057 DWORD chunk_size;
00058 BYTE riff_type[4];
00059 }s_wave_riff;
00060
00061 typedef struct __attribute__((__packed__))
00062 {
00063 BYTE chunk_id[4];
00064 DWORD chunk_size;
00065 WORD compression_code;
00066 WORD nb_channels;
00067 DWORD sample_rate;
00068 DWORD avg_bytes_per_sec;
00069 WORD block_align;
00070 WORD bits_per_sample;
00071 WORD extra_bytes;
00072 }s_wave_fmt;
00073
00074 typedef struct __attribute__((__packed__))
00075 {
00076 s_wave_fmt fmt;
00077 WORD samples_per_block;
00078 }s_wave_fmt_dvi;
00079
00080 typedef struct __attribute__((__packed__))
00081 {
00082 BYTE chunk_id[4];
00083 DWORD chunk_size;
00084 }s_wave_data;
00085
00086 typedef struct __attribute__((__packed__))
00087 {
00088 WORD isamp0;
00089 BYTE step_table_index;
00090 BYTE reserved;
00091 }s_wave_dvi_block_header;
00092
00093
00094
00095
00096
00097
00098 int fget_struct(FILE *_file, void *_ptr, int size, char *_start_str)
00099 {
00100 int end;
00101 char *_str;
00102
00103 end = 0;
00104 while(!feof(_file) && !end)
00105 {
00106 _str = _start_str;
00107 while(*_str == fgetc(_file))
00108 {
00109 _str++;
00110 if (!*_str)
00111 {
00112 end = 1;
00113 break;
00114 }
00115 }
00116 }
00117
00118 if (!end)
00119 return 0;
00120
00121 fseek(_file, -strlen(_start_str), SEEK_CUR);
00122 fread(_ptr, 1, size, _file);
00123
00124 return 1;
00125 }
00126
00127 int main(int argc, char *_argv[])
00128 {
00129 FILE *_file;
00130 s_wave_riff header_riff;
00131 s_wave_fmt_dvi header_dvi;
00132 s_wave_data header_data;
00133 s_wave_dvi_block_header header_block;
00134 short step_index;
00135 short predicted_value;
00136 char _buffer[4];
00137 int i, j, k, l, nb_bytes_per_block;
00138 int block_size;
00139 char c;
00140 int block_sent = 0;
00141 int end = 0;
00142 char _progress_bar[33];
00143
00144 if (argc != 2)
00145 {
00146 printf("usage: ADPCM_IMA_DVI filename\n");
00147 return 0;
00148 }
00149
00150 _file = fopen(_argv[1], "rb");
00151
00152 if (!_file)
00153 {
00154 printf("Unable to open file.\n");
00155 return 0;
00156 }
00157
00158 printf("IMA/DVI ADPCM decoder\n");
00159 printf("");
00160
00161 if (!fget_struct(_file, &header_riff, sizeof(s_wave_riff), "RIFF"))
00162 {
00163 printf("Invalid RIFF File.\n");
00164 fclose(_file);
00165 return 0;
00166 }
00167 if (!fget_struct(_file, &header_dvi, sizeof(s_wave_fmt_dvi), "fmt "))
00168 {
00169 printf("Invalid RIFF Format.\n");
00170 fclose(_file);
00171 return 0;
00172 }
00173 if (!fget_struct(_file, &header_data, sizeof(s_wave_data), "data"))
00174 {
00175 printf("Invalid RIFF Data.\n");
00176 fclose(_file);
00177 return 0;
00178 }
00179
00180 if (header_dvi.fmt.compression_code != WAVE_FORMAT_DVI_ADPCM)
00181 {
00182 printf("Invalid IMA/DVI ADPCM File.\n");
00183 fclose(_file);
00184 return 0;
00185 }
00186
00187 if (header_dvi.fmt.bits_per_sample != 4)
00188 {
00189 printf("Error! The input adpcm wav file must have a number of bits per sample equals to 4.\n");
00190 fclose(_file);
00191 return 0;
00192 }
00193
00194 printf("File name: %s\n", _argv[1]);
00195 printf("Number of channels: %i\n", header_dvi.fmt.nb_channels);
00196 printf("Sample rate: %i Hz\n", header_dvi.fmt.sample_rate);
00197 printf("Total average data rate: %i\n", header_dvi.fmt.avg_bytes_per_sec);
00198 printf("Block alignment: %i bytes\n", header_dvi.fmt.block_align);
00199 printf("Number of bits per sample: %i bits\n", header_dvi.fmt.bits_per_sample);
00200 printf("Number of samples per channel per Block: %i samples\n", header_dvi.samples_per_block);
00201
00202 if (header_dvi.fmt.nb_channels > 1)
00203 printf("This program will only decode the last channel of the input wav file.\n");
00204
00205 printf("Opening serial port %s...", RS232_PORT);
00206 fflush(stdout);
00207
00208
00209 if (!rs232_open(RS232_PORT, RS232_BAUD_RATE, RS232_BYTE_SIZE, RS232_PARITY, RS232_STOP_BIT))
00210 {
00211 printf("\t[ FAILED ]\n");
00212 fclose(_file);
00213 return 0;
00214 }
00215
00216 printf("\t[ OK ]\n");
00217
00218
00219 nb_bytes_per_block = (header_dvi.fmt.block_align/(4*header_dvi.fmt.nb_channels)-1);
00220 block_size = nb_bytes_per_block*4;
00221
00222 printf("Waiting for serial communication with the interface...");
00223 fflush(stdout);
00224
00225
00226 do
00227 {
00228 while(!rs232_read(&c, 1, &k));
00229 }while(k != 1 && c != 'S');
00230
00231
00232 rs232_write(&((char *) &block_size)[3], 1, &k);
00233 rs232_write(&((char *) &block_size)[2], 1, &k);
00234 rs232_write(&((char *) &block_size)[1], 1, &k);
00235 rs232_write(&((char *) &block_size)[0], 1, &k);
00236
00237
00238 rs232_write(&((char *) &(header_dvi.fmt.sample_rate))[3], 1, &k);
00239 rs232_write(&((char *) &(header_dvi.fmt.sample_rate))[2], 1, &k);
00240 rs232_write(&((char *) &(header_dvi.fmt.sample_rate))[1], 1, &k);
00241 rs232_write(&((char *) &(header_dvi.fmt.sample_rate))[0], 1, &k);
00242
00243 printf("\t[ OK ]\nSending initialization parameters.\n");
00244
00245 for(j=0; j<header_data.chunk_size/header_dvi.fmt.block_align && !end; j++)
00246 {
00247
00248 for(i=0; i<header_dvi.fmt.nb_channels; i++)
00249 fread(&header_block, 1, sizeof(s_wave_dvi_block_header), _file);
00250
00251 predicted_value = header_block.isamp0;
00252 step_index = header_block.step_table_index;
00253
00254
00255 do
00256 {
00257 while(!rs232_read(&c, 1, &k));
00258 if (kbhit())
00259 end = 1;
00260 }while((k != 1 || c != 1) && !end);
00261
00262 if (!end)
00263 {
00264 ++block_sent;
00265 k = (block_sent*block_size*32)/header_data.chunk_size;
00266 for(i=0; i<k; i++)
00267 _progress_bar[i] = '=';
00268 for(;i<32; i++)
00269 _progress_bar[i] = ' ';
00270 _progress_bar[i] = '\0';
00271 printf("\r[%32s] %i bytes", _progress_bar, block_sent*block_size);
00272 fflush(stdout);
00273
00274
00275
00276 rs232_write(&((char *) &predicted_value)[1], 1, &k);
00277 rs232_write(&((char *) &predicted_value)[0], 1, &k);
00278
00279 rs232_write(&((char *) &step_index)[1], 1, &k);
00280 rs232_write(&((char *) &step_index)[0], 1, &k);
00281
00282
00283 for(i=0; i<nb_bytes_per_block; i++)
00284 {
00285
00286 for(k=0; k<(header_dvi.fmt.nb_channels-1); k++)
00287 fread(_buffer, 1, 4, _file);
00288
00289 fread(_buffer, 1, 4, _file);
00290 rs232_write(&_buffer[0], 1, &k);
00291 rs232_write(&_buffer[1], 1, &k);
00292 rs232_write(&_buffer[2], 1, &k);
00293 rs232_write(&_buffer[3], 1, &k);
00294 }
00295 }
00296 }
00297
00298
00299 rs232_close();
00300 fclose(_file);
00301
00302 return 1;
00303 }