| 1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
|---|
| 2 | * vi:set ts=8: |
|---|
| 3 | * |
|---|
| 4 | * al_ext_capture.c |
|---|
| 5 | * |
|---|
| 6 | * audio recording extension |
|---|
| 7 | * |
|---|
| 8 | */ |
|---|
| 9 | #include "al_siteconfig.h" |
|---|
| 10 | |
|---|
| 11 | #include <AL/al.h> |
|---|
| 12 | #include <AL/alc.h> |
|---|
| 13 | #include <stdio.h> |
|---|
| 14 | #include <stdlib.h> |
|---|
| 15 | #include <string.h> |
|---|
| 16 | |
|---|
| 17 | #include "al_ext_needed.h" |
|---|
| 18 | #include "al_ext_capture.h" |
|---|
| 19 | |
|---|
| 20 | #include "al_buffer.h" |
|---|
| 21 | #include "al_error.h" |
|---|
| 22 | #include "al_debug.h" |
|---|
| 23 | #include "al_mixer.h" |
|---|
| 24 | #include "al_source.h" |
|---|
| 25 | #include "alc/alc_device.h" |
|---|
| 26 | #include "alc/alc_context.h" |
|---|
| 27 | #include "al_types.h" |
|---|
| 28 | |
|---|
| 29 | #include "audioconvert/audioconvert.h" |
|---|
| 30 | |
|---|
| 31 | #include "backends/alc_backend.h" |
|---|
| 32 | #include "al_mutexlib.h" |
|---|
| 33 | |
|---|
| 34 | #ifdef OPENAL_EXTENSION |
|---|
| 35 | |
|---|
| 36 | /* |
|---|
| 37 | * we are not being build into the library, therefore define the |
|---|
| 38 | * table that informs openal how to register the extensions upon |
|---|
| 39 | * dlopen. |
|---|
| 40 | */ |
|---|
| 41 | struct { ALubyte *name; void *addr; } alExtension_03222001 [] = { |
|---|
| 42 | AL_EXT_PAIR(alCaptureInit_EXT), |
|---|
| 43 | AL_EXT_PAIR(alCaptureStart_EXT), |
|---|
| 44 | AL_EXT_PAIR(alCaptureStop_EXT), |
|---|
| 45 | AL_EXT_PAIR(alCaptureGetData_EXT), |
|---|
| 46 | AL_EXT_PAIR(alCaptureDestroy_EXT), |
|---|
| 47 | { NULL, NULL } |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | /* |
|---|
| 51 | * We don't need init or fini functions, but we might as well |
|---|
| 52 | * keep them in place if, in some distant future, they turn out |
|---|
| 53 | * to be useful. |
|---|
| 54 | */ |
|---|
| 55 | void alExtInit_03282000(void) { |
|---|
| 56 | fprintf(stderr, "alExtInit_03282000 STUB\n"); |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void alExtFini_03282000(void) { |
|---|
| 61 | fprintf(stderr, "alExtFini_03282000 STUB\n"); |
|---|
| 62 | return; |
|---|
| 63 | } |
|---|
| 64 | #endif /* OPENAL_EXTENSION */ |
|---|
| 65 | |
|---|
| 66 | void alInitCapture(void) { |
|---|
| 67 | return; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | void alFiniCapture(void) { |
|---|
| 71 | return; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | ALboolean alCaptureInit_EXT( UNUSED(ALenum format), |
|---|
| 75 | UNUSED(ALuint rate), |
|---|
| 76 | UNUSED(ALsizei bufferSize) ) |
|---|
| 77 | { |
|---|
| 78 | ALuint cid; |
|---|
| 79 | AL_context *cc; |
|---|
| 80 | AL_device *capture_device; |
|---|
| 81 | |
|---|
| 82 | /* get the current context */ |
|---|
| 83 | capture_device = NULL; |
|---|
| 84 | cid = _alcCCId; |
|---|
| 85 | _alcLockContext( cid ); |
|---|
| 86 | cc = _alcGetContext(cid); |
|---|
| 87 | if ( cc != NULL ) { |
|---|
| 88 | capture_device = cc->read_device; |
|---|
| 89 | if ( capture_device == NULL ) { |
|---|
| 90 | char spec[1024]; |
|---|
| 91 | const char *fmt="'( (direction \"read\") (sampling-rate %d))"; |
|---|
| 92 | |
|---|
| 93 | snprintf(spec, sizeof(spec), fmt, rate); |
|---|
| 94 | capture_device = alcOpenDevice((ALchar *)spec); |
|---|
| 95 | if ( capture_device ) { |
|---|
| 96 | _alcSetContext(NULL, cid, capture_device); |
|---|
| 97 | _alcDeviceSet(capture_device); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | _alcUnlockContext( cid ); |
|---|
| 102 | |
|---|
| 103 | return (capture_device != NULL); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | ALboolean alCaptureDestroy_EXT( void ) |
|---|
| 107 | { |
|---|
| 108 | ALuint cid; |
|---|
| 109 | AL_context *cc; |
|---|
| 110 | |
|---|
| 111 | /* get the current context */ |
|---|
| 112 | cid = _alcCCId; |
|---|
| 113 | _alcLockContext( cid ); |
|---|
| 114 | cc = _alcGetContext(cid); |
|---|
| 115 | if ( cc == NULL ) { |
|---|
| 116 | _alcUnlockContext( cid ); |
|---|
| 117 | return AL_FALSE; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if ( cc->read_device ) { |
|---|
| 121 | /* Only close it if we opened it originally */ |
|---|
| 122 | if (cc->write_device && (cc->read_device != cc->write_device)) { |
|---|
| 123 | alcCloseDevice(cc->read_device); |
|---|
| 124 | cc->read_device = NULL; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | _alcUnlockContext( cid ); |
|---|
| 128 | |
|---|
| 129 | return AL_TRUE; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | ALboolean alCaptureStart_EXT( void ) |
|---|
| 133 | { |
|---|
| 134 | return AL_FALSE; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | ALboolean alCaptureStop_EXT( void ) |
|---|
| 138 | { |
|---|
| 139 | return AL_FALSE; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | ALsizei alCaptureGetData_EXT( UNUSED(ALvoid* data), |
|---|
| 143 | ALsizei n, |
|---|
| 144 | UNUSED(ALenum format), |
|---|
| 145 | UNUSED(ALuint rate) ) |
|---|
| 146 | { |
|---|
| 147 | AL_device *dev; |
|---|
| 148 | ALuint size; |
|---|
| 149 | ALuint cid; |
|---|
| 150 | AL_context *cc; |
|---|
| 151 | |
|---|
| 152 | /* get the read device */ |
|---|
| 153 | cid = _alcCCId; |
|---|
| 154 | cc = _alcGetContext(cid); |
|---|
| 155 | if ( cc == NULL ) { |
|---|
| 156 | return 0; |
|---|
| 157 | } |
|---|
| 158 | dev = cc->read_device; |
|---|
| 159 | |
|---|
| 160 | if ( (dev->format == format) && (dev->speed == rate) ) { |
|---|
| 161 | size = _alcDeviceRead(cid, data, (ALuint)n); |
|---|
| 162 | } else { |
|---|
| 163 | ALuint samples; |
|---|
| 164 | void *temp; |
|---|
| 165 | |
|---|
| 166 | samples = n / (_alGetBitsFromFormat(format) / 8); |
|---|
| 167 | |
|---|
| 168 | /* Set size to the bytes of raw audio data we need */ |
|---|
| 169 | size = _al_PCMRatioify(rate, dev->speed, |
|---|
| 170 | format, dev->format, samples); |
|---|
| 171 | size *= (_alGetBitsFromFormat(dev->format) / 8); |
|---|
| 172 | |
|---|
| 173 | if ( n > (ALsizei)size ) |
|---|
| 174 | temp = malloc( (size_t)n ); |
|---|
| 175 | else |
|---|
| 176 | temp = malloc( (size_t)size ); |
|---|
| 177 | |
|---|
| 178 | if ( size > 0 ) { |
|---|
| 179 | size = _alcDeviceRead(cid, temp, size); |
|---|
| 180 | |
|---|
| 181 | temp = (void *)_alBufferCanonizeData(dev->format, |
|---|
| 182 | temp, |
|---|
| 183 | size, |
|---|
| 184 | dev->speed, |
|---|
| 185 | format, |
|---|
| 186 | rate, |
|---|
| 187 | &size, |
|---|
| 188 | AL_TRUE); |
|---|
| 189 | } else { |
|---|
| 190 | /* Hmm, zero size in record.. */ |
|---|
| 191 | memset(temp, 0, (size_t)n); |
|---|
| 192 | size = n; |
|---|
| 193 | } |
|---|
| 194 | if(temp == NULL) { |
|---|
| 195 | fprintf(stderr, "could not canonize data\n"); |
|---|
| 196 | return 0; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | memcpy(data, temp, size); |
|---|
| 200 | |
|---|
| 201 | free( temp ); |
|---|
| 202 | } |
|---|
| 203 | return size; |
|---|
| 204 | } |
|---|