diff options
Diffstat (limited to 'src/test/portaudiotest.cpp')
-rw-r--r-- | src/test/portaudiotest.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/test/portaudiotest.cpp b/src/test/portaudiotest.cpp new file mode 100644 index 0000000..58f6cac --- /dev/null +++ b/src/test/portaudiotest.cpp | |||
@@ -0,0 +1,158 @@ | |||
1 | using namespace std; | ||
2 | #include <iostream> | ||
3 | #include <portaudio.h> | ||
4 | #include "portaudiotest.h" | ||
5 | #define PA_FRAMESPERBUF 512 | ||
6 | #define __uint32 unsigned long | ||
7 | |||
8 | int HD24UserInterface::portaudio_process( | ||
9 | const void *inputBuffer, | ||
10 | void *outputBuffer, | ||
11 | __uint32 nframes, | ||
12 | const PaStreamCallbackTimeInfo* timeinfo, | ||
13 | PaStreamCallbackFlags, | ||
14 | void *userData) | ||
15 | { | ||
16 | HD24UserInterface* mythis=(HD24UserInterface*)userData; | ||
17 | if (!mythis->havestreamtime) | ||
18 | { | ||
19 | mythis->streamtime=0; | ||
20 | mythis->timeoffset=timeinfo->currentTime; | ||
21 | mythis->havestreamtime=true; | ||
22 | } | ||
23 | mythis->streamtime+=nframes; | ||
24 | cout << ((mythis->streamtime)/48000) << " nframes=" << nframes <<" streamtime=" << timeinfo->currentTime-(mythis->timeoffset) << endl; | ||
25 | float sval=1; | ||
26 | for (int i=0;i<nframes;i++) | ||
27 | { | ||
28 | if ((i%16)==0) sval=-sval; | ||
29 | ((float*)(outputBuffer))[i]=sval; | ||
30 | } | ||
31 | return paContinue; | ||
32 | } | ||
33 | |||
34 | |||
35 | void HD24UserInterface::portaudioinit() | ||
36 | { | ||
37 | cout << "portaudioinit" << endl; | ||
38 | PaError err; | ||
39 | inputParameters=new PaStreamParameters; | ||
40 | outputParameters=new PaStreamParameters; | ||
41 | err=Pa_Initialize(); | ||
42 | if (err!=paNoError) | ||
43 | { | ||
44 | Pa_Terminate(); | ||
45 | return; | ||
46 | } | ||
47 | m_isportaudioinitialized=true; | ||
48 | } | ||
49 | |||
50 | HD24UserInterface::HD24UserInterface() | ||
51 | { | ||
52 | m_isportaudioinitialized=false; | ||
53 | portaudiostream=NULL; | ||
54 | return; | ||
55 | } | ||
56 | |||
57 | bool HD24UserInterface::isportaudioinitialized() | ||
58 | { | ||
59 | return m_isportaudioinitialized; | ||
60 | } | ||
61 | |||
62 | void HD24UserInterface::portaudio_transport_start() | ||
63 | { | ||
64 | |||
65 | cout << "portaudio transport start" << endl; | ||
66 | |||
67 | if (!isportaudioinitialized()) | ||
68 | { | ||
69 | PaError err=Pa_Initialize(); | ||
70 | if (err != paNoError) | ||
71 | { | ||
72 | cout << "Cannot initialize portaudio- exiting." << endl; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | if (isportaudioinitialized() && (portaudiostream!=NULL)) | ||
77 | { | ||
78 | cout << "already have stream- done starting" << endl; | ||
79 | return; | ||
80 | } | ||
81 | |||
82 | PaDeviceIndex indevice=Pa_GetDefaultInputDevice(); | ||
83 | bzero(inputParameters,sizeof(*inputParameters)); | ||
84 | inputParameters->device=indevice; | ||
85 | inputParameters->channelCount=1; | ||
86 | inputParameters->sampleFormat=paFloat32; | ||
87 | inputParameters->suggestedLatency = Pa_GetDeviceInfo( inputParameters->device )->defaultLowInputLatency; | ||
88 | inputParameters->hostApiSpecificStreamInfo = NULL; | ||
89 | |||
90 | cout << "Input params set" << endl; | ||
91 | cout << "Device=" << inputParameters->device << endl; | ||
92 | cout << "Channelcount=" << inputParameters->channelCount << endl; | ||
93 | cout << "sampleFormat=" << inputParameters->sampleFormat << endl; | ||
94 | cout << "suggestedlatency=" << inputParameters->suggestedLatency << endl; | ||
95 | cout << "================================="<<endl; | ||
96 | bzero(outputParameters,sizeof(*outputParameters)); | ||
97 | PaDeviceIndex outdevice=Pa_GetDefaultOutputDevice(); | ||
98 | outputParameters->device=outdevice; | ||
99 | outputParameters->channelCount=1; | ||
100 | outputParameters->sampleFormat=paFloat32; | ||
101 | outputParameters->suggestedLatency = Pa_GetDeviceInfo( outputParameters->device )->defaultLowOutputLatency; | ||
102 | outputParameters->hostApiSpecificStreamInfo = NULL; | ||
103 | |||
104 | cout << "Output params set" << endl; | ||
105 | cout << "Device=" << outputParameters->device << endl; | ||
106 | cout << "Channelcount=" << outputParameters->channelCount << endl; | ||
107 | cout << "sampleFormat=" << outputParameters->sampleFormat << endl; | ||
108 | cout << "suggestedlatency=" << outputParameters->suggestedLatency << endl; | ||
109 | cout << "================================="<<endl; | ||
110 | |||
111 | double samrate=48000; | ||
112 | cout << "trying samerate="<<samrate<<endl; | ||
113 | cout << "framesperbuf=" << PA_FRAMESPERBUF << endl; | ||
114 | |||
115 | PaError err=Pa_OpenStream( | ||
116 | &portaudiostream, | ||
117 | this->inputParameters, | ||
118 | this->outputParameters, | ||
119 | samrate, | ||
120 | PA_FRAMESPERBUF, /* frames per buffer */ | ||
121 | paClipOff | paDitherOff, | ||
122 | portaudio_process, | ||
123 | (void*)this); | ||
124 | this->havestreamtime=false; | ||
125 | |||
126 | if (err!=paNoError) | ||
127 | { | ||
128 | cout << "Error opening stream" << endl; | ||
129 | Pa_Terminate(); | ||
130 | return; | ||
131 | } | ||
132 | |||
133 | cout << "Stream opened, going to start it now..." << endl; | ||
134 | |||
135 | err=Pa_StartStream(portaudiostream); | ||
136 | |||
137 | if (err!=paNoError) | ||
138 | { | ||
139 | cout << "Error starting stream" << endl; | ||
140 | Pa_Terminate(); | ||
141 | return; | ||
142 | } | ||
143 | |||
144 | cout << "Stream started" << endl; | ||
145 | |||
146 | return; | ||
147 | } | ||
148 | |||
149 | int main() | ||
150 | { | ||
151 | HD24UserInterface* ui=new HD24UserInterface(); | ||
152 | ui->portaudioinit(); | ||
153 | ui->portaudio_transport_start(); | ||
154 | int blah; | ||
155 | cin >> blah; | ||
156 | |||
157 | } | ||
158 | |||