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