File: audioconvert.h

package info (click to toggle)
openalpp-cvs 20041206-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,796 kB
  • ctags: 549
  • sloc: cpp: 3,891; makefile: 154; ansic: 56; sh: 5
file content (164 lines) | stat: -rwxr-xr-x 5,790 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * Parts of this file are copied from OpenAL source code.
 * Copyright (C) Loki Games, licensed under the LGPL.
 *
 * This code has since been (more or less) modified to be a part of:
 * OpenAL++ - an object oriented toolkit for spatial sound
 * Copyright (C) 2002 VRlab, Ume University
 *
 * OpenAL++ was created using the libraries:
 *                 OpenAL (http://www.openal.org), 
 *              PortAudio (http://www.portaudio.com/), and
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */

#ifndef AUDIOCONVERT_H_INCLUDED
#define AUDIOCONVERT_H_INCLUDED

#include "openalpp/export.h"
#include <AL/al.h>
#include <stdlib.h>
#include <string.h>
#include "openalpp/error.h"

namespace openalpp {

/**
 * Class for converting audio.
 */
class OPENALPP_API AudioConvert {
  unsigned short channels_,bits_;
  unsigned int frequency_;
  ALenum format_;
 public:
  /**
   * Constructor.
   * @param format is the (OpenAL) format that data will be converted to.
   * @param frequency is the frequency the data will be converted to.
   */
  AudioConvert(ALenum format,unsigned int frequency);

  /**
   * Apply the conversion to data.
   * @param data is the data to convert.
   * @param format is the (OpenAL) format of the data.
   * @param frequency is the frequency of the data.
   * @param size is the size of the data. It will be updated to the new size.
   */
  void *apply(void *data,ALenum format,unsigned int frequency,unsigned int &size);
};

typedef struct _acAudioCVT {
  int needed;                     /* Set to 1 if conversion possible */
  ALushort src_format;            /* Source audio format */
  ALushort dst_format;            /* Target audio format */
  double rate_incr;               /* Rate conversion increment */
  void   *buf;                    /* Buffer to hold entire audio data */
  int    len;                     /* Length of original audio buffer */
  int    len_cvt;                 /* Length of converted audio buffer */
  int    len_mult;                /* buffer must be len*len_mult big */
  double len_ratio;       /* Given len, final size is len*len_ratio */
  void (*filters[10])(struct _acAudioCVT *cvt, ALushort format);
  int filter_index;               /* Current audio conversion function */
} acAudioCVT;

/* Audio format flags (defaults to LSB byte order) */
#define AUDIO_U8        0x0008  /* Unsigned 8-bit samples */
#define AUDIO_S8        0x8008  /* Signed 8-bit samples */
#define AUDIO_U16LSB    0x0010  /* Unsigned 16-bit samples */
#define AUDIO_S16LSB    0x8010  /* Signed 16-bit samples */
#define AUDIO_U16MSB    0x1010  /* As above, but big-endian byte order */
#define AUDIO_S16MSB    0x9010  /* As above, but big-endian byte order */

/* Native audio byte ordering */
#ifndef WORDS_BIGENDIAN
#define AUDIO_U16       AUDIO_U16LSB
#define AUDIO_S16       AUDIO_S16LSB
#define swap16le(x) (x)
#define swap32le(x) (x)
#define swap16be(x) swap16(x)
#define swap32be(x) swap32(x)
#else
#define AUDIO_U16       AUDIO_U16MSB
#define AUDIO_S16       AUDIO_S16MSB
#define swap16le(x) swap16(x)
#define swap32le(x) swap32(x)
#define swap16be(x) (x)
#define swap32be(x) (x)
#endif

#define _al_ALCHANNELS(fmt) ((fmt==AL_FORMAT_MONO16||fmt==AL_FORMAT_MONO8)?1:2)

#define DATA            0x61746164              /* "data" */
#define FACT            0x74636166              /* "fact" */
#define LIST            0x5453494c              /* "LIST" */
#define RIFF            0x46464952
#define WAVE            0x45564157
#define FMT             0x20746D66

#define AL_FORMAT_IMA_ADPCM_MONO16_EXT            0x10000
#define AL_FORMAT_IMA_ADPCM_STEREO16_EXT          0x10001
#define AL_FORMAT_WAVE_EXT                        0x10002

#define NELEMS(x) ((sizeof x) / (sizeof *x))

#define PCM_CODE        0x0001
#define MS_ADPCM_CODE   0x0002
#define IMA_ADPCM_CODE  0x0011

#define MS_ADPCM_max ((1<<(16-1))-1)
#define MS_ADPCM_min -(1<<(16-1))

typedef struct Chunk {
  ALuint magic;
  ALuint length;
  void *data;
} Chunk;

struct MS_ADPCM_decodestate_FULL {
  ALubyte hPredictor;
  ALushort iDelta;
  ALshort iSamp1;
  ALshort iSamp2;
};

typedef struct WaveFMT {
  ALushort encoding;
  ALushort channels;               /* 1 = mono, 2 = stereo */
  ALuint frequency;              /* One of 11025, 22050, or 44100 Hz */
  ALuint byterate;               /* Average bytes per second */
  ALushort blockalign;             /* Bytes per sample block */
  ALushort bitspersample;
} alWaveFMT_LOKI;

typedef struct IMA_ADPCM_decodestate_s {
  ALint valprev;  /* Previous output value */
  ALbyte index;           /* Index into stepsize table */
} alIMAADPCM_decodestate_LOKI;

typedef struct IMA_ADPCM_decoder {
  alWaveFMT_LOKI wavefmt;
  ALushort wSamplesPerBlock;
  alIMAADPCM_decodestate_LOKI state[2];
} alIMAADPCM_state_LOKI;
/*
void *AudioConvert(ALvoid *data,
		   ALenum f_format, ALuint f_size, ALuint f_freq,
		   ALenum t_format, ALuint t_freq, ALuint *retsize);
*/
}

#endif