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
|
/*
Copyright (C) 2000 Dancer A.L Vesperman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#if (__GNUG__ >= 2) && (!defined WIN32)
# pragma implementation
#endif
#ifdef XWIN
#include "Midi.h"
#ifndef ALPHA_LINUX_CXX
# include <unistd.h>
# include <csignal>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif
#include "fnames.h"
#include "Audio.h"
using std::cerr;
using std::endl;
#include "Configuration.h"
extern Configuration *config;
const Uint32 Timidity_binary_magic=0x345300;
const Uint32 Timidity_binary_magic_sfx=0x345301;
// #undef HAVE_TIMIDITY_BIN // Damn. Can't do this while SDL has the audio device.
// New strategy - Tell timidity to output to stdout. Capture that via a pipe, and
// introduce it back up to the mixing layer.
#if HAVE_TIMIDITY_BIN
#include "Timidity_binary.h"
template<class T>
T max(T x,T y)
{
return (x>y)?x:y;
}
static void *midi_process(void *p)
{
Timidity_binary *ptr=static_cast<Timidity_binary *>(p);
ptr->player();
return 0;
}
static void *sfx_process(void *p)
{
Timidity_binary *ptr=static_cast<Timidity_binary *>(p);
ptr->sfxplayer();
return 0;
}
static void copy_to_fh(std::string name,int number)
{
char buf[8192];
int fh=::open(name.c_str(),O_RDONLY);
if(fh==-1)
{
perror("open");
return;
}
while(1)
{
ssize_t r=read(fh, buf, sizeof(buf));
if(r<=0)
break;
write(number,buf,r);
}
close(number);
close(fh);
}
static void copy_to_name(std::string name1,std::string name2)
{
int fh=::open(name2.c_str(),O_WRONLY);
if(fh==-1)
{
perror("open");
return;
}
copy_to_fh(name1,fh);
}
int timidity_play(std::string filename, bool repeat, std::string &newfilename, pid_t &pid)
{
#if HAVE_MKSTEMP
char newfn[128];
std::strcpy(newfn,"/tmp/u7_midi_fileXXXXXX");
int dfh=mkstemp(newfn);
if(dfh==-1)
{
perror("mkstemp");
return -1;
}
copy_to_fh(filename,dfh);
unlink(filename.c_str());
newfilename=newfn;
#else
char newfn[L_tmpnam];
newfilename=tmpnam(newfn);
copy_to_name(filename,newfilename);
unlink(filename.c_str());
#endif
char nbuf[32];
snprintf(nbuf,32,"%lu", Audio::get_ptr()->actual.freq);
std::string repstr = "-id";
if(repeat)
repstr += "l";
int pdes[2];
pipe(pdes);
switch(pid = fork())
{
case -1:
close(pdes[0]);
close(pdes[1]);
break;
case 0:
dup2(pdes[1], STDOUT_FILENO);
close(pdes[1]);
close(pdes[0]);
close(STDERR_FILENO);
execlp("timidity","timidity","-Or1slS",repstr.c_str(),"-s",nbuf,"-o-",newfilename.c_str(),0);
_exit(127);
default:
{
close(pdes[1]);
return pdes[0];
}
}
return -1;
}
void Timidity_binary::player(void)
{
Audio::get_ptr()->Destroy_Audio_Stream(Timidity_binary_magic);
ProducerConsumerBuf *audiostream=Audio::get_ptr()->Create_Audio_Stream(
Timidity_binary_magic);
if (!audiostream)
{
cerr << "Timidity_binary(): All audio streams in use" << endl;
return;
}
char buf[4096];
std::string newfilename;
pid_t timidity_pid;
int fd = timidity_play(filename, do_repeat, newfilename, timidity_pid);
for (;;)
{
size_t x=read(fd, buf, sizeof(buf));
if(stop_music_flag || x<=0 || !audiostream->is_consuming())
break;
audiostream->produce(buf,x);
}
close(fd);
kill(timidity_pid, SIGKILL);
unlink(newfilename.c_str());
audiostream->end_production();
audiostream=0;
}
void Timidity_binary::sfxplayer(void)
{
Audio::get_ptr()->Destroy_Audio_Stream(Timidity_binary_magic_sfx);
ProducerConsumerBuf *audiostream=Audio::get_ptr()->Create_Audio_Stream(
Timidity_binary_magic_sfx);
char buf[4096];
std::string newfilename;
pid_t timidity_pid;
int fd = timidity_play(sfxname, false, newfilename, timidity_pid);
for (;;)
{
size_t x=read(fd, buf, sizeof(buf));
if(x<=0 || !audiostream->is_consuming())
break;
audiostream->produce(buf,x);
}
close(fd);
kill(timidity_pid, SIGKILL);
unlink(newfilename.c_str());
audiostream->end_production();
audiostream=0;
}
Timidity_binary::Timidity_binary() : midi_thread(0), sfx_thread(0), filename(),
stop_music_flag(false)
{
// Figure out if the binary is where we expect it to be.
}
Timidity_binary::~Timidity_binary()
{
// Stop any current player
stop_track();
stop_sfx();
}
void Timidity_binary::stop_track(void)
{
stop_music_flag = true;
Audio::get_ptr()->Destroy_Audio_Stream(Timidity_binary_magic);
if (midi_thread)
pthread_join(midi_thread, 0);
midi_thread = 0;
stop_music_flag = false;
}
void Timidity_binary::stop_sfx(void)
{
Audio::get_ptr()->Destroy_Audio_Stream(Timidity_binary_magic_sfx);
pthread_join(sfx_thread, 0);
}
bool Timidity_binary::is_playing(void)
{
if(Audio::get_ptr()->is_playing(Timidity_binary_magic))
return true;
return false;
}
void Timidity_binary::start_track(XMIDIEventList *event_list,bool repeat)
{
const char *name = MIDITMPFILE;
event_list->Write(name);
#if DEBUG
cerr << "Starting midi sequence with Timidity_binary" << endl;
#endif
{
#if DEBUG
cerr << "Stopping any running track" << endl;
#endif
stop_track();
}
#if DEBUG
cerr << "Starting to play " << name << endl;
#endif
filename=name;
do_repeat=repeat;
cerr << "Creating new player thread" << endl;
if(pthread_create(&midi_thread, 0, midi_process, this)==-1)
perror("pthread_create");
}
/*
virtual void start_track( *, bool repeat);
virtual void start_sfx(XMIDIEventList *);
*/
void Timidity_binary::start_sfx(XMIDIEventList *event_list)
{
const char *name = MIDISFXFILE;
event_list->Write(name);
#if DEBUG
cerr << "Starting sound effect with Timidity_binary" << endl;
#endif
{
#if DEBUG
cerr << "Stopping any running sfx" << endl;
#endif
stop_sfx();
}
#if DEBUG
cerr << "Starting to play " << name << endl;
#endif
sfxname=name;
if(pthread_create(&sfx_thread, 0, sfx_process, this)==-1)
perror("pthread_create");
}
const char *Timidity_binary::copyright(void)
{
return "Internal cheapass forked timidity synthesiser";
}
#endif // HAVE_TIMIDITY_BIN
#endif // XWIN
|