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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
|
/**************************************************************************
fmplayer.cpp - all the FMPlayer-API
-------------------
begin : September 20th 2002
copyright : (C) 2002-2003 by Daniel Gruen
email : daniel_gruen@web.de
***************************************************************************/
/***************************************************************************
* *
* 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 is a slim and special fms version for qliss3d
// please use the original version from http://sourceforge.net/projects/fmsynth
// if you want to do anything on it
#include "include/fmplayer.h"
#include <cmath>
#ifdef FMS_WORKS
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include "include/fmval.h"
#include "include/version.h"
using namespace std;
int dspfd = 0;
void writeSoundByte(unsigned char * byte, PlayMode mode) {
ssize_t r;
//cout << int(byte) << endl;
if(mode != DSP)
return;
while ((r = write(dspfd, byte, 1)) < 1) {
if (r == -1)
return;
}
}
/*----------------------------------------------------------------*/
FMPlayer::FMPlayer() {
if(debug_level>1)
cout << "FMPlayer::FMPlayer called" << endl;
first = new FMChannel(this);
channel = first;
syncPointer();
rate = D_SAMPRATE;
mode = DSP;
}
FMPlayer::~FMPlayer() {
if(debug_level>1)
cout << "FMPlayer::~FMPlayer called" << endl;
delete first;
if(debug_level>1)
cout << "FMPlayer::~FMPlayer finished" << endl;
}
void FMPlayer::nextSound() {
if(debug_level>1)
cout << "FMPlayer::nextSound called" << endl;
channel->nextSound();
syncPointer();
}
void FMPlayer::setRate(int drate) {
if(debug_level>1)
cout << "FMPlayer::setRate called" << endl;
rate = drate;
}
int FMPlayer::getRate() const {
return rate;
}
void FMPlayer::openFiles() {
if(debug_level>1)
cout << "FMPlayer::openFiles called" << endl;
try{
first->openFiles();
}
catch(...){
cout << "caught error in FMPlayer::openFiles" << endl;
throw;
}
}
void FMPlayer::compute() {
if(debug_level>1)
cout << "FMPlayer::compute called" << endl;
first->compute();
}
void FMPlayer::syncPointer() {
if(debug_level>1)
cout << "FMPlayer::syncPointer called" << endl;
package = channel->package;
sound = channel->sound;
}
void FMPlayer::play() {
try{
if(debug_level>1)
cout << "FMPlayer::play called" << endl;
openFiles();
compute();
playInit();
while(first->ptr)
playVal();
playCleanup();
}
catch(int x){
if(x>0) // wenn's eine Fehlermeldung ist
throw;
}
}
void FMPlayer::playInit() {
if(debug_level>1)
cout << "FMPlayer::playInit called" << endl;
int format = AFMT_U8;
int chan = 0;
first->pointerReset();
try{
if(mode == DSP && !dspfd){
if(debug_level>1)
cout << "going to open and initialize /dev/dsp" << endl;
if((dspfd = open("/dev/dsp", O_WRONLY)) == -1) {
mode = NONE;
dspfd = 0;
if(debug_level)
cout << "error opening /dev/dsp: reset PlayMode and dsp-fd" << endl;
throw DSP_OPEN_ERR;
return;
}
if(ioctl(dspfd, SNDCTL_DSP_STEREO, &chan) == -1) {
mode = NONE;
throw DSP_CHAN_ERR;
return;
}
if(ioctl(dspfd, SNDCTL_DSP_SETFMT, &format) == -1) {
mode = NONE;
throw DSP_SFMT_ERR;
return;
}
if(ioctl(dspfd, SNDCTL_DSP_SPEED, &rate) == -1) {
mode = NONE;
throw DSP_RATE_ERR;
return;
}
if(debug_level)
cout << "opened /dev/dsp successfully!" << endl;
}
else if(mode == NONE){
return;
}
else{
if(debug_level)
cout << "PlayMode " << mode << " not implemented!" << endl;
throw PLY_MODE_ERR;
return;
}
}
catch(...){
throw;
}
if(debug_level>1)
cout << "FMPlayer::playInit finished" << endl;
}
void FMPlayer::playVal(){
unsigned char buff[1];
buff[0] = int(*first->ptr);
writeSoundByte(buff, mode);
first->ptrInc();
}
void FMPlayer::playCleanup() {
if(debug_level>1)
cout << "FMPlayer::playCleanup called" << endl;
if(mode == DSP){
if(ioctl(dspfd, SNDCTL_DSP_SYNC) == -1) {
throw DSP_SYNC_ERR;
}
close(dspfd); dspfd = 0;
}
}
/*----------------------------------------------------------------*/
FMChannel::FMChannel(FMPlayer *parentPlayer) {
if(debug_level>1)
cout << "FMChannel::FMChannel called" << endl;
player = parentPlayer;
first = new FMPackage(this);
package = first;
syncPointer();
ptr = 0;
dr = 0;
}
FMChannel::~FMChannel(){
if(debug_level>1)
cout << "FMChannel::~FMChannel called" << endl;
delete first;
if(debug_level>1)
cout << "FMChannel::~FMChannel finished" << endl;
}
void FMChannel::nextSound() {
if(debug_level>1)
cout << "FMChannel::nextSound called" << endl;
package->nextSound();
syncPointer();
}
void FMChannel::openFiles() {
if(debug_level>1)
cout << "FMChannel::openFiles called" << endl;
try{
first->openFiles();
}
catch(...){
cout << "catch error in FMChannel::openFiles" << endl;
throw;
}
}
void FMChannel::compute() {
if(debug_level>1)
cout << "FMChannel::compute called" << endl;
package = first;
while(package){
package->compute();
package = 0;
}
ptr = first->values;
package = first;
dr = 0;
}
void FMChannel::syncPointer() {
if(debug_level>1)
cout << "FMChannel::syncPointer called" << endl;
if(package)
sound = package->sound;
if(debug_level>1)
cout << "FMChannel::syncPointer finished" << endl;
}
void FMChannel::ptrInc() {
if(ptr){
ptr++;
i++;
if(ptr >= package->values + package->getRval()){
ptr = package->values;
}
if(i >= package->getTime() * player->getRate() * package->getRepeat()){
// channel repeat problematisch!
ptr = 0;
}
}
}
void FMChannel::pointerReset() {
if(debug_level>1)
cout << "FMChannel::pointerReset called" << endl;
package = first;
ptr = package->values;
i = 0;
if(debug_level>1)
cout << "FMChannel::pointerReset finished" << endl;
}
/*----------------------------------------------------------------*/
FMPackage::FMPackage(FMChannel *parentChannel){
if(debug_level>1)
cout << "FMPackage::FMPackage called" << endl;
channel = parentChannel;
tvol = 0.0; // have to do this before FMSoundfile::FMSoundfile(this)
sound = new FMSoundfile(this);
first = sound;
time = 1.0;
vol = 1.0;
repeat = 1.0;
rval = 0;
values = 0;
comp_lock=0;
}
FMPackage::~FMPackage() {
if(debug_level>1)
cout << "FMPackage::~FMPackage called" << endl;
delete first;
if(values)
delete []values;
if(debug_level>1)
cout << "FMPackage::~FMPackage finished" << endl;
}
void FMPackage::nextSound() {
if(debug_level>1)
cout << "FMPackage::nextSound called" << endl;
if(!sound->next){
sound->next = new FMSoundfile(this);
comp_lock=0;
}
sound = sound->next;
}
float FMPackage::getTime() const {
return time;
}
void FMPackage::setTime(float dtime) {
if(debug_level>1)
cout << "FMPackage::setTime called" << endl;
time = dtime; compLockAll();
}
float FMPackage::getVolume() const {
if(debug_level>1)
cout << "FMPackage::getVolume called" << endl;
return vol;
}
void FMPackage::setVolume(float dvol) {
if(debug_level>1)
cout << "FMPackage::setVolume called" << endl;
vol = dvol; comp_lock=0;
}
int FMPackage::getRval() const {
return rval;
}
float FMPackage::getRepeat() const {
return repeat;
}
void FMPackage::setTVol(float dtvol) {
if(debug_level>1)
cout << "FMPackage::setTVol called" << endl;
tvol = dtvol;
}
float FMPackage::getTVol() const {
return tvol;
}
void FMPackage::setRepeat(float drepeat) {
if(debug_level>1)
cout << "FMPackage::setRepeat called" << endl;
repeat = drepeat;
}
void FMPackage::volumeChanged(float oldv, float newv){
if(debug_level>1)
cout << "FMPackage::volumeChanged called" << endl << "Volume changed: " << tvol << " - ";
tvol += newv - oldv;
if(debug_level>1)
cout << tvol << endl;
comp_lock=0;
}
void FMPackage::openFiles() {
if(debug_level>1)
cout << "FMPackage::openFiles called" << endl;
try{
FMSoundfile * tmpsound = first;
while(tmpsound){
if(!tmpsound->openLock())
tmpsound->openFile();
tmpsound = tmpsound->next;
if(debug_level>1)
cout << "opening of one file finished" << endl;
}
if(debug_level)
cout << "opening of all files in package finished" << endl;
}
catch(...){
cout << "caught error in FMPackage::openFiles" << endl;
throw;
}
}
void FMPackage::compute() {
if(debug_level>1)
cout << "FMPackage::compute called" << endl;
if(comp_lock){
FMSoundfile * tmp = first;
bool ac = 1;
while(tmp){
ac = ac * tmp->compLock();
tmp = tmp->next;
}
if(ac){
if(debug_level)
cout << "already computed" << endl;
return;
}
}
if(values){
if(debug_level)
cout << "recomputing package" << endl;
delete []values;
}
// first, how many values do we need?
rval = 1;
//int numsounds = 0;
FMSoundfile *tmpsound = first;
while(tmpsound){
//numsounds++;
tmpsound->compute();
tmpsound->setAtime(0.0);
//tmpsound->setVolume(tmpsound->getVolume() / tvol, 0);
// don't set total volume, that's the reason for 0
rval = int(kgv(rval, tmpsound->rval()));
tmpsound = tmpsound->next;
}
if(rval > channel->player->getRate() * time || rval < 0)
rval = int(channel->player->getRate() * time);
// ok, so we'll make a new value array
if(debug_level>1)
cout << rval << " values will be computed in package" << endl;
values = new float[rval];
ptr = values;
if(debug_level>1)
cout << "values starting @ " << ptr << endl;
// and now, we'll actually compute the values
while(ptr < values+rval){
*ptr = 0.0;
tmpsound = first;
while(tmpsound){
*ptr = *ptr + tmpsound->value();
//if(tmpsound->ffreq)
// tmpsound->setFreq(tmpsound->ffreq->HFValue());
tmpsound = tmpsound->next;
}
ptr++;
}
comp_lock=1;
if(debug_level>1)
cout << "FMPackage::compute finished after computing" << endl;
}
void FMPackage::compLockAll(){
}
/*----------------------------------------------------------------*/
FMFile::FMFile(FMPackage *parentPackage){
if(debug_level>1)
cout << "FMFile::FMFile called" << endl;
package = parentPackage;
freq = 1;
atime = 0.0;
open_lock = 0;
volume = 1.0;
}
FMFile::~FMFile(){
}
bool FMFile::openLock(){
if(debug_level>1)
cout << "FMFile::openLock called" << endl;
return open_lock;
}
bool FMFile::compLock(){
if(debug_level>1)
cout << "FMFile::compLock called" << endl;
return 1;
}
void FMFile::compute(){
if(debug_level>1)
cout << "FMFile::compute called" << endl;
}
void FMFile::setFreq(float dfreq) {
freq = dfreq;
package->compLockAll();
}
void FMFile::setVolume(float dvolume, bool setptvol){
if(debug_level>1)
cout << "FMFile::setVolume called" << endl;
if(setptvol){
if(debug_level>1)
cout << "changing total volume (" << volume << ", " << dvolume << ")" << endl;
package->volumeChanged(volume, dvolume);
}
volume=dvolume;
package->compLockAll();
}
float FMFile::getVolume() const {
if(debug_level>1)
cout << "FMFile::getVolume called" << endl;
return volume;
}
void FMFile::setAtime(float datime) {
if(debug_level>1)
cout << "FMFile::setAtime called" << endl;
atime = datime; /*should there be a comp_lock=0 here?*/
}
void FMFile::incAtime() {
atime = fmod((atime + vtl[getVlen()-1].getTime() * freq / package->channel->player->getRate()),
vtl[getVlen()-1].getTime());
}
void FMFile::openFile() {
if(debug_level)
cout << "FMFile::openFile called" << endl;
try{
init();
}
catch(...){
throw;
open_lock=0;
}
}
float FMFile::value(bool count_on){
float avalue = fmval(this, atime);
if(count_on)
incAtime();
return avalue;
}
int FMFile::rval(){
if(debug_level>1)
cout << "FMFile::rval called" << endl;
int drval = 1;
drval = int(kgv(drval,package->channel->player->getRate() / freq));
if(drval < 0.0 || drval > package->channel->player->getRate() * package->getTime())
drval = int(package->channel->player->getRate() * package->getTime());
return drval;
}
/*----------------------------------------------------------------*/
FMSoundfile::FMSoundfile(FMPackage *parentPackage) : FMFile(parentPackage){
if(debug_level>1)
cout << "FMSoundfile::FMSoundfile called" << endl;
next = 0;
freq = 441.0;
package->volumeChanged(0.0, 1.0);
}
FMSoundfile::~FMSoundfile(){
if(debug_level>1)
cout << "FMSoundfile::~FMSoundfile called" << endl;
delete []vtl;
if(next)
delete next;
if(debug_level>1)
cout << "FMSoundfile::~FMSoundfile finished" << endl;
}
float FMSoundfile::value(bool count_on){
return FMFile::value(count_on) * (volume/package->getTVol());
}
/*----------------------------------------------------------------*/
#endif
float ggt(float a, float b) {
if(a < 0)
a = a * -1;
if(b < 0)
b = b * -1;
if(b>a)
return ggt(b, a);
if(b!=0)
return ggt(b, fmod(a, b));
else
return a;
}
float kgv(float a, float b) {
return a*b/ggt(a, b);
}
|