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 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
|
/*
Copyright 2005 PyTom <pytom@bishoujo.us>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pss.h"
#include <Python.h>
#include <SDL/SDL.h>
#include <SDL/SDL_sound.h>
#include <stdio.h>
/* The current Python. */
PyInterpreterState* interp;
PyThreadState* thread = NULL;
static void incref(PyObject *ref) {
PyThreadState *oldstate;
PyEval_AcquireLock();
oldstate = PyThreadState_Swap(thread);
Py_INCREF(ref);
PyThreadState_Swap(oldstate);
PyEval_ReleaseLock();
}
static void decref(PyObject *ref) {
PyThreadState *oldstate;
PyEval_AcquireLock();
oldstate = PyThreadState_Swap(thread);
Py_DECREF(ref);
PyThreadState_Swap(oldstate);
PyEval_ReleaseLock();
}
/* Locking on entry from python... */
#define BEGIN() PyThreadState *_save;
#define ENTER() { _save = PyEval_SaveThread(); SDL_LockAudio(); }
#define EXIT() { SDL_UnlockAudio(); PyEval_RestoreThread(_save); }
/* Min and Max */
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
/* The number of channels we support. */
#define NUM_CHANNELS 8
/* Various error codes. */
#define SUCCESS 0
#define SDL_ERROR -1
#define SOUND_ERROR -2
#define PSS_ERROR -3
/* This is called with the appropriate error code at the end of a
* function. */
#define error(err) PSS_error = err
int PSS_error = SUCCESS;
static const char *error_msg = NULL;
/* Have we been initialized? */
static int initialized = 0;
/*
* This structure represents a channel the system knows about
* and can play from.
*/
struct Channel {
/* The currently playing sample, NULL if this sample isn't playing
anything. */
Sound_Sample *playing;
/* The name of the playing music. */
PyObject *playing_name;
/* The number of ms to take to fade in the playing sample. */
int playing_fadein;
/* Is the playing sample tight? */
int playing_tight;
/* The queued up sample. */
Sound_Sample *queued;
/* The name of the queued up sample. */
PyObject *queued_name;
/* The number of ms to take to fade in the queued sample. */
int queued_fadein;
/* Is the queued sample tight? */
int queued_tight;
/* Is this channel paused? */
int paused;
/* The volume of the channel. */
int volume;
/* The number of decoded bytes in the buffer. */
int decoded;
/* The offset into the buffer that we last stopped at. */
int last;
/* The position (in bytes) that this channel has queued to. */
int pos;
/*
* The number of bytes for each step of fade.
* 0 when no fade is in progress.
*/
int fade_step_len;
/* How many bytes we are into the current fade step. */
int fade_off;
/* The current fade volume. */
int fade_vol;
/* The change in fade_vol for each step. */
int fade_delta;
/* The number of bytes in which we'll stop. */
int stop_bytes;
/* The event posted to the queue when we finish a track. */
int event;
};
/*
* All of the channels that the system knows about.
*/
struct Channel channels[NUM_CHANNELS];
/*
* The spec of the audio that is playing.
*/
SDL_AudioSpec audio_spec;
static int ms_to_bytes(int ms) {
return ((long long) ms) * audio_spec.freq * audio_spec.channels * 2 / 1000;
}
static int bytes_to_ms(int bytes) {
return ((long long) bytes) * 1000 / (audio_spec.freq * audio_spec.channels * 2);
}
static void start_sample(struct Channel* c, int reset_fade) {
int fade_steps;
if (!c) return;
c->decoded = 0;
c->last = 0;
c->pos = 0;
if (reset_fade) {
if (c->playing_fadein == 0) {
c->fade_step_len = 0;
} else {
fade_steps = c->volume;
c->fade_delta = 1;
c->fade_off = 0;
c->fade_vol = 0;
c->fade_step_len = ms_to_bytes(c->playing_fadein) / fade_steps;
c->fade_step_len &= ~0x7; // Even sample.
}
c->stop_bytes = -1;
}
}
/* static void update_pause(void) { */
/* int i; */
/* int pause = 1; */
/* for (i = 0; i < NUM_CHANNELS; i++) { */
/* if (channels[i].playing) { */
/* pause = 0; */
/* break; */
/* } */
/* } */
/* SDL_PauseAudio(pause); */
/* } */
static void free_sample(Sound_Sample *ss) {
Sound_FreeSample(ss);
}
// Actually mixes the audio.
static void mixaudio(Uint8 *dst, Uint8 *src, int length, int volume) {
// SDL_MixAudio may not work when length % 16 != 0.
if ((length & 0x0f) == 0) {
SDL_MixAudio(dst, src, length, volume);
} else {
int newlength = length + 16 - (length & 0xf);
Uint8 newsrc[newlength];
Uint8 newdst[newlength];
memcpy(newsrc, src, length);
memcpy(newdst, dst, length);
// Mix the audio once.
SDL_MixAudio(newdst, newsrc, newlength, volume);
memcpy(dst, newdst, length);
}
}
// Mixes the audio, while performing fading.
static void fade_mixaudio(struct Channel *c,
Uint8 *dst, Uint8 *src, int length) {
while (length) {
// No fade case.
if (c->fade_step_len == 0) {
mixaudio(dst, src, length, c->volume);
return;
}
// Fading, but we have some space left in the current step.
if (c->fade_off < c->fade_step_len) {
int l = min(c->fade_step_len - c->fade_off, length);
mixaudio(dst, src, l, c->fade_vol);
length -= l;
dst += l;
src += l;
c->fade_off += l;
continue;
}
// Otherwise, we have no space left in the current fade step.
// Go to the next step.
c->fade_off = 0;
c->fade_vol += c->fade_delta;
// Don't stop on a fadeout.
if (c->fade_vol <= 0) {
c->fade_vol = 0;
}
// Stop on a fadein.
if (c->fade_vol >= c->volume) {
c->fade_vol = c->volume;
c->fade_step_len = 0;
}
}
return;
}
static void post_event(struct Channel *c) {
if (! c->event) {
return;
}
SDL_Event e;
memset(&e, 0, sizeof(e));
e.type = c->event;
SDL_PushEvent(&e);
}
static void callback(void *userdata, Uint8 *stream, int length) {
int channel = 0;
for (channel = 0; channel < NUM_CHANNELS; channel++) {
int mixed = 0;
struct Channel *c = &channels[channel];
if (! c->playing) {
continue;
}
if (c->paused) {
continue;
}
while (mixed < length && c->playing) {
int mixleft = length - mixed;
int bufleft = c->decoded - c->last;
// We have some data in the buffer.
if (c->stop_bytes && bufleft) {
int bytes = min(bufleft, mixleft);
if (c->stop_bytes != -1)
bytes = min(c->stop_bytes, bytes);
fade_mixaudio(c, &stream[mixed],
&( ((Uint8*) c->playing->buffer) [c->last]),
bytes);
mixed += bytes;
c->last += bytes;
if (c->stop_bytes != -1)
c->stop_bytes -= bytes;
c->pos += bytes;
continue;
}
// Otherwise, no data is left in the buffer. Check why,
// and act accordingly.
// EOF or ERROR -- Skip to the next sample.
if (c->stop_bytes == 0 || c->playing->flags & (SOUND_SAMPLEFLAG_ERROR | SOUND_SAMPLEFLAG_EOF)) {
int old_tight = c->playing_tight;
post_event(c);
free_sample(c->playing);
decref(c->playing_name);
c->playing = c->queued;
c->playing_name = c->queued_name;
c->playing_fadein = c->queued_fadein;
c->playing_tight = c->queued_tight;
c->queued = NULL;
c->queued_name = NULL;
c->queued_fadein = 0;
c->queued_tight = 0;
start_sample(c, ! old_tight);
/* update_pause(); */
continue;
}
// If we're here, we're simply out of data in the
// buffer. Decode some.
c->decoded = Sound_Decode(c->playing);
c->last = 0;
}
}
}
/*
* Checks that the given channel is in range. Returns 0 if it is,
* sets an error and returns -1 if it is not.
*/
static int check_channel(int c) {
if (c < 0 || c >= NUM_CHANNELS) {
error(PSS_ERROR);
error_msg = "Channel number out of range.";
return -1;
}
return 0;
}
/*
* Loads the provided sample. Returns the sample on success, NULL on
* failure.
*/
static Sound_Sample *load_sample(SDL_RWops *rw, const char *ext) {
Sound_AudioInfo ai;
Sound_Sample *rv;
ai.format = audio_spec.format;
ai.channels = audio_spec.channels;
ai.rate = audio_spec.freq;
rv = Sound_NewSample(rw, ext, &ai, audio_spec.samples * audio_spec.channels * 2);
return rv;
}
void PSS_play(int channel, SDL_RWops *rw, const char *ext, PyObject *name, int fadein, int tight, int paused) {
BEGIN();
struct Channel *c;
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
/* Free playing and queued samples. */
if (c->playing) {
free_sample(c->playing);
c->playing = NULL;
decref(c->playing_name);
c->playing_name = NULL;
c->playing_tight = 0;
}
if (c->queued) {
free_sample(c->queued);
c->queued = NULL;
decref(c->queued_name);
c->queued_name = NULL;
c->queued_tight = 0;
}
/* Allocate playing sample. */
c->playing = load_sample(rw, ext);
if (! c->playing) {
EXIT();
error(SOUND_ERROR);
return;
}
incref(name);
c->playing_name = name;
c->playing_fadein = fadein;
c->playing_tight = tight;
c->paused = paused;
start_sample(c, 1);
/* update_pause(); */
EXIT();
error(SUCCESS);
}
void PSS_queue(int channel, SDL_RWops *rw, const char *ext, PyObject *name, int fadein, int tight) {
BEGIN();
struct Channel *c;
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
/* If we're not playing, then we should play instead of queue. */
if (!c->playing) {
EXIT();
PSS_play(channel, rw, ext, name, fadein, tight, 0);
return;
}
/* Free queued sample. */
if (c->queued) {
free_sample(c->queued);
c->queued = NULL;
decref(c->queued_name);
c->queued_name = NULL;
c->queued_tight = 0;
}
/* Allocate queued sample. */
c->queued = load_sample(rw, ext);
if (! c->queued) {
EXIT();
error(SOUND_ERROR);
return;
}
incref(name);
c->queued_name = name;
c->queued_fadein = fadein;
c->queued_tight = tight;
EXIT();
error(SUCCESS);
}
/*
* Stops all music from playing, freeing the data used by the
* music.
*/
void PSS_stop(int channel) {
BEGIN();
struct Channel *c;
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
if (c->playing) {
post_event(c);
}
/* Free playing and queued samples. */
if (c->playing) {
free_sample(c->playing);
c->playing = NULL;
decref(c->playing_name);
c->playing_name = NULL;
}
if (c->queued) {
free_sample(c->queued);
c->queued = NULL;
decref(c->queued_name);
c->queued_name = NULL;
}
/* update_pause(); */
EXIT();
error(SUCCESS);
}
/*
* This dequeues the queued sound from the supplied channel, if
* such a sound is queued. This does nothing to the playing
* sound.
*
* This does nothing if the playing sound is tight, ever_tight is
* false.
*/
void PSS_dequeue(int channel, int even_tight) {
BEGIN();
struct Channel *c;
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
if (c->queued && (! c->playing_tight || even_tight)) {
free_sample(c->queued);
c->queued = NULL;
decref(c->queued_name);
c->queued_name = NULL;
} else {
c->queued_tight = 0;
}
EXIT();
error(SUCCESS);
}
/*
* Returns the queue depth of the current channel. This is 0 if we're
* stopped, 1 if there's something playing but nothing queued, and 2
* if there's both something playing and something queued.
*/
int PSS_queue_depth(int channel) {
int rv = 0;
BEGIN();
struct Channel *c;
if (check_channel(channel)) {
return 0;
}
c = &channels[channel];
ENTER();
if (c->playing) rv++;
if (c->queued) rv++;
EXIT();
error(SUCCESS);
return rv;
}
PyObject *PSS_playing_name(int channel) {
BEGIN();
PyObject *rv;
struct Channel *c;
if (check_channel(channel)) {
Py_INCREF(Py_None);
return Py_None;
}
ENTER();
c = &channels[channel];
if (c->playing_name) {
rv = c->playing_name;
} else {
rv = Py_None;
}
incref(rv);
EXIT();
error(SUCCESS);
return rv;
}
/*
* Causes the given channel to fadeout playing after a specified
* number of milliseconds. The playing sound stops once the
* fadeout finishes (a queued sound may then start at full volume).
*/
void PSS_fadeout(int channel, int ms) {
BEGIN();
int fade_steps;
struct Channel *c;
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
if (ms == 0) {
c->stop_bytes = 0;
EXIT();
error(SUCCESS);
return;
}
fade_steps = c->volume;
c->fade_delta = -1;
c->fade_off = 0;
c->fade_vol = c->volume;
c->fade_step_len = ms_to_bytes(ms) / fade_steps;
c->fade_step_len &= ~0x7; // Even sample.
c->stop_bytes = ms_to_bytes(ms);
c->queued_tight = 0;
if (!c->queued) {
c->playing_tight = 0;
}
EXIT();
error(SUCCESS);
}
/*
* Sets the pause flag on the given channel 0 = unpaused, 1 = paused.
*/
void PSS_pause(int channel, int pause) {
BEGIN();
struct Channel *c;
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
c->paused = pause;
EXIT();
error(SUCCESS);
}
void PSS_unpause_all(void) {
int i;
BEGIN();
ENTER();
for (i = 0; i < NUM_CHANNELS; i++) {
channels[i].paused = 0;
}
EXIT();
error(SUCCESS);
}
/*
* Returns the position of the given channel, in ms.
*/
int PSS_get_pos(int channel) {
int rv;
struct Channel *c;
BEGIN();
if (check_channel(channel)) {
return -1;
}
c = &channels[channel];
ENTER();
if (c->playing) {
rv = bytes_to_ms(c->pos);
} else {
rv = -1;
}
EXIT();
error(SUCCESS);
return rv;
}
/*
* Sets an event that is queued up when the track on the given channel
* ends due to natural termination or a forced stop.
*/
void PSS_set_endevent(int channel, int event) {
struct Channel *c;
BEGIN();
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
c->event = event;
EXIT();
error(SUCCESS);
}
/*
* This sets the natural volume of the channel. (This may not take
* effect immediately if a fade is going on.)
*/
void PSS_set_volume(int channel, float volume) {
struct Channel *c;
BEGIN();
if (check_channel(channel)) {
return;
}
c = &channels[channel];
ENTER();
c->volume = (int) (volume * SDL_MIX_MAXVOLUME);
EXIT();
error(SUCCESS);
}
float PSS_get_volume(int channel) {
float rv;
struct Channel *c;
BEGIN();
if (check_channel(channel)) {
return 0.0;
}
c = &channels[channel];
ENTER();
rv = 1.0 * c->volume / SDL_MIX_MAXVOLUME;
EXIT();
error(SUCCESS);
return rv;
}
/*
* Initializes the sound to the given frequencies, channels, and
* sample buffer size.
*/
void PSS_init(int freq, int stereo, int samples) {
int i;
if (initialized) {
return;
}
PyEval_InitThreads();
if (!thread) {
thread = PyThreadState_Get();
interp = thread->interp;
thread = PyThreadState_New(interp);
}
if (!thread) {
error(SDL_ERROR);
return;
}
if (SDL_Init(SDL_INIT_AUDIO)) {
error(SDL_ERROR);
return;
}
audio_spec.freq = freq;
audio_spec.format = AUDIO_S16SYS;
audio_spec.channels = stereo;
audio_spec.samples = samples;
audio_spec.callback = callback;
audio_spec.userdata = NULL;
if (SDL_OpenAudio(&audio_spec, NULL)) {
error(SDL_ERROR);
return;
}
if (! Sound_Init()) {
SDL_CloseAudio();
error(SOUND_ERROR);
return;
}
/*
* Initialize the channels.
*/
for (i = 0; i < NUM_CHANNELS; i++) {
channels[i].playing = NULL;
channels[i].queued = NULL;
channels[i].volume = SDL_MIX_MAXVOLUME;
channels[i].paused = 1;
channels[i].event = 0;
}
SDL_PauseAudio(0);
initialized = 1;
error(SUCCESS);
}
void PSS_quit() {
BEGIN();
if (! initialized) {
return;
}
int i;
ENTER();
SDL_PauseAudio(1);
EXIT();
for (i = 0; i < NUM_CHANNELS; i++) {
PSS_stop(i);
}
Sound_Quit();
SDL_CloseAudio();
initialized = 0;
error(SUCCESS);
}
/*
* Returns the error message string if an error has occured, or
* NULL if no error has happened.
*/
const char *PSS_get_error() {
switch(PSS_error) {
case 0:
return "";
case SDL_ERROR:
return SDL_GetError();
case SOUND_ERROR:
return Sound_GetError();
case PSS_ERROR:
return error_msg;
default:
return "Error getting error.";
}
}
|