1 /*  wave.h - WAVE data definitions
2  *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
3  *
4  *  This program is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU General Public License
6  *  as published by the Free Software Foundation; either version 2
7  *  of the License, or (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 /*
20  * $Id: wave.h,v 1.48 2009/03/11 17:18:01 jason Exp $
21  */
22 
23 #ifndef __WAVE_H__
24 #define __WAVE_H__
25 
26 #include <stdio.h>
27 #include "format-types.h"
28 
29 #define WAVE_RIFF                       "RIFF"
30 #define WAVE_WAVE                       "WAVE"
31 #define WAVE_FMT                        "fmt "
32 #define WAVE_DATA                       "data"
33 
34 #define AIFF_FORM                       "FORM"
35 #define AIFF_FORM_TYPE_AIFF             "AIFF"
36 #define AIFF_FORM_TYPE_AIFC             "AIFC"
37 #define AIFF_COMM                       "COMM"
38 #define AIFF_COMPRESSION_NONE           "NONE"
39 #define AIFF_COMPRESSION_SOWT           "sowt"
40 #define AIFF_SSND                       "SSND"
41 
42 #define WAVE_FORMAT_UNKNOWN             (0x0000)
43 #define WAVE_FORMAT_PCM                 (0x0001)
44 #define WAVE_FORMAT_ADPCM               (0x0002)
45 #define WAVE_FORMAT_IEEE_FLOAT          (0x0003)
46 #define WAVE_FORMAT_ALAW                (0x0006)
47 #define WAVE_FORMAT_MULAW               (0x0007)
48 #define WAVE_FORMAT_OKI_ADPCM           (0x0010)
49 #define WAVE_FORMAT_IMA_ADPCM           (0x0011)
50 #define WAVE_FORMAT_DIGISTD             (0x0015)
51 #define WAVE_FORMAT_DIGIFIX             (0x0016)
52 #define WAVE_FORMAT_DOLBY_AC2           (0x0030)
53 #define WAVE_FORMAT_GSM610              (0x0031)
54 #define WAVE_FORMAT_ROCKWELL_ADPCM      (0x003b)
55 #define WAVE_FORMAT_ROCKWELL_DIGITALK   (0x003c)
56 #define WAVE_FORMAT_G721_ADPCM          (0x0040)
57 #define WAVE_FORMAT_G728_CELP           (0x0041)
58 #define WAVE_FORMAT_MPEG                (0x0050)
59 #define WAVE_FORMAT_MPEGLAYER3          (0x0055)
60 #define WAVE_FORMAT_G726_ADPCM          (0x0064)
61 #define WAVE_FORMAT_G722_ADPCM          (0x0065)
62 #define WAVE_FORMAT_EXTENSIBLE          (0xfffe)
63 
64 #define CD_BLOCK_SIZE                   (2352)
65 #define CD_BLOCKS_PER_SEC               (75)
66 #define CD_BLOCK_ALIGN                  (4)
67 #define CD_MIN_BURNABLE_SIZE            (705600)
68 #define CD_CHANNELS                     (2)
69 #define CD_SAMPLES_PER_SEC              (44100)
70 #define CD_BITS_PER_SAMPLE              (16)
71 #define CD_RATE                         (176400)
72 
73 #define CANONICAL_HEADER_SIZE           (44)
74 
75 #define PROBLEM_NOT_CD_QUALITY          (0x00000001)
76 #define PROBLEM_CD_BUT_BAD_BOUND        (0x00000002)
77 #define PROBLEM_CD_BUT_TOO_SHORT        (0x00000004)
78 #define PROBLEM_HEADER_NOT_CANONICAL    (0x00000008)
79 #define PROBLEM_EXTRA_CHUNKS            (0x00000010)
80 #define PROBLEM_HEADER_INCONSISTENT     (0x00000020)
81 #define PROBLEM_MAY_BE_TRUNCATED        (0x00000040)
82 #define PROBLEM_JUNK_APPENDED           (0x00000080)
83 #define PROBLEM_DATA_NOT_ALIGNED        (0x00000100)
84 
85 /* macros to determine if files have certain problems */
86 
87 #define PROB_NOT_CD(f)                  ((f->problems) & (PROBLEM_NOT_CD_QUALITY))
88 #define PROB_BAD_BOUND(f)               ((f->problems) & (PROBLEM_CD_BUT_BAD_BOUND))
89 #define PROB_TOO_SHORT(f)               ((f->problems) & (PROBLEM_CD_BUT_TOO_SHORT))
90 #define PROB_HDR_NOT_CANONICAL(f)       ((f->problems) & (PROBLEM_HEADER_NOT_CANONICAL))
91 #define PROB_EXTRA_CHUNKS(f)            ((f->problems) & (PROBLEM_EXTRA_CHUNKS))
92 #define PROB_HDR_INCONSISTENT(f)        ((f->problems) & (PROBLEM_HEADER_INCONSISTENT))
93 #define PROB_TRUNCATED(f)               ((f->problems) & (PROBLEM_MAY_BE_TRUNCATED))
94 #define PROB_JUNK_APPENDED(f)           ((f->problems) & (PROBLEM_JUNK_APPENDED))
95 #define PROB_DATA_NOT_ALIGNED(f)        ((f->problems) & (PROBLEM_DATA_NOT_ALIGNED))
96 #define PROB_ODD_SIZED_DATA(f)          (f->data_size & 1)
97 
98 typedef struct _wave_info {
99   char *filename,              /* file name of input file                             */
100         m_ss[16];              /* length, in m:ss.nnn or m:ss.ff format               */
101 
102   wint header_size;            /* length of header, in bytes                          */
103 
104   long extra_riff_size;        /* total size of any extra RIFF chunks                 */
105 
106   wshort channels,             /* number of channels                                  */
107          block_align,          /* block align                                         */
108          bits_per_sample,      /* bits per sample                                     */
109          wave_format;          /* WAVE data format                                    */
110 
111   wlong samples_per_sec,       /* samples per second                                  */
112         avg_bytes_per_sec,     /* average bytes per second (should equal rate)        */
113         rate,                  /* calculated rate (should equal avg_bytes_per_sec)    */
114         length,                /* length of file, in seconds                          */
115         data_size,             /* length of data chunk, as reported in the header     */
116         padded_data_size,      /* length of data chunk, including possible pad byte   */
117         total_size,            /* total length of the WAVE data, header and all       */
118         chunk_size,            /* read from header, should be total_size - 8          */
119         actual_size,           /* set to the size of input file - this is misleading, */
120                                /* since the input file may be compressed              */
121         new_data_size,         /* (set and used only in certain modes, ignore)        */
122         beginning_byte,        /* (set and used only in certain modes, ignore)        */
123         new_beginning_byte;    /* (set and used only in certain modes, ignore)        */
124 
125   double exact_length;         /* length of file, in seconds (with ms precision)      */
126 
127   unsigned long problems;      /* bitmap of problems found with the file, see above   */
128 
129   struct _format_module
130                 *input_format; /* pointer to the input format module that opens this  */
131 
132   FILE *input, *output;        /* input/output file pointers (= NULL, use as needed)  */
133 
134   proc_info input_proc;        /* pid (and handle on WIN32) of child input process    */
135   proc_info output_proc;       /* pid (and handle on WIN32) of child output process   */
136 
137   bool file_has_id3v2_tag;     /* does this file contain an ID3v2 tag?                */
138   bool stream_has_id3v2_tag;   /* does the decoded input stream contain an ID3v2 tag? */
139   wlong id3v2_tag_size;        /* size of the ID3v2 tag this file contains, if any    */
140 } wave_info;
141 
142 /* returns a wave_info struct, filled out with the values of the WAVE data contained in the filename given. */
143 /* If called with NULL as the argument, then a wave_info struct is returned with all fields zero'd out.     */
144 wave_info *new_wave_info(char *);
145 
146 /* constructs a canonical WAVE header from the values in the wave_info struct */
147 void make_canonical_header(unsigned char *buf,wave_info *info);
148 
149 /* returns a string corresponding to the WAVE format code given */
150 char *format_to_str(wshort);
151 
152 /* replaces the size chunk size at beginning of the wave header */
153 void put_chunk_size(unsigned char *,unsigned long);
154 
155 /* replaces the size reported in the "data" chunk of the wave header with the new size -
156    also updates chunk size at beginning of the wave header */
157 void put_data_size(unsigned char *,int,unsigned long);
158 
159 /* kluges the WAVE header to get correct values when helper programs don't provide them */
160 bool do_header_kluges(unsigned char *,wave_info *);
161 
162 /* verifies that data coming in on the associated file descriptor describes a valid WAVE header */
163 bool verify_wav_header_internal(wave_info *,bool);
164 #define verify_wav_header(a) verify_wav_header_internal(a,FALSE)
165 
166 #endif
167