main.c File Reference

#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 Documentation

#define BYTE   unsigned char

#define DWORD   unsigned long

#define RS232_BAUD_RATE   CBR_115200

Definition at line 37 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.

Referenced by main().

#define RS232_BYTE_SIZE   8

Definition at line 39 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.

Referenced by main().

#define RS232_PARITY   RS232_PARITY_NOPARITY

Definition at line 41 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.

Referenced by main().

#define RS232_PORT   "COM1"

Definition at line 35 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.

Referenced by main().

#define RS232_STOP_BIT   RS232_STOP_BIT_ONE

Definition at line 43 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.

Referenced by main().

#define WAVE_FORMAT_DVI_ADPCM   0x0011

#define WORD   unsigned short


Function Documentation

int fget_struct ( FILE *  _file,
void *  _ptr,
int  size,
char *  _start_str 
)

Definition at line 98 of file UTILS/PROGRAMS/WINDOWS/ADPCM_STREAMING/main.c.

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 }

int main ( int  argc,
char *  _argv[] 
)

Definition at line 127 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.

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   // Open the desired rs232 port
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   // Support only the 4 bits per sample format.
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   // Wait until the start sending command is recieved
00226   do
00227   {
00228     while(!rs232_read(&c, 1, &k));
00229   }while(k != 1 && c != 'S');
00230 
00231   // Send the length of the data block (4 bytes)
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   // Send the sample rate of the sound (4 bytes)
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     // Read the last channel
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     // Wait until the start sending command is recieved
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       // send the initialization parameters
00275       // The predicted value (2 bytes)
00276       rs232_write(&((char *) &predicted_value)[1], 1, &k);
00277       rs232_write(&((char *) &predicted_value)[0], 1, &k);
00278       // The step index (2 bytes)
00279       rs232_write(&((char *) &step_index)[1], 1, &k);
00280       rs232_write(&((char *) &step_index)[0], 1, &k);
00281 
00282       // Send data
00283       for(i=0; i<nb_bytes_per_block; i++)
00284       {
00285         // Ignore the first channels
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   // Close the rs232 port
00299   rs232_close();
00300   fclose(_file);
00301 
00302   return 1;
00303 }


Generated on Thu Dec 17 19:57:24 2009 for AVR32 UC3 - EVK1104 DSPLib Demo Documentation by  doxygen 1.5.5