|
|
1.1 root 1: /*
2: Hatari - microphone.c
3: microphone (jack connector) emulation (Falcon mode only)
4:
5: This file is distributed under the GNU Public License, version 2 or at
6: your option any later version. Read the file gpl.txt for details.
7:
8: This program uses the PortAudio Portable Audio Library.
9: For more information see: http://www.portaudio.com
10: Copyright (c) 1999-2000 Ross Bencina and Phil Burk
11: */
12:
13: #include "main.h"
14:
15: #if HAVE_PORTAUDIO
16:
17: #include <portaudio.h>
18: #include "microphone.h"
19: #include "crossbar.h"
20:
21:
22: #define FRAMES_PER_BUFFER (64)
23:
24: /* Static functions */
25: static void Microphone_Error (void);
26: static int Microphone_Terminate(void);
27:
28: static int Microphone_Callback (const void *inputBuffer, void *outputBuffer,
29: unsigned long framesPerBuffer,
30: const PaStreamCallbackTimeInfo* timeInfo,
31: PaStreamCallbackFlags statusFlags,
32: void *userData);
33:
34: /* Static datas */
35: static PaStreamParameters micro_inputParameters;
36: static PaStream *micro_stream;
37: static PaError micro_err;
38:
39: static int micro_sampleRate;
40: static Sint16 micro_buffer_L[FRAMES_PER_BUFFER]; /* left buffer */
41: static Sint16 micro_buffer_R[FRAMES_PER_BUFFER]; /* right buffer */
42:
43:
44: /* This routine will be called by the PortAudio engine when audio is needed.
45: ** It may be called at interrupt level on some machines so don't do anything
46: ** that could mess up the system like calling malloc() or free().
47: */
48: static int Microphone_Callback (const void *inputBuffer, void *outputBuffer,
49: unsigned long framesPerBuffer,
50: const PaStreamCallbackTimeInfo* timeInfo,
51: PaStreamCallbackFlags statusFlags,
52: void *userData)
53: {
54: unsigned int i;
55: const Sint16 *in = inputBuffer;
56:
57: Sint16 *out_L = (Sint16*)micro_buffer_L;
58: Sint16 *out_R = (Sint16*)micro_buffer_R;
59:
60: if (inputBuffer == NULL) {
61: memset(micro_buffer_L, 0, sizeof(micro_buffer_L));
62: memset(micro_buffer_R, 0, sizeof(micro_buffer_R));
63: }
64: else {
65: for (i=0; i<framesPerBuffer; i++) {
66: *out_L ++ = *in++; /* left data */
67: *out_R ++ = *in++; /* right data */
68: }
69: }
70:
71: /* send buffer to crossbar */
72: Crossbar_GetMicrophoneDatas(micro_buffer_L, micro_buffer_R, framesPerBuffer);
73:
74: /* get Next Microphone datas */
75: return paContinue;
76: }
77:
78:
79: /*******************************************************************/
80:
81: /**
82: * Microphone (jack) inits : init portaudio microphone emulation
83: * - sampleRate : system sound frequency
84: */
85: int Microphone_Start(int sampleRate)
86: {
87: micro_sampleRate = sampleRate;
88:
89: /* Initialize portaudio */
90: micro_err = Pa_Initialize();
91: if (micro_err != paNoError) {
92: Microphone_Error();
93: return 0;
94: }
95:
96: /* Initialize microphone parameters */
97: micro_inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
98: if (micro_inputParameters.device == paNoDevice) {
99: fprintf (stderr, "Microphone: No input device found.\n");
100: Microphone_Terminate();
101: return 0;
102: }
103:
104: micro_inputParameters.channelCount = 2; /* stereo input */
105: micro_inputParameters.sampleFormat = paInt16; /* 16 bits sound */
106: micro_inputParameters.suggestedLatency = Pa_GetDeviceInfo (micro_inputParameters.device)->defaultLowInputLatency;
107: micro_inputParameters.hostApiSpecificStreamInfo = NULL;
108:
109: /* Open Microphone stream */
110: micro_err = Pa_OpenStream (
111: µ_stream,
112: µ_inputParameters,
113: NULL,
114: micro_sampleRate,
115: FRAMES_PER_BUFFER,
116: 0, /* paClipOff, we won't output out of range samples so don't bother clipping them */
117: Microphone_Callback,
118: NULL);
119: if (micro_err != paNoError) {
120: Microphone_Error();
121: return 0;
122: }
123:
124: /* Start microphone recording */
125: micro_err = Pa_StartStream( micro_stream );
126: if (micro_err != paNoError) {
127: Microphone_Error();
128: return 0;
129: }
130:
131: return 1;
132: }
133:
134: /**
135: * Microphone (jack) Error : display current portaudio error
136: */
137: static void Microphone_Error(void)
138: {
139: fprintf (stderr, "An error occured while using the portaudio stream\n");
140: fprintf (stderr, "Error number: %d\n", micro_err);
141: fprintf (stderr, "Error message: %s\n", Pa_GetErrorText (micro_err));
142:
143: Microphone_Terminate();
144: }
145:
146: /**
147: * Microphone (jack) Terminate : terminate the microphone emulation
148: */
149: static int Microphone_Terminate(void)
150: {
151: micro_err = Pa_Terminate();
152: if (micro_err != paNoError) {
153: fprintf (stderr, "PortAudio error: %s\n", Pa_GetErrorText(micro_err));
154: return -1;
155: }
156: return 0;
157: }
158:
159: /**
160: * Microphone (jack) stop : stops the current recording
161: */
162: int Microphone_Stop(void)
163: {
164: /* Close Microphone stream */
165: micro_err = Pa_CloseStream(micro_stream);
166: if (micro_err != paNoError) {
167: Microphone_Error();
168: return -1;
169: }
170:
171: return Microphone_Terminate();
172: }
173:
174: #endif /* HAVE PORTAUDIO */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.