#include <stdio.h>
#include "rs232.h"
Go to the source code of this file.
Data Structures | |
struct | s_wave_data |
struct | s_wave_dvi_block_header |
struct | s_wave_fmt |
struct | s_wave_fmt_dvi |
struct | s_wave_riff |
Defines | |
#define | BYTE unsigned char |
#define | DWORD unsigned long |
#define | RS232_BAUD_RATE CBR_115200 |
#define | RS232_BYTE_SIZE 8 |
#define | RS232_PARITY RS232_PARITY_NOPARITY |
#define | RS232_PORT "COM1" |
#define | RS232_STOP_BIT RS232_STOP_BIT_ONE |
#define | WAVE_FORMAT_DVI_ADPCM 0x0011 |
#define | WORD unsigned short |
Functions | |
int | fget_struct (FILE *_file, void *_ptr, int size, char *_start_str) |
int | main (int argc, char *_argv[]) |
#define BYTE unsigned char |
Definition at line 47 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.
#define DWORD unsigned long |
Definition at line 49 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.
#define RS232_BAUD_RATE CBR_115200 |
#define RS232_BYTE_SIZE 8 |
#define RS232_PARITY RS232_PARITY_NOPARITY |
#define RS232_PORT "COM1" |
#define RS232_STOP_BIT RS232_STOP_BIT_ONE |
#define WAVE_FORMAT_DVI_ADPCM 0x0011 |
Definition at line 54 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.
#define WORD unsigned short |
Definition at line 48 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.
int fget_struct | ( | FILE * | _file, | |
void * | _ptr, | |||
int | size, | |||
char * | _start_str | |||
) |
Definition at line 100 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.
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 }
int main | ( | int | argc, | |
char * | _argv[] | |||
) |
Definition at line 129 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.
References s_wave_fmt::avg_bytes_per_sec, s_wave_fmt::bits_per_sample, s_wave_fmt::block_align, s_wave_data::chunk_size, s_wave_fmt::compression_code, fget_struct(), s_wave_fmt_dvi::fmt, s_wave_dvi_block_header::isamp0, s_wave_fmt::nb_channels, RS232_BAUD_RATE, RS232_BYTE_SIZE, rs232_close(), rs232_open(), RS232_PARITY, RS232_PORT, rs232_read(), RS232_STOP_BIT, rs232_write(), s_wave_fmt::sample_rate, s_wave_fmt_dvi::samples_per_block, s_wave_dvi_block_header::step_table_index, and WAVE_FORMAT_DVI_ADPCM.
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 // Open the desired rs232 port 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 // Support only the 4 bits per sample format. 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 // Wait until the start sending command is recieved 00228 do 00229 { 00230 while(!rs232_read(&c, 1, &k)); 00231 }while(k != 1 && c != 'S'); 00232 00233 // Send the length of the data block (4 bytes) 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 // Send the sample rate of the sound (4 bytes) 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 // Read the last channel 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 // Wait until the start sending command is recieved 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 // send the initialization parameters 00277 // The predicted value (2 bytes) 00278 rs232_write(&((char *) &predicted_value)[1], 1, &k); 00279 rs232_write(&((char *) &predicted_value)[0], 1, &k); 00280 // The step index (2 bytes) 00281 rs232_write(&((char *) &step_index)[1], 1, &k); 00282 rs232_write(&((char *) &step_index)[0], 1, &k); 00283 00284 // Send data 00285 for(i=0; i<nb_bytes_per_block; i++) 00286 { 00287 // Ignore the first channels 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 // Close the rs232 port 00301 rs232_close(); 00302 fclose(_file); 00303 00304 return 1; 00305 }