1 /*
2  * libPCM by László Szerémi.
3  * Copyright under Boost License.
4  */
5 
6 module libPCM.common;
7 
8 import core.stdc.stdlib;
9 
10 import libPCM.types;
11 
12 /**
13  * Describes the type of codec used, as well as the bitdepth
14  */
15 public enum CodecType : ushort{
16 	NULL			=	0,
17 	UNSIGNED8BIT	=	1,
18 	UNSIGNED12BIT	=	2,
19 	UNSIGNED16BIT	=	3,
20 	UNSIGNED24BIT	=	4,
21 	UNSIGNED32BIT	=	5,
22 	SIGNED8BIT		=	6,
23 	SIGNED12BIT		=	7,
24 	SIGNED16BIT		=	8,
25 	SIGNED24BIT		=	9,
26 	SIGNED32BIT		=	10,
27 	DIALOGIC_ADPCM	=	32,
28 	IMA_ADPCM		=	33,
29 	COMPACT_ADPCM	=	34,
30 	MU_LAW			=	64,
31 	A_LAW_87_6		=	65,
32 	FLOAT			=	96,
33 }
34 
35 /**
36  * For *.wav files
37  */
38 public enum WAVAudioFormat : ushort{
39 	/* WAVE form wFormatTag IDs */
40 	UNKNOWN		=	0x0000, /* Microsoft Corporation */
41 	PCM			=	0x0001,
42 	ADPCM		=	0x0002, /* Microsoft Corporation */
43 	IEEE_FLOAT	=	0x0003, /* Microsoft Corporation */
44 	VSELP		=	0x0004, /* Compaq Computer Corp. */
45 	IBM_CVSD	=	0x0005, /* IBM Corporation */
46 	ALAW		=	0x0006, /* Microsoft Corporation */
47 	MULAW		=	0x0007, /* Microsoft Corporation */
48 	DTS			=	0x0008, /* Microsoft Corporation */
49 	OKI_ADPCM	=	0x0010, /* OKI */
50 	DVI_ADPCM	=	0x0011, /* Intel Corporation */
51 	IMA_ADPCM	=	DVI_ADPCM, /*  Intel Corporation */
52 	MEDIASPACE_ADPCM	=	0x0012, /* Videologic */
53 	SIERRA_ADPCM=	0x0013, /* Sierra Semiconductor Corp */
54 	G723_ADPCM	=	0x0014, /* Antex Electronics Corporation */
55 	DIGISTD		=	0x0015, /* DSP Solutions, Inc. */
56 	DIGIFIX		=	0x0016, /* DSP Solutions, Inc. */
57 	DIALOGIC_OKI_ADPCM	=	0x0017, /* Dialogic Corporation */
58 	MEDIAVISION_ADPCM	=	0x0018, /* Media Vision, Inc. */
59 	CU_CODEC	=	0x0019, /* Hewlett-Packard Company */
60 	YAMAHA_ADPCM=	0x0020, /* Yamaha Corporation of America */
61 	SONARC		=	0x0021, /* Speech Compression */
62 	DSPGROUP_TRUESPEECH	=	0x0022, /* DSP Group, Inc */
63 	ECHOSC1		=	0x0023, /* Echo Speech Corporation */
64 	AUDIOFILE_AF36		=	0x0024, /* Virtual Music, Inc. */
65 	APTX		=	0x0025, /* Audio Processing Technology */
66 	AUDIOFILE_AF10		=	0x0026, /* Virtual Music, Inc. */
67 	PROSODY_1612=	0x0027, /* Aculab plc */
68 	LRC			=	0x0028, /* Merging Technologies S.A. */
69 	DOLBY_AC2	=	0x0030, /* Dolby Laboratories */
70 	GSM610		=	0x0031, /* Microsoft Corporation */
71 	MSNAUDIO	=	0x0032, /* Microsoft Corporation */
72 	ANTEX_ADPCME=	0x0033, /* Antex Electronics Corporation */
73 	CONTROL_RES_VQLPC	=	0x0034, /* Control Resources Limited */
74 	DIGIREAL	=	0x0035, /* DSP Solutions, Inc. */
75 	DIGIADPCM	=	0x0036, /* DSP Solutions, Inc. */
76 	CONTROL_RES_CR10	=	0x0037, /* Control Resources Limited */
77 	NMS_VBXADPCM=	0x0038, /* Natural MicroSystems */
78 	CS_IMAADPCM	=	0x0039, /* Crystal Semiconductor IMA ADPCM */
79 	ECHOSC3		=	0x003A, /* Echo Speech Corporation */
80 	ROCKWELL_ADPCM		=	0x003B, /* Rockwell International */
81 	ROCKWELL_DIGITALK	=	0x003C, /* Rockwell International */
82 	XEBEC		=	0x003D, /* Xebec Multimedia Solutions Limited */
83 	G721_ADPCM	=	0x0040, /* Antex Electronics Corporation */
84 	G728_CELP	=	0x0041, /* Antex Electronics Corporation */
85 	MSG723		=	0x0042, /* Microsoft Corporation */
86 	MPEG		=	0x0050, /* Microsoft Corporation */
87 	RT24		=	0x0052, /* InSoft, Inc. */
88 	PAC			=	0x0053, /* InSoft, Inc. */
89 	MPEGLAYER3	=	0x0055, /* ISO/MPEG Layer3 Format Tag */
90 	LUCENT_G723	=	0x0059, /* Lucent Technologies */
91 	CIRRUS		=	0x0060, /* Cirrus Logic */
92 	ESPCM		=	0x0061, /* ESS Technology */
93 	VOXWARE		=	0x0062, /* Voxware Inc */
94 	CANOPUS_ATRAC		=	0x0063, /* Canopus, co., Ltd. */
95 	G726_ADPCM	=	0x0064, /* APICOM */
96 	G722_ADPCM	=	0x0065, /* APICOM */
97 	DSAT_DISPLAY=	0x0067, /* Microsoft Corporation */
98 	VOXWARE_BYTE_ALIGNED=	0x0069, /* Voxware Inc */
99 	VOXWARE_AC8	=	0x0070, /* Voxware Inc */
100 	VOXWARE_AC10=	0x0071, /* Voxware Inc */
101 	VOXWARE_AC16=	0x0072, /* Voxware Inc */
102 	VOXWARE_AC20=	0x0073, /* Voxware Inc */
103 	VOXWARE_RT24=	0x0074, /* Voxware Inc */
104 	VOXWARE_RT29=	0x0075, /* Voxware Inc */
105 	VOXWARE_RT29HW		=	0x0076, /* Voxware Inc */
106 	VOXWARE_VR12=	0x0077, /* Voxware Inc */
107 	VOXWARE_VR18=	0x0078, /* Voxware Inc */
108 	VOXWARE_TQ40=	0x0079, /* Voxware Inc */
109 	SOFTSOUND	=	0x0080, /* Softsound, Ltd. */
110 	VOXWARE_TQ60=	0x0081, /* Voxware Inc */
111 	MSRT24		=	0x0082, /* Microsoft Corporation */
112 	G729A		=	0x0083, /* AT&T Labs, Inc. */
113 	MVI_MVI2	=	0x0084, /* Motion Pixels */
114 	DF_G726		=	0x0085, /* DataFusion Systems (Pty) (Ltd) */
115 	DF_GSM610	=	0x0086, /* DataFusion Systems (Pty) (Ltd) */
116 	ISIAUDIO	=	0x0088, /* Iterated Systems, Inc. */
117 	ONLIVE		=	0x0089, /* OnLive! Technologies, Inc. */
118 	SBC24		=	0x0091, /* Siemens Business Communications Sys */
119 	DOLBY_AC3_SPDIF		=	0x0092, /* Sonic Foundry */
120 	MEDIASONIC_G723		=	0x0093, /* MediaSonic */
121 	PROSODY_8KBPS		=	0x0094, /* Aculab plc */
122 	ZYXEL_ADPCM	=	0x0097, /* ZyXEL Communications, Inc. */
123 	PHILIPS_LPCBB		=	0x0098, /* Philips Speech Processing */
124 	PACKED		=	0x0099, /* Studer Professional Audio AG */
125 	MALDEN_PHONYTALK	=	0x00A0, /* Malden Electronics Ltd. */
126 	RHETOREX_ADPCM		=	0x0100, /* Rhetorex Inc. */
127 	IRAT		=	0x0101, /* BeCubed Software Inc. */
128 	VIVO_G723	=	0x0111, /* Vivo Software */
129 	VIVO_SIREN	=	0x0112, /* Vivo Software */
130 	DIGITAL_G723=	0x0123, /* Digital Equipment Corporation */
131 	SANYO_LD_ADPCM		=	0x0125, /* Sanyo Electric Co., Ltd. */
132 	SIPROLAB_ACEPLNET	=	0x0130, /* Sipro Lab Telecom Inc. */
133 	SIPROLAB_ACELP4800	=	0x0131, /* Sipro Lab Telecom Inc. */
134 	SIPROLAB_ACELP8V3	=	0x0132, /* Sipro Lab Telecom Inc. */
135 	SIPROLAB_G729		=	0x0133, /* Sipro Lab Telecom Inc. */
136 	SIPROLAB_G729A		=	0x0134, /* Sipro Lab Telecom Inc. */
137 	SIPROLAB_KELVIN		=	0x0135, /* Sipro Lab Telecom Inc. */
138 	G726ADPCM	=	0x0140, /* Dictaphone Corporation */
139 	QUALCOMM_PUREVOICE	=	0x0150, /* Qualcomm, Inc. */
140 	QUALCOMM_HALFRATE	=	0x0151, /* Qualcomm, Inc. */
141 	TUBGSM		=	0x0155, /* Ring Zero Systems, Inc. */
142 	MSAUDIO1	=	0x0160, /* Microsoft Corporation */
143 	CREATIVE_ADPCM		=	0x0200, /* Creative Labs, Inc */
144 	CREATIVE_FASTSPEECH8=	0x0202, /* Creative Labs, Inc */
145 	CREATIVE_FASTSPEECH10	=	0x0203, /* Creative Labs, Inc */
146 	UHER_ADPCM	=	0x0210, /* UHER informatic GmbH */
147 	QUARTERDECK	=	0x0220, /* Quarterdeck Corporation */
148 	ILINK_VC	=	0x0230, /* I-link Worldwide */
149 	RAW_SPORT	=	0x0240, /* Aureal Semiconductor */
150 	IPI_HSX		=	0x0250, /* Interactive Products, Inc. */
151 	IPI_RPELP	=	0x0251, /* Interactive Products, Inc. */
152 	CS2			=	0x0260, /* Consistent Software */
153 	SONY_SCX	=	0x0270, /* Sony Corp. */
154 	FM_TOWNS_SND=	0x0300, /* Fujitsu Corp. */
155 	BTV_DIGITAL	=	0x0400, /* Brooktree Corporation */
156 	QDESIGN_MUSIC		=	0x0450, /* QDesign Corporation */
157 	VME_VMPCM	=	0x0680, /* AT&T Labs, Inc. */
158 	TPC			=	0x0681, /* AT&T Labs, Inc. */
159 	OLIGSM		=	0x1000, /* Ing C. Olivetti & C., S.p.A. */
160 	OLIADPCM	=	0x1001, /* Ing C. Olivetti & C., S.p.A. */
161 	OLICELP		=	0x1002, /* Ing C. Olivetti & C., S.p.A. */
162 	OLISBC		=	0x1003, /* Ing C. Olivetti & C., S.p.A. */
163 	OLIOPR		=	0x1004, /* Ing C. Olivetti & C., S.p.A. */
164 	LH_CODEC	=	0x1100, /* Lernout & Hauspie */
165 	WAVE_FORMAT_NORRIS	=	0x1400, /* Norris Communications, Inc. */
166 	SOUNDSPACE_MUSICOMPRESS	=	0x1500, /* AT&T Labs, Inc. */
167 	DVM		=	0x2000, /* FAST Multimedia AG */
168 }
169 
170 public class AudioFileException : Exception{
171 	@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
172     {
173         super(msg, file, line, next);
174     }
175 
176     @nogc @safe pure nothrow this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
177     {
178         super(msg, file, line, next);
179     }
180 }
181 
182 public @nogc:
183 	/**
184 	 * Returns the word lenght for the given codec type
185 	 */
186 	int getWordLength(CodecType codec){
187 		switch(codec){
188 			case CodecType.A_LAW_87_6, CodecType.MU_LAW, CodecType.SIGNED8BIT, CodecType.UNSIGNED8BIT: return 8;
189 			case CodecType.DIALOGIC_ADPCM, CodecType.IMA_ADPCM: return 4;
190 			case CodecType.SIGNED16BIT, CodecType.UNSIGNED16BIT: return 16;
191 			case CodecType.SIGNED24BIT, CodecType.UNSIGNED24BIT: return 24;
192 			case CodecType.SIGNED32BIT, CodecType.UNSIGNED32BIT, CodecType.FLOAT: return 32;
193 			case CodecType.COMPACT_ADPCM: return 2;
194 			default: return 0;
195 		}
196 	}
197 	/**
198 	 * Completely deallocates the memory for the given PCM data
199 	 */
200 	/*void deletePCMFromMemory(PCMFile* file){
201 		if(file.name){
202 			free(file.name);
203 		}
204 		if(file.startOfData){
205 			free(file.startOfData);
206 		}else{
207 			for(int i ; i < file.header.numOfChannels ; i++){
208 				free((*file.waveData + i).data);
209 				free(file.waveData + i);
210 			}
211 		}
212 		free(file);
213 	}*/