• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

examples/example6/SoundPlayer.cpp

Go to the documentation of this file.
00001 #include "SoundPlayer.h"
00002 
00005 /***************************************************************/
00006 /* SoundPlayer implementation                                  */
00007 /***************************************************************/
00008 
00009 /* SoundPlayer constructor */
00010 SoundPlayer::SoundPlayer() {
00011     // Create a new playback
00012     int error;
00013     static const pa_sample_spec ss = {PA_SAMPLE_S16LE, 24000, 1};
00014     if (!(connection = pa_simple_new(NULL, NULL, PA_STREAM_PLAYBACK, NULL,
00015                                      "playback", &ss, NULL, NULL, &error))) {
00016         fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n",
00017                 pa_strerror(error));
00018     }
00019 }
00020 
00021 /* SoundPlayer destructor */
00022 SoundPlayer::~SoundPlayer() {
00023     if (connection)
00024         pa_simple_free(connection);
00025 }
00026 
00027 /* Play a buffer */
00028 void SoundPlayer::playBuffer(unsigned char * b, size_t s) {
00029     if (connection) {
00030         int error;
00031         // Play buffer
00032         if (pa_simple_write(connection, b, s, &error) < 0) {
00033             fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n",
00034                     pa_strerror(error));
00035             return;
00036         }
00037     }
00038 }
00039 
00040 int SoundPlayer::getLatency() {
00041     if (connection) {
00042         int error;
00043         pa_usec_t ret = pa_simple_get_latency(connection, &error);
00044         return (int)ret;
00045     } else return 0;
00046 }
00047 
00048 /***************************************************************/
00049 /* SoundPlayer::SoundAction implementation                     */
00050 /***************************************************************/
00051 
00052 /* SoundAction constructors */
00053 SoundPlayer::SoundAction::SoundAction(SoundPlayer * a) {
00054     player = a;
00055     time = 0;
00056     latency = a ? a->getLatency() : 0;
00057     buffer = NULL;
00058 }
00059 
00060 SoundPlayer::SoundAction::SoundAction(SoundPlayer * a, int t) {
00061     player = a;
00062     time = t;
00063     latency = a ? a->getLatency() : 0;
00064     buffer = NULL;
00065 }
00066 
00067 SoundPlayer::SoundAction::SoundAction(const SoundPlayer::SoundAction & b) {
00068     // Copy fields from the target.
00069     time = b.time;
00070     latency = b.latency;
00071     player = b.getPlayer();
00072     if (b.buffer) {
00073         // Increment reference counter for the buffer.
00074         refCount = b.refCount;
00075         buffer = b.buffer;
00076         size = b.size;
00077         *refCount++;
00078     } else {
00079         buffer = NULL;
00080     }  
00081 }
00082 
00083 /* SoundAction destructor */
00084 SoundPlayer::SoundAction::~SoundAction() {
00085     if (buffer) {
00086         *refCount--; // Decrement the reference counter. 
00087         if (*refCount == 0) { // Deallocate if reference counter is 0.
00088             delete refCount;
00089             delete[] buffer;
00090         }
00091     }
00092 }
00093 
00094 void SoundPlayer::SoundAction::setWavFile(const char * f) {
00095 
00096     // Delete existing connection
00097     if (buffer) {
00098         *refCount--;
00099         if (*refCount == 0) {
00100             delete buffer;
00101             delete refCount;
00102         }
00103         buffer = NULL;
00104     }
00105 
00106     filename = std::string(f);
00107     int fd;
00108   
00109     // Check that the file can be opened.
00110     if ((fd = open(f, O_RDONLY)) < 0) {
00111         fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
00112         return;
00113     } 
00114 
00115     // Find out how long the file is
00116     struct stat stat_buf;
00117     fstat(fd, &stat_buf);
00118     ssize_t capacity = stat_buf.st_size;
00119 
00120     // Read the file and store into a buffer
00121     buffer = new unsigned char[capacity];
00122     size = 0;
00123     while (size < capacity) {
00124         ssize_t r = read(fd, buffer + size, 4096);
00125         if (r == 0) break; // EOF
00126         if (r < 0) {
00127             fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
00128         }
00129         size += r;
00130     }
00131 
00132     close(fd);
00133 
00134     // Make a reference count for the buffer
00135     refCount = new unsigned int;
00136     *refCount = 1;
00137 }
00138 
00139 /* Perform the required action */
00140 void SoundPlayer::SoundAction::doAction() {
00141     if (buffer) {
00142         player->playBuffer(buffer, size);
00143     }
00144 }
00145 
00146 
00147 

Generated on Fri Sep 24 2010 15:52:59 for FCam by  doxygen 1.7.1