1 1.22 mrg /* $NetBSD: libaudio.h,v 1.22 2024/03/08 06:57:59 mrg Exp $ */ 2 1.1 mrg 3 1.2 mrg /* 4 1.22 mrg * Copyright (c) 1999, 2009, 2013, 2015, 2019, 2024 Matthew R. Green 5 1.2 mrg * All rights reserved. 6 1.2 mrg * 7 1.2 mrg * Redistribution and use in source and binary forms, with or without 8 1.2 mrg * modification, are permitted provided that the following conditions 9 1.2 mrg * are met: 10 1.2 mrg * 1. Redistributions of source code must retain the above copyright 11 1.2 mrg * notice, this list of conditions and the following disclaimer. 12 1.2 mrg * 2. Redistributions in binary form must reproduce the above copyright 13 1.2 mrg * notice, this list of conditions and the following disclaimer in the 14 1.2 mrg * documentation and/or other materials provided with the distribution. 15 1.2 mrg * 16 1.2 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.2 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.2 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.2 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.2 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.2 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.2 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.2 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.2 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.2 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.2 mrg * SUCH DAMAGE. 27 1.2 mrg */ 28 1.1 mrg 29 1.1 mrg /* 30 1.9 mrg * audio formats 31 1.9 mrg */ 32 1.11 mrg #define AUDIO_FORMAT_DEFAULT -1 33 1.9 mrg #define AUDIO_FORMAT_NONE 1 34 1.9 mrg #define AUDIO_FORMAT_SUN 2 35 1.9 mrg #define AUDIO_FORMAT_WAV 3 36 1.9 mrg 37 1.9 mrg int audio_format_from_str (char *); 38 1.9 mrg 39 1.9 mrg /* 40 1.21 mrg * Audio encoding formats; this is a additional to those set 41 1.21 mrg * in sys/audioio.h, but with a large offset to avoid future 42 1.21 mrg * conflicts (additional ones are libaudio-software only.) 43 1.21 mrg * 44 1.21 mrg * This is to support floating-point WAV files. These require 45 1.21 mrg * software conversion to a supported format. 46 1.21 mrg */ 47 1.21 mrg #define AUDIO_ENCODING_LIBAUDIO_FLOAT32 1001 /* 32-bit IEEE FP. */ 48 1.21 mrg #define AUDIO_ENCODING_LIBAUDIO_FLOAT64 1002 /* 64-bit IEEE FP. */ 49 1.21 mrg 50 1.21 mrg /* 51 1.1 mrg * We copy the Sun/NeXT on-disk audio header format and document what 52 1.1 mrg * we know of it here. 53 1.1 mrg * 54 1.1 mrg * The header size appears to be an offset to where the data really 55 1.1 mrg * begins, rather than defining the real length of the audio header. 56 1.1 mrg * The Sun/NeXT audio format seems to only use 24 bytes of data (with 57 1.1 mrg * an additional 8 bytes of nuls written, padding it to 32 bytes). 58 1.1 mrg * 59 1.1 mrg * If the size of the audio data is unknown (eg, reading from a pipe) 60 1.1 mrg * the Sun demo audio tools place AUDIO_UNKNOWN_SIZE in the 61 1.1 mrg * `data_size' member. 62 1.1 mrg * 63 1.1 mrg * For stereo data, the channels appear to be interleaved with the 64 1.1 mrg * left channel first. For more channels, who knows? 65 1.1 mrg */ 66 1.1 mrg 67 1.1 mrg /* 68 1.1 mrg * This is the Sun/NeXT audio file magic value. Note that it 69 1.1 mrg * is also `.snd' in ASCII. 70 1.1 mrg */ 71 1.1 mrg #define AUDIO_FILE_MAGIC ((u_int32_t)0x2e736e64) 72 1.1 mrg #define AUDIO_UNKNOWN_SIZE ((unsigned)(~0)) 73 1.1 mrg 74 1.1 mrg typedef struct { 75 1.1 mrg u_int32_t magic; 76 1.1 mrg u_int32_t hdr_size; /* header size; in bytes */ 77 1.1 mrg u_int32_t data_size; /* optional; in bytes */ 78 1.1 mrg u_int32_t encoding; /* see below */ 79 1.1 mrg u_int32_t sample_rate; /* per second */ 80 1.1 mrg u_int32_t channels; /* number of interleaved channels */ 81 1.1 mrg } sun_audioheader; 82 1.1 mrg 83 1.1 mrg #define Audio_filehdr sun_audioheader /* SunOS compat(?) */ 84 1.1 mrg 85 1.1 mrg /* 86 1.1 mrg * these are the types of "encoding" for above. taken from the 87 1.1 mrg * SunOS <multimedia/audio_filehdr.h>. 88 1.1 mrg */ 89 1.1 mrg #define AUDIO_FILE_ENCODING_MULAW_8 1 90 1.1 mrg #define AUDIO_FILE_ENCODING_LINEAR_8 2 91 1.1 mrg #define AUDIO_FILE_ENCODING_LINEAR_16 3 92 1.1 mrg #define AUDIO_FILE_ENCODING_LINEAR_24 4 93 1.1 mrg #define AUDIO_FILE_ENCODING_LINEAR_32 5 94 1.1 mrg #define AUDIO_FILE_ENCODING_FLOAT 6 95 1.1 mrg #define AUDIO_FILE_ENCODING_DOUBLE 7 96 1.1 mrg #define AUDIO_FILE_ENCODING_ADPCM_G721 23 97 1.1 mrg #define AUDIO_FILE_ENCODING_ADPCM_G722 24 98 1.1 mrg #define AUDIO_FILE_ENCODING_ADPCM_G723_3 25 99 1.1 mrg #define AUDIO_FILE_ENCODING_ADPCM_G723_5 26 100 1.1 mrg #define AUDIO_FILE_ENCODING_ALAW_8 27 101 1.1 mrg 102 1.10 mrg const char *audio_enc_from_val (int); 103 1.6 mrg int audio_enc_to_val (const char *); 104 1.1 mrg 105 1.14 mrg int audio_sun_to_encoding (int, u_int *, u_int *); 106 1.6 mrg int audio_encoding_to_sun (int, int, int *); 107 1.1 mrg 108 1.1 mrg /* 109 1.22 mrg * RIFF WAVE files. Sources: RFC 2361, and various Microsoft docs 110 1.22 mrg * https://learn.microsoft.com/en-us/windows/win32/xaudio2/resource-interchange-file-format--riff- 111 1.22 mrg * https://learn.microsoft.com/en-us/previous-versions/windows/hardware/design/dn653308(v=vs.85) 112 1.22 mrg * "Multimedia Programming Interface and Data Specifications 1.0" chapter 4 113 1.1 mrg */ 114 1.1 mrg 115 1.1 mrg /* 116 1.1 mrg * This is the WAV audio file magic value. Note that it 117 1.1 mrg * is also `RIFF' and `WAVE' in ASCII. 118 1.1 mrg */ 119 1.1 mrg #define WAVAUDIO_FILE_MAGIC_RIFF ((u_int32_t)0x52494646) 120 1.1 mrg #define WAVAUDIO_FILE_MAGIC_WAVE ((u_int32_t)0x57415645) 121 1.1 mrg #define WAVAUDIO_FILE_MAGIC_FMT ((u_int32_t)0x666d7420) 122 1.1 mrg #define WAVAUDIO_FILE_MAGIC_DATA ((u_int32_t)0x64617461) 123 1.1 mrg 124 1.22 mrg /* From RFC 2361 */ 125 1.1 mrg #define WAVE_FORMAT_UNKNOWN (0x0000) 126 1.1 mrg #define WAVE_FORMAT_PCM (0x0001) 127 1.1 mrg #define WAVE_FORMAT_ADPCM (0x0002) 128 1.21 mrg #define WAVE_FORMAT_IEEE_FLOAT (0x0003) 129 1.22 mrg #define WAVE_FORMAT_VSELP (0x0004) 130 1.22 mrg #define WAVE_FORMAT_IBM_CVSD (0x0005) 131 1.1 mrg #define WAVE_FORMAT_ALAW (0x0006) 132 1.1 mrg #define WAVE_FORMAT_MULAW (0x0007) 133 1.1 mrg #define WAVE_FORMAT_OKI_ADPCM (0x0010) 134 1.16 mrg #define WAVE_FORMAT_IMA_ADPCM (0x0011) 135 1.22 mrg #define WAVE_FORMAT_MEDIASPACE_ADPCM (0x0012) 136 1.22 mrg #define WAVE_FORMAT_SIERRA_ADPCM (0x0013) 137 1.22 mrg #define WAVE_FORMAT_G723_ADPCM (0x0014) 138 1.1 mrg #define WAVE_FORMAT_DIGISTD (0x0015) 139 1.1 mrg #define WAVE_FORMAT_DIGIFIX (0x0016) 140 1.22 mrg #define WAVE_FORMAT_DIALOGIC_OKI_ADPCM (0x0017) 141 1.22 mrg #define WAVE_FORMAT_MEDIAVISION_ADPCM (0x0018) 142 1.22 mrg #define WAVE_FORMAT_CU_CODEC (0x0019) 143 1.22 mrg #define WAVE_FORMAT_YAMAHA_ADPCM (0x0020) 144 1.22 mrg #define WAVE_FORMAT_SONARC (0x0021) 145 1.22 mrg #define WAVE_FORMAT_DSPGROUP_TRUESPEECH (0x0022) 146 1.22 mrg #define WAVE_FORMAT_ECHOSC1 (0x0023) 147 1.22 mrg #define WAVE_FORMAT_AUDIOFILE_AF36 (0x0024) 148 1.22 mrg #define WAVE_FORMAT_APTX (0x0025) 149 1.22 mrg #define WAVE_FORMAT_AUDIOFILE_AF10 (0x0026) 150 1.22 mrg #define WAVE_FORMAT_PROSODY_1612 (0x0027) 151 1.22 mrg #define WAVE_FORMAT_LRC (0x0028) 152 1.16 mrg #define WAVE_FORMAT_DOLBY_AC2 (0x0030) 153 1.16 mrg #define WAVE_FORMAT_GSM610 (0x0031) 154 1.22 mrg #define WAVE_FORMAT_MSNAUDIO (0x0032) 155 1.22 mrg #define WAVE_FORMAT_ANTEX_ADPCME (0x0033) 156 1.22 mrg #define WAVE_FORMAT_CONTROL_RES_VQLPC (0x0034) 157 1.22 mrg #define WAVE_FORMAT_DIGIREAL (0x0035) 158 1.22 mrg #define WAVE_FORMAT_DIGIADPCM (0x0036) 159 1.22 mrg #define WAVE_FORMAT_CONTROL_RES_CR10 (0x0037) 160 1.22 mrg #define WAVE_FORMAT_NMS_VBXADPCM (0x0038) 161 1.22 mrg #define WAVE_FORMAT_ROLAND_RDAC (0x0039) 162 1.22 mrg #define WAVE_FORMAT_ECHOSC3 (0x003a) 163 1.16 mrg #define WAVE_FORMAT_ROCKWELL_ADPCM (0x003b) 164 1.16 mrg #define WAVE_FORMAT_ROCKWELL_DIGITALK (0x003c) 165 1.22 mrg #define WAVE_FORMAT_XEBEC (0x003d) 166 1.16 mrg #define WAVE_FORMAT_G721_ADPCM (0x0040) 167 1.16 mrg #define WAVE_FORMAT_G728_CELP (0x0041) 168 1.22 mrg #define WAVE_FORMAT_MSG723 (0x0042) 169 1.16 mrg #define WAVE_FORMAT_MPEG (0x0050) 170 1.22 mrg #define WAVE_FORMAT_RT24 (0x0052) 171 1.22 mrg #define WAVE_FORMAT_PAC (0x0053) 172 1.16 mrg #define WAVE_FORMAT_MPEGLAYER3 (0x0055) 173 1.22 mrg #define WAVE_FORMAT_LUCENT_G723 (0x0059) 174 1.22 mrg #define WAVE_FORMAT_CIRRUS (0x0060) 175 1.22 mrg #define WAVE_FORMAT_ESPCM (0x0061) 176 1.22 mrg #define WAVE_FORMAT_VOXWARE (0x0062) 177 1.22 mrg #define WAVE_FORMAT_CANOPUS_ATRAC (0x0063) 178 1.16 mrg #define WAVE_FORMAT_G726_ADPCM (0x0064) 179 1.16 mrg #define WAVE_FORMAT_G722_ADPCM (0x0065) 180 1.22 mrg #define WAVE_FORMAT_DSAT (0x0066) 181 1.22 mrg #define WAVE_FORMAT_DSAT_DISPLAY (0x0067) 182 1.22 mrg #define WAVE_FORMAT_VOXWARE_BYTE_ALIGNED (0x0069) 183 1.22 mrg #define WAVE_FORMAT_VOXWARE_AC8 (0x0070) 184 1.22 mrg #define WAVE_FORMAT_VOXWARE_AC10 (0x0071) 185 1.22 mrg #define WAVE_FORMAT_VOXWARE_AC16 (0x0072) 186 1.22 mrg #define WAVE_FORMAT_VOXWARE_AC20 (0x0073) 187 1.22 mrg #define WAVE_FORMAT_VOXWARE_RT24 (0x0074) 188 1.22 mrg #define WAVE_FORMAT_VOXWARE_RT29 (0x0075) 189 1.22 mrg #define WAVE_FORMAT_VOXWARE_RT29HW (0x0076) 190 1.22 mrg #define WAVE_FORMAT_VOXWARE_VR12 (0x0077) 191 1.22 mrg #define WAVE_FORMAT_VOXWARE_VR18 (0x0078) 192 1.22 mrg #define WAVE_FORMAT_VOXWARE_TQ40 (0x0079) 193 1.22 mrg #define WAVE_FORMAT_SOFTSOUND (0x0080) 194 1.22 mrg #define WAVE_FORMAT_VOXWARE_TQ60 (0x0081) 195 1.22 mrg #define WAVE_FORMAT_MSRT24 (0x0082) 196 1.22 mrg #define WAVE_FORMAT_G729A (0x0083) 197 1.22 mrg #define WAVE_FORMAT_MVI_MV12 (0x0084) 198 1.22 mrg #define WAVE_FORMAT_DF_G726 (0x0085) 199 1.22 mrg #define WAVE_FORMAT_DF_GSM610 (0x0086) 200 1.22 mrg #define WAVE_FORMAT_ISIAUDIO (0x0088) 201 1.22 mrg #define WAVE_FORMAT_ONLIVE (0x0089) 202 1.22 mrg #define WAVE_FORMAT_SBC24 (0x0091) 203 1.22 mrg #define WAVE_FORMAT_DOLBY_AC3_SPDIF (0x0092) 204 1.22 mrg #define WAVE_FORMAT_ZYXEL_ADPCM (0x0097) 205 1.22 mrg #define WAVE_FORMAT_PHILIPS_LPCBB (0x0098) 206 1.22 mrg #define WAVE_FORMAT_PACKED (0x0099) 207 1.22 mrg #define WAVE_FORMAT_RHETOREX_ADPCM (0x0100) 208 1.22 mrg #define WAVE_FORMAT_IRAT (0x0101) 209 1.22 mrg #define WAVE_FORMAT_VIVO_G723 (0x0111) 210 1.22 mrg #define WAVE_FORMAT_VIVO_SIREN (0x0112) 211 1.22 mrg #define WAVE_FORMAT_DIGITAL_G723 (0x0123) 212 1.22 mrg #define WAVE_FORMAT_CREATIVE_ADPCM (0x0200) 213 1.22 mrg #define WAVE_FORMAT_CREATIVE_FASTSPEECH8 (0x0202) 214 1.22 mrg #define WAVE_FORMAT_CREATIVE_FASTSPEECH10 (0x0203) 215 1.22 mrg #define WAVE_FORMAT_QUARTERDECK (0x0220) 216 1.22 mrg #define WAVE_FORMAT_FM_TOWNS_SND (0x0300) 217 1.22 mrg #define WAVE_FORMAT_BTV_DIGITAL (0x0400) 218 1.22 mrg #define WAVE_FORMAT_VME_VMPCM (0x0680) 219 1.22 mrg #define WAVE_FORMAT_OLIGSM (0x1000) 220 1.22 mrg #define WAVE_FORMAT_OLIADPCM (0x1001) 221 1.22 mrg #define WAVE_FORMAT_OLICELP (0x1002) 222 1.22 mrg #define WAVE_FORMAT_OLISBC (0x1003) 223 1.22 mrg #define WAVE_FORMAT_OLIOPR (0x1004) 224 1.22 mrg #define WAVE_FORMAT_LH_CODEC (0x1100) 225 1.22 mrg #define WAVE_FORMAT_NORRIS (0x1400) 226 1.22 mrg #define WAVE_FORMAT_ISIAUDIO2 (0x1401) 227 1.22 mrg #define WAVE_FORMAT_SOUNDSPACE_MUSICOMPRESS (0x1500) 228 1.22 mrg #define WAVE_FORMAT_DVM (0x2000) 229 1.22 mrg 230 1.16 mrg #define WAVE_FORMAT_EXTENSIBLE (0xfffe) 231 1.1 mrg 232 1.10 mrg const char *wav_enc_from_val (int); 233 1.9 mrg 234 1.1 mrg typedef struct { 235 1.1 mrg char name[4]; 236 1.1 mrg u_int32_t len; 237 1.1 mrg } wav_audioheaderpart; 238 1.1 mrg 239 1.1 mrg typedef struct { 240 1.1 mrg u_int16_t tag; 241 1.1 mrg u_int16_t channels; 242 1.1 mrg u_int32_t sample_rate; 243 1.22 mrg u_int32_t avg_bytes_per_sec; 244 1.1 mrg u_int16_t alignment; 245 1.1 mrg u_int16_t bits_per_sample; 246 1.13 drochner } __packed wav_audioheaderfmt; 247 1.1 mrg 248 1.16 mrg typedef struct { 249 1.16 mrg u_int16_t len; 250 1.16 mrg u_int16_t valid_bits; 251 1.16 mrg u_int32_t speaker_pos_mask; 252 1.16 mrg u_int16_t sub_tag; 253 1.22 mrg u_int8_t guid[14]; 254 1.16 mrg } __packed wav_audiohdrextensible; 255 1.16 mrg 256 1.8 mrg /* returns size of header, or -ve for failure */ 257 1.20 mrg ssize_t audio_wav_parse_hdr (void *, size_t, u_int *, u_int *, u_int *, u_int *, off_t *); 258 1.1 mrg 259 1.17 joerg extern int verbose; 260 1.17 joerg 261 1.1 mrg /* 262 1.1 mrg * audio routine error codes 263 1.1 mrg */ 264 1.1 mrg #define AUDIO_ENOENT -1 /* no such audio format */ 265 1.1 mrg #define AUDIO_ESHORTHDR -2 /* short header */ 266 1.1 mrg #define AUDIO_EWAVUNSUPP -3 /* WAV: unsupported file */ 267 1.1 mrg #define AUDIO_EWAVBADPCM -4 /* WAV: bad PCM bps */ 268 1.1 mrg #define AUDIO_EWAVNODATA -5 /* WAV: missing data */ 269 1.9 mrg #define AUDIO_EINTERNAL -6 /* internal error */ 270 1.1 mrg 271 1.1 mrg #define AUDIO_MAXERRNO 5 272 1.1 mrg 273 1.1 mrg /* and something to get a string associated with this error */ 274 1.6 mrg const char *audio_errstring (int); 275 1.1 mrg 276 1.1 mrg /* 277 1.1 mrg * generic routines? 278 1.1 mrg */ 279 1.6 mrg void decode_int (const char *, int *); 280 1.19 mrg void decode_uint (const char *, unsigned *); 281 1.6 mrg void decode_time (const char *, struct timeval *); 282 1.6 mrg void decode_encoding (const char *, int *); 283 1.1 mrg 284 1.1 mrg /* 285 1.20 mrg * Track info, for reading/writing sun/wav header. 286 1.18 mrg * 287 1.18 mrg * Note that write_header() may change the values of format, 288 1.18 mrg * encoding. 289 1.18 mrg */ 290 1.18 mrg 291 1.20 mrg struct track_info { 292 1.18 mrg int outfd; 293 1.18 mrg char *header_info; 294 1.18 mrg int format; 295 1.18 mrg int encoding; 296 1.18 mrg int precision; 297 1.18 mrg int qflag; 298 1.20 mrg off_t total_size; 299 1.18 mrg int sample_rate; 300 1.18 mrg int channels; 301 1.18 mrg }; 302 1.18 mrg 303 1.18 mrg typedef void (*write_conv_func) (u_char *, int); 304 1.18 mrg 305 1.20 mrg void write_header (struct track_info *); 306 1.20 mrg write_conv_func write_get_conv_func(struct track_info *); 307 1.18 mrg 308 1.18 mrg /* backends for the above */ 309 1.20 mrg int sun_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp); 310 1.20 mrg int wav_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp); 311 1.20 mrg write_conv_func sun_write_get_conv_func(struct track_info *ti); 312 1.20 mrg write_conv_func wav_write_get_conv_func(struct track_info *ti); 313 1.18 mrg 314 1.18 mrg extern char audio_default_info[8]; 315 1.18 mrg 316 1.18 mrg /* 317 1.1 mrg * get/put 16/32 bits of big/little endian data 318 1.1 mrg */ 319 1.1 mrg #include <sys/types.h> 320 1.1 mrg #include <machine/endian.h> 321 1.1 mrg #include <machine/bswap.h> 322 1.1 mrg 323 1.1 mrg #if BYTE_ORDER == BIG_ENDIAN 324 1.1 mrg 325 1.1 mrg #define getle16(v) bswap16(v) 326 1.1 mrg #define getle32(v) bswap32(v) 327 1.1 mrg #define getbe16(v) (v) 328 1.1 mrg #define getbe32(v) (v) 329 1.1 mrg 330 1.9 mrg #define putle16(x,v) (x) = bswap16(v) 331 1.9 mrg #define putle32(x,v) (x) = bswap32(v) 332 1.9 mrg #define putbe16(x,v) (x) = (v) 333 1.9 mrg #define putbe32(x,v) (x) = (v) 334 1.9 mrg 335 1.1 mrg #else 336 1.1 mrg 337 1.1 mrg #define getle16(v) (v) 338 1.1 mrg #define getle32(v) (v) 339 1.1 mrg #define getbe16(v) bswap16(v) 340 1.1 mrg #define getbe32(v) bswap32(v) 341 1.9 mrg 342 1.9 mrg #define putle16(x,v) (x) = (v) 343 1.9 mrg #define putle32(x,v) (x) = (v) 344 1.9 mrg #define putbe16(x,v) (x) = bswap16(v) 345 1.9 mrg #define putbe32(x,v) (x) = bswap32(v) 346 1.1 mrg 347 1.1 mrg #endif 348