Go to the documentation of this file.00001 #include "SoundPlayer.h"
00002
00005
00006
00007
00008
00009
00010 SoundPlayer::SoundPlayer() {
00011
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
00022 SoundPlayer::~SoundPlayer() {
00023 if (connection) {
00024 pa_simple_free(connection);
00025 }
00026 }
00027
00028
00029 void SoundPlayer::playBuffer(unsigned char *b, size_t s) {
00030 if (connection) {
00031 int error;
00032
00033 if (pa_simple_write(connection, b, s, &error) < 0) {
00034 fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n",
00035 pa_strerror(error));
00036 return;
00037 }
00038 }
00039 }
00040
00041 int SoundPlayer::getLatency() {
00042 if (connection) {
00043 int error;
00044 pa_usec_t ret = pa_simple_get_latency(connection, &error);
00045 return (int)ret;
00046 } else { return 0; }
00047 }
00048
00049
00050
00051
00052
00053
00054 SoundPlayer::SoundAction::SoundAction(SoundPlayer *a) {
00055 player = a;
00056 time = 0;
00057 latency = a ? a->getLatency() : 0;
00058 buffer = NULL;
00059 }
00060
00061 SoundPlayer::SoundAction::SoundAction(SoundPlayer *a, int t) {
00062 player = a;
00063 time = t;
00064 latency = a ? a->getLatency() : 0;
00065 buffer = NULL;
00066 }
00067
00068 SoundPlayer::SoundAction::SoundAction(const SoundPlayer::SoundAction &b) {
00069
00070 time = b.time;
00071 latency = b.latency;
00072 player = b.getPlayer();
00073 if (b.buffer) {
00074
00075 refCount = b.refCount;
00076 buffer = b.buffer;
00077 size = b.size;
00078 *refCount++;
00079 } else {
00080 buffer = NULL;
00081 }
00082 }
00083
00084
00085 SoundPlayer::SoundAction::~SoundAction() {
00086 if (buffer) {
00087 *refCount--;
00088 if (*refCount == 0) {
00089 delete refCount;
00090 delete[] buffer;
00091 }
00092 }
00093 }
00094
00095 void SoundPlayer::SoundAction::setWavFile(const char *f) {
00096
00097
00098 if (buffer) {
00099 *refCount--;
00100 if (*refCount == 0) {
00101 delete buffer;
00102 delete refCount;
00103 }
00104 buffer = NULL;
00105 }
00106
00107 filename = std::string(f);
00108 int fd;
00109
00110
00111 if ((fd = open(f, O_RDONLY)) < 0) {
00112 fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
00113 return;
00114 }
00115
00116
00117 struct stat stat_buf;
00118 fstat(fd, &stat_buf);
00119 ssize_t capacity = stat_buf.st_size;
00120
00121
00122 buffer = new unsigned char[capacity];
00123 size = 0;
00124 while (size < capacity) {
00125 ssize_t r = read(fd, buffer + size, 4096);
00126 if (r == 0) { break; }
00127 if (r < 0) {
00128 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
00129 }
00130 size += r;
00131 }
00132
00133 close(fd);
00134
00135
00136 refCount = new unsigned int;
00137 *refCount = 1;
00138 }
00139
00140
00141 void SoundPlayer::SoundAction::doAction() {
00142 if (buffer) {
00143 player->playBuffer(buffer, size);
00144 }
00145 }
00146
00147
00148