simple.cpp

Go to the documentation of this file.
00001 /*
00002  * simple.cpp
00003  *
00004  * Some changes Copyright (C) 2009 by Thomas Vaughan.
00005  * Earlier changes are from the audiere project.
00006  *
00007  * This file copied directly from the audiere source tree
00008  *   (examples/simple/simple.cpp).  Copy made from the 1.9.4 version.
00009  *
00010  * As such, this file falls under the Lesser Gnu Public License (LPGL).
00011  *
00012  *
00013  * This is just supposed to play a single file to the given device.
00014  */
00015 
00016 #include "audiere/audiere.h"
00017 
00018 #include <iostream>
00019 #include <memory>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 
00023 using namespace std;
00024 using namespace audiere;
00025 
00026 
00027 #ifdef WIN32
00028 
00029 #include <windows.h>
00030 inline void sleepSecond() {
00031   Sleep(1000);
00032 }
00033 
00034 #else  // assume POSIX
00035 
00036 #include <unistd.h>
00037 inline void sleepSecond() {
00038   sleep(1);
00039 }
00040 
00041 #endif
00042 
00043 
00044 int main(int argc, const char** argv) {
00045   if (argc != 2 && argc != 3) {
00046     cerr << "usage: simple <filename> [<device>]" << endl;
00047     return EXIT_FAILURE;
00048   }
00049 
00050   cerr << "initializing..." << endl;
00051 
00052   const char* device_name = "";
00053   if (argc == 3) {
00054     device_name = argv[2];
00055   }
00056 
00057   AudioDevicePtr device = OpenDevice(device_name);
00058   if (!device) {
00059     cerr << "OpenDevice() failed" << endl;
00060     return EXIT_FAILURE;
00061   }
00062 
00063   cerr << "opened device" << endl;
00064 
00065   OutputStreamPtr sound = OpenSound(device, argv[1]);
00066   if (!sound) {
00067     cerr << "OpenSound() failed" << endl;
00068     return EXIT_FAILURE;
00069   }
00070 
00071   cerr << "opened sound" << endl;
00072 
00073   sound->play();
00074 
00075   cerr << "started playback" << endl;
00076   while (sound->isPlaying()) {
00077     sleepSecond();
00078     if (sound->isSeekable()) {
00079       cerr << "position: " << sound->getPosition() << endl;
00080     }
00081   }
00082 
00083   return EXIT_SUCCESS;
00084 }