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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI 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 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI 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 OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnCppWrapper.h>
#if XN_PLATFORM == XN_PLATFORM_WIN32
#pragma warning(push,3)
#include <MMSystem.h>
#pragma warning(pop)
#endif
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define SAMPLE_XML_PATH "/etc/openni/SamplesConfig.xml"
#define NUMBER_OF_AUDIO_BUFFERS 100
#define CHECK_RC(rc, what) \
if (rc != XN_STATUS_OK) \
{ \
printf("%s failed: %s\n", what, xnGetStatusString(rc)); \
return rc; \
}
const XnUInt32 nSupportedNodes = 10;
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
using namespace xn;
enum Mode
{
MODE_PLAY,
MODE_RECORD,
};
void printUsage(const XnChar* arg0)
{
printf("usage: %s [play | record]\n", arg0);
}
XnStatus play(Context& /*context*/, AudioGenerator* aGens, XnUInt32 nNodes)
{
XnStatus nRetVal = XN_STATUS_OK;
#if XN_PLATFORM == XN_PLATFORM_WIN32
HWAVEOUT hWaveOut;
WAVEHDR* pAudioBuffers;
int nAudioNextBuffer;
if (nNodes > 1)
{
printf("Warning: only supports playing of a single node. Will play node '%s'.\n", aGens[0].GetName());
}
XnWaveOutputMode waveMode;
nRetVal = aGens[0].GetWaveOutputMode(waveMode);
CHECK_RC(nRetVal, "Failed getting wave output mode");
// init wave out
WAVEFORMATEX wf;
wf.wFormatTag = 0x0001; // PCM
wf.nChannels = waveMode.nChannels;
wf.nSamplesPerSec = waveMode.nSampleRate;
wf.wBitsPerSample = waveMode.nBitsPerSample;
wf.nBlockAlign = wf.wBitsPerSample * wf.nChannels / 8;
wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec;
MMRESULT mmRes = waveOutOpen(&hWaveOut, WAVE_MAPPER, &wf, NULL, NULL, CALLBACK_NULL);
if (mmRes != MMSYSERR_NOERROR)
{
printf("Warning: Failed opening wave out device. Audio could not be played!\n");
return XN_STATUS_ERROR;
}
// create some wave headers for playing
pAudioBuffers = new WAVEHDR[NUMBER_OF_AUDIO_BUFFERS];
xnOSMemSet(pAudioBuffers, 0, sizeof(WAVEHDR)*NUMBER_OF_AUDIO_BUFFERS);
const XnUInt32 nMaxBufferSize = 2 * 1024 * 1024;
for (int i = 0; i < NUMBER_OF_AUDIO_BUFFERS; ++i)
{
pAudioBuffers[i].lpData = new XnChar[nMaxBufferSize];
pAudioBuffers[i].dwUser = i;
pAudioBuffers[i].dwFlags = WHDR_DONE; // mark this buffer as empty (already played)
}
nAudioNextBuffer = 0;
printf ("Press any key to exit...\n");
// drop data till now
aGens[0].WaitAndUpdateData();
while (!xnOSWasKeyboardHit())
{
// Read audio chunk
nRetVal = aGens[0].WaitAndUpdateData();
if (nRetVal != XN_STATUS_OK)
{
printf("Waiting for new data failed: %s\n", xnGetStatusString(nRetVal));
}
XnUInt32 nBufferSize = aGens[0].GetDataSize();
WAVEHDR* pHeader = &pAudioBuffers[nAudioNextBuffer];
if ((pHeader->dwFlags & WHDR_DONE) == 0)
{
printf("No audio buffer is available!. Audio buffer will be lost!\n");
continue;
}
// first unprepare this header
MMRESULT mmRes = waveOutUnprepareHeader(hWaveOut, pHeader, sizeof(WAVEHDR));
if (mmRes != MMSYSERR_NOERROR)
{
CHAR msg[250];
waveOutGetErrorText(mmRes, msg, 250);
printf("Failed unpreparing header: %s", msg);
}
pHeader->dwFlags = 0;
xnOSMemCopy(pHeader->lpData, aGens[0].GetAudioBuffer(), nBufferSize);
pHeader->dwBufferLength = nBufferSize;
// prepare header
mmRes = waveOutPrepareHeader(hWaveOut, pHeader, sizeof(WAVEHDR));
if (mmRes != MMSYSERR_NOERROR)
{
CHAR msg[250];
waveOutGetErrorText(mmRes, msg, 250);
printf("Unable to prepare header: %s", msg);
continue;
}
// queue header
mmRes = waveOutWrite(hWaveOut, pHeader, sizeof(WAVEHDR));
if (mmRes != MMSYSERR_NOERROR)
{
CHAR msg[250];
waveOutGetErrorText(mmRes, msg, 250);
printf("Unable to queue header: %s", msg);
continue;
}
nAudioNextBuffer = (nAudioNextBuffer + 1) % NUMBER_OF_AUDIO_BUFFERS;
}
// stop playback
if (MMSYSERR_NOERROR != waveOutReset(hWaveOut))
{
printf("Warning: Failed stopping wave out device.!\n");
return XN_STATUS_ERROR;
}
// close device
waveOutClose(hWaveOut);
// release memory
for (int i = 0; i < NUMBER_OF_AUDIO_BUFFERS; ++i)
{
delete[] pAudioBuffers[i].lpData;
}
delete pAudioBuffers;
#endif
return XN_STATUS_OK;
}
XnStatus record(Context& context, AudioGenerator* aGens, XnUInt32 nNodes)
{
XnStatus nRetVal = XN_STATUS_OK;
// open files
XN_FILE_HANDLE aFiles[nSupportedNodes];
for (XnUInt32 i = 0; i < nNodes; ++i)
{
XnChar strFileName[XN_FILE_MAX_PATH];
sprintf(strFileName, "audio.%s.pcm", aGens[i].GetName());
nRetVal = xnOSOpenFile(strFileName, XN_OS_FILE_WRITE, &aFiles[i]);
CHECK_RC(nRetVal, "Open file");
}
printf ("Press any key to exit...\n");
// drop data till now
context.WaitAnyUpdateAll();
while (!xnOSWasKeyboardHit())
{
// Read audio chunk
nRetVal = context.WaitAnyUpdateAll();
if (nRetVal != XN_STATUS_OK)
{
printf("Waiting for new data failed: %s\n", xnGetStatusString(nRetVal));
}
// check which node has new data
for (XnUInt32 i = 0; i < nNodes; ++i)
{
if (aGens[i].IsDataNew())
{
nRetVal = xnOSWriteFile(aFiles[i], aGens[i].GetAudioBuffer(), aGens[i].GetDataSize());
CHECK_RC(nRetVal, "Write to file");
}
}
}
// close files
for (XnUInt32 i = 0; i < nNodes; ++i)
{
xnOSCloseFile(&aFiles[i]);
}
return (XN_STATUS_OK);
}
int main(int argc, char* argv[])
{
XnStatus nRetVal = XN_STATUS_OK;
Context context;
EnumerationErrors errors;
Mode mode;
// default mode
#if XN_PLATFORM == XN_PLATFORM_WIN32
mode = MODE_PLAY;
#else
mode = MODE_RECORD;
#endif
// check if mode was provided by user
if (argc > 1)
{
if (strcmp(argv[1], "play") == 0)
{
mode = MODE_PLAY;
}
else if (strcmp(argv[1], "record") == 0)
{
mode = MODE_RECORD;
}
else
{
printUsage(argv[0]);
return -1;
}
}
// make sure mode is valid
#if XN_PLATFORM != XN_PLATFORM_WIN32
if (mode == MODE_PLAY)
{
printf("Playing is not supported on this platform!\n");
return -1;
}
#endif
ScriptNode scriptNode;
nRetVal = context.InitFromXmlFile(SAMPLE_XML_PATH, scriptNode);
if (nRetVal == XN_STATUS_NO_NODE_PRESENT)
{
XnChar strError[1024];
errors.ToString(strError, 1024);
printf("%s\n", strError);
return (nRetVal);
}
else if (nRetVal != XN_STATUS_OK)
{
printf("Open failed: %s\n", xnGetStatusString(nRetVal));
return (nRetVal);
}
// find audio nodes
AudioGenerator gens[nSupportedNodes];
XnUInt32 nNodes = 0;
NodeInfoList list;
nRetVal = context.EnumerateExistingNodes(list, XN_NODE_TYPE_AUDIO);
CHECK_RC(nRetVal, "Enumerate audio nodes");
for (NodeInfoList::Iterator it = list.Begin(); it != list.End(); ++it)
{
NodeInfo info = *it;
nRetVal = info.GetInstance(gens[nNodes]);
CHECK_RC(nRetVal, "Get audio node");
nNodes++;
}
if (nNodes == 0)
{
printf("No audio node was found!\n");
return -1;
}
if (mode == MODE_PLAY)
{
nRetVal = play(context, gens, nNodes);
}
else if (mode == MODE_RECORD)
{
nRetVal = record(context, gens, nNodes);
}
scriptNode.Release();
for (int i = 0; i < nSupportedNodes; ++i)
gens[i].Release();
context.Release();
return nRetVal;
}
|