Ignore:
Timestamp:
Apr 22, 2011, 7:32:17 PM (11 years ago)
Author:
Sam Hocevar
Message:

imlib: remove a lot of dead code, especially from the linked list and
the WAV reader.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/imlib/readwav.cpp

    r524 r534  
    1919struct wav_chunk
    2020{
    21   char id[4];
    22   unsigned long size;
    23   char type[4];
    24 } ;
     21    char id[4];
     22    uint32_t size;
     23    char type[4];
     24};
    2525
    2626struct wav_tag
    2727{
    28   char id[4];
    29   unsigned long size;
    30 } ;
     28    char id[4];
     29    uint32_t size;
     30};
    3131
    3232
    3333struct wav_format
    3434{
    35   unsigned short fmt_tag,channels;
    36   unsigned long samplesps,avg_bytesps;
    37   unsigned short align;
    38 } ;
    39 
     35    uint16_t fmt_tag, channels;
     36    uint32_t samplesps, avg_bytesps;
     37    uint16_t align;
     38};
    4039
    4140struct pcm_wave
    4241{
    43   wav_format wf;
    44   unsigned short bitsps;
    45 } ;
     42    wav_format wf;
     43    uint16_t bitsps;
     44};
    4645
    47 
    48 
    49 
    50 void read_chunk(wav_chunk &chunk, bFILE *fp)
     46static void read_chunk(wav_chunk &chunk, bFILE *fp)
    5147{
    52   fp->read(&chunk.id,4);
    53   chunk.size=fp->read_uint32();
    54   fp->read(&chunk.type,4);
     48    fp->read(&chunk.id, 4);
     49    chunk.size = fp->read_uint32();
     50    fp->read(&chunk.type, 4);
    5551}
    5652
    57 void read_tag(wav_tag &tag, bFILE *fp)
     53static void read_tag(wav_tag &tag, bFILE *fp)
    5854{
    59   fp->read(&tag.id,4);
    60   tag.size=fp->read_uint32();
     55    fp->read(&tag.id, 4);
     56    tag.size = fp->read_uint32();
    6157}
    6258
    63 void read_wav_format(wav_format &fmt, bFILE *fp)
     59static void read_pcm(pcm_wave &pcm, bFILE *fp)
    6460{
    65   fmt.fmt_tag=fp->read_uint16();
    66   fmt.channels=fp->read_uint16();
    67   fmt.samplesps=fp->read_uint32();
    68   fmt.avg_bytesps=fp->read_uint32();
    69   fmt.align=fp->read_uint16();
     61    pcm.wf.fmt_tag = fp->read_uint16();
     62    pcm.wf.channels = fp->read_uint16();
     63    pcm.wf.samplesps = fp->read_uint32();
     64    pcm.wf.avg_bytesps = fp->read_uint32();
     65    pcm.wf.align = fp->read_uint16();
     66    pcm.bitsps = fp->read_uint16();
    7067}
    7168
     69uint8_t *read_wav(char const *filename, int &sample_rate, int &data_size)
     70{
     71    bFILE *fp = open_file(filename, "rb");
     72    if (fp->open_failure())
     73    {
     74        delete fp;
     75        return NULL;
     76    }
    7277
    73 void read_pcm(pcm_wave &pcm, bFILE *fp)
    74 {
    75   read_wav_format(pcm.wf,fp);
    76   pcm.bitsps=fp->read_uint16();
     78    wav_chunk chunk;
     79    read_chunk(chunk, fp);
     80    if (memcmp(chunk.type, "WAVE", 4) != 0)
     81    {
     82        printf("Bad WAV file (chunk) %s\n", filename);
     83        delete fp;
     84        return NULL;
     85    }
     86
     87    wav_tag tag;
     88    read_tag(tag, fp);
     89    if (memcmp(tag.id, "fmt ", 4) != 0)
     90    {
     91        printf( "fmt tag missing, bad file (%s)\n", filename);
     92        delete fp;
     93        return NULL;
     94    }
     95
     96    pcm_wave pcm;
     97    read_pcm(pcm, fp);
     98
     99    fp->seek(tag.size - 16, SEEK_CUR);  // seek to offset of sample
     100
     101    read_tag(tag, fp);
     102
     103    if (memcmp(tag.id, "data", 4) != 0)
     104    {
     105        printf("Bad Wav file (tag), %s\n", filename);
     106        delete fp;
     107        return NULL;
     108    }
     109
     110    data_size = tag.size;
     111    uint8_t *ret = (uint8_t *)malloc(tag.size);
     112    ERROR(ret, "Malloc error");
     113
     114    sample_rate = pcm.wf.samplesps;
     115    ERROR((unsigned int)fp->read(ret, tag.size) == tag.size, "Premature end of file");
     116    ERROR(pcm.bitsps == 8, "Only 8-bit samples supported");
     117    ERROR(pcm.wf.channels == 1, "Only mono samples supported");
     118    ERROR(pcm.wf.align == 1, "Bad block alignment");
     119    delete fp;
     120    return ret;
    77121}
    78122
    79 
    80 
    81 void write_wav(char *filename, long sample_rate, long data_size, unsigned char *data)
    82 {
    83   bFILE *fp=open_file(filename,"wb");
    84   if (fp->open_failure())
    85   {
    86     printf("Unable to open %s for writing\n", filename);
    87     delete fp;
    88     exit(1);
    89   }
    90 
    91   /***************  Write the chunk ***************************/
    92   fp->write((void *)"RIFF",4);
    93   fp->write_uint32(data_size+36);
    94   fp->write((void *)"WAVE",4);
    95 
    96 
    97   /************** Write the tag *******************************/
    98   fp->write((void *)"fmt ",4);
    99   fp->write_uint32(16);
    100 
    101 
    102   /************** Write PCM ***********************************/
    103   fp->write_uint16(1);          // format_tag
    104   fp->write_uint16(1);          // mono recording
    105   fp->write_uint32(sample_rate);
    106   fp->write_uint32(sample_rate);   // average bytes per sec
    107   fp->write_uint16(1);    // alignment? Don't know what this does?
    108   fp->write_uint16(8);    // 8 bits per sample
    109 
    110   /************* Write data tag ******************************/
    111   fp->write((void *)"data",4);
    112   fp->write_uint32(data_size);
    113 
    114   /************ Now write sample data ************************/
    115   fp->write(data,data_size);
    116 
    117   delete fp;
    118 }
    119 
    120 
    121 
    122 unsigned char *read_wav(char *filename, long &sample_rate, long &data_size)
    123 {
    124   unsigned char *data;
    125   wav_chunk chunk;
    126   wav_tag tag;
    127   pcm_wave pcm;
    128 
    129   bFILE *fp=open_file(filename,"rb");
    130   if (fp->open_failure())
    131   { delete fp; return NULL; }
    132   read_chunk(chunk,fp);
    133   if (memcmp(chunk.type,"WAVE",4)!=0)
    134   {
    135     printf("Bad WAV file (chunk) %s\n",filename);
    136     delete fp;
    137     return NULL;
    138   }
    139 
    140   read_tag(tag,fp);
    141   if (memcmp(tag.id,"fmt ",4)!=0)
    142   {
    143     printf( "fmt tag missing, bad file (%s)\n",filename);
    144     delete fp;
    145     return NULL;
    146   }
    147 
    148 
    149   read_pcm(pcm,fp);
    150 
    151   fp->seek(tag.size-16,SEEK_CUR);  // seek to offset of sample
    152 
    153   read_tag(tag,fp);
    154 
    155   if (memcmp(tag.id,"data",4)!=0)
    156   {
    157     printf("Bad Wav file (tag), %s\n",filename);
    158     delete fp;
    159     return NULL;
    160   }
    161 
    162   data_size=tag.size;
    163   data=(unsigned char *)malloc(tag.size);
    164   ERROR(data,"Malloc error");
    165 
    166   sample_rate=pcm.wf.samplesps;
    167   ERROR((unsigned int)fp->read(data,tag.size)==tag.size,"Premature end of file");
    168   ERROR(pcm.bitsps==8,"Only 8-bit samples supported");
    169   ERROR(pcm.wf.channels==1,"Only mono samples supported");
    170   ERROR(pcm.wf.align==1,"Bad block alignment");
    171   delete fp;
    172   return data;
    173 }
    174 
    175 
    176 
Note: See TracChangeset for help on using the changeset viewer.