paqa_tools.c (4432B)
1 2 /* 3 * PortAudio Portable Real-Time Audio Library 4 * Latest Version at: http://www.portaudio.com 5 * 6 * Copyright (c) 1999-2010 Phil Burk and Ross Bencina 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining 9 * a copy of this software and associated documentation files 10 * (the "Software"), to deal in the Software without restriction, 11 * including without limitation the rights to use, copy, modify, merge, 12 * publish, distribute, sublicense, and/or sell copies of the Software, 13 * and to permit persons to whom the Software is furnished to do so, 14 * subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 28 /* 29 * The text above constitutes the entire PortAudio license; however, 30 * the PortAudio community also makes the following non-binding requests: 31 * 32 * Any person wishing to distribute modifications to the Software is 33 * requested to send the modifications to the original developer so that 34 * they can be incorporated into the canonical version. It is also 35 * requested that these non-binding requests be included along with the 36 * license above. 37 */ 38 39 #include "paqa_tools.h" 40 41 42 /*******************************************************************/ 43 void PaQa_ListAudioDevices(void) 44 { 45 int i, numDevices; 46 const PaDeviceInfo *deviceInfo; 47 numDevices = Pa_GetDeviceCount(); 48 for( i=0; i<numDevices; i++ ) 49 { 50 deviceInfo = Pa_GetDeviceInfo( i ); 51 printf( "#%d: ", i ); 52 printf( "%2d in", deviceInfo->maxInputChannels ); 53 printf( ", %2d out", deviceInfo->maxOutputChannels ); 54 printf( ", %s", deviceInfo->name ); 55 printf( ", on %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name ); 56 } 57 } 58 59 /*******************************************************************/ 60 void PaQa_ConvertToFloat( const void *input, int numSamples, PaSampleFormat inFormat, float *output ) 61 { 62 int i; 63 switch( inFormat ) 64 { 65 case paUInt8: 66 { 67 unsigned char *data = (unsigned char *)input; 68 for( i=0; i<numSamples; i++ ) 69 { 70 int value = *data++; 71 value -= 128; 72 *output++ = value / 128.0f; 73 } 74 } 75 break; 76 77 case paInt8: 78 { 79 char *data = (char *)input; 80 for( i=0; i<numSamples; i++ ) 81 { 82 int value = *data++; 83 *output++ = value / 128.0f; 84 } 85 } 86 break; 87 88 case paInt16: 89 { 90 short *data = (short *)input; 91 for( i=0; i<numSamples; i++ ) 92 { 93 *output++ = *data++ / 32768.0f; 94 } 95 } 96 break; 97 98 case paInt32: 99 { 100 int *data = (int *)input; 101 for( i=0; i<numSamples; i++ ) 102 { 103 int value = (*data++) >> 8; 104 float fval = (float) (value / ((double) 0x00800000)); 105 *output++ = fval; 106 } 107 } 108 break; 109 } 110 111 } 112 113 /*******************************************************************/ 114 void PaQa_ConvertFromFloat( const float *input, int numSamples, PaSampleFormat outFormat, void *output ) 115 { 116 int i; 117 switch( outFormat ) 118 { 119 case paUInt8: 120 { 121 unsigned char *data = (unsigned char *)output; 122 for( i=0; i<numSamples; i++ ) 123 { 124 float value = *input++; 125 int byte = ((int) (value * 127)) + 128; 126 *data++ = (unsigned char) byte; 127 } 128 } 129 break; 130 131 case paInt8: 132 { 133 char *data = (char *)output; 134 for( i=0; i<numSamples; i++ ) 135 { 136 float value = *input++; 137 int byte = (int) (value * 127); 138 *data++ = (char) byte; 139 } 140 } 141 break; 142 143 case paInt16: 144 { 145 short *data = (short *)output; 146 for( i=0; i<numSamples; i++ ) 147 { 148 float value = *input++; 149 // Use assymmetric conversion to avoid clipping. 150 short sval = value * 32767.0; 151 *data++ = sval; 152 } 153 } 154 break; 155 156 case paInt32: 157 { 158 int *data = (int *)output; 159 for( i=0; i<numSamples; i++ ) 160 { 161 float value = *input++; 162 // Use assymmetric conversion to avoid clipping. 163 int ival = value * ((double) 0x007FFFF0); 164 ival = ival << 8; 165 *data++ = ival; 166 } 167 } 168 break; 169 } 170 171 }