⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 effect_position.c

📁 SDL_mixer 是一个基于 SDL 的混音器
💻 C
📖 第 1 页 / 共 4 页
字号:
/*    SDL_mixer:  An audio mixer library based on the SDL library    Copyright (C) 1997-2004 Sam Lantinga    This library is free software; you can redistribute it and/or    modify it under the terms of the GNU Library General Public    License as published by the Free Software Foundation; either    version 2 of the License, or (at your option) any later version.    This library 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    Library General Public License for more details.    You should have received a copy of the GNU Library General Public    License along with this library; if not, write to the Free    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    This file by Ryan C. Gordon (icculus@linuxgames.com)    These are some internally supported special effects that use SDL_mixer's    effect callback API. They are meant for speed over quality.  :)*//* $Id: effect_position.c 1451 2004-11-15 23:36:49Z slouken $ */#include <stdio.h>#include <stdlib.h>#include <string.h>#include "SDL.h"#include "SDL_mixer.h"#include "SDL_endian.h"#define __MIX_INTERNAL_EFFECT__#include "effects_internal.h"/* profile code:    #include <sys/time.h>    #include <unistd.h>    struct timeval tv1;    struct timeval tv2;        gettimeofday(&tv1, NULL);        ... do your thing here ...    gettimeofday(&tv2, NULL);    printf("%ld\n", tv2.tv_usec - tv1.tv_usec);*//* * Positional effects...panning, distance attenuation, etc. */typedef struct _Eff_positionargs{    volatile float left_f;    volatile float right_f;    volatile Uint8 left_u8;    volatile Uint8 right_u8;    volatile float left_rear_f;    volatile float right_rear_f;    volatile float center_f;    volatile float lfe_f;    volatile Uint8 left_rear_u8;    volatile Uint8 right_rear_u8;    volatile Uint8 center_u8;    volatile Uint8 lfe_u8;    volatile float distance_f;    volatile Uint8 distance_u8;    volatile Sint16 room_angle;    volatile int in_use;    volatile int channels;} position_args;static position_args **pos_args_array = NULL;static position_args *pos_args_global = NULL;static int position_channels = 0;/* This just frees up the callback-specific data. */static void _Eff_PositionDone(int channel, void *udata){    if (channel < 0) {        if (pos_args_global != NULL) {            free(pos_args_global);            pos_args_global = NULL;        }    }    else if (pos_args_array[channel] != NULL) {        free(pos_args_array[channel]);        pos_args_array[channel] = NULL;    }}static void _Eff_position_u8(int chan, void *stream, int len, void *udata){    volatile position_args *args = (volatile position_args *) udata;    Uint8 *ptr = (Uint8 *) stream;    int i;        /*         * if there's only a mono channnel (the only way we wouldn't have         *  a len divisible by 2 here), then left_f and right_f are always         *  1.0, and are therefore throwaways.         */    if (len % sizeof (Uint16) != 0) {        *ptr = (Uint8) (((float) *ptr) * args->distance_f);        ptr++;        len--;    }    if (args->room_angle == 0)    for (i = 0; i < len; i += sizeof (Uint8) * 2) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;    }    else for (i = 0; i < len; i += sizeof (Uint8) * 2) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;    }}static void _Eff_position_u8_c4(int chan, void *stream, int len, void *udata){    volatile position_args *args = (volatile position_args *) udata;    Uint8 *ptr = (Uint8 *) stream;    int i;        /*         * if there's only a mono channnel (the only way we wouldn't have         *  a len divisible by 2 here), then left_f and right_f are always         *  1.0, and are therefore throwaways.         */    if (len % sizeof (Uint16) != 0) {        *ptr = (Uint8) (((float) *ptr) * args->distance_f);        ptr++;        len--;    }    if (args->room_angle == 0)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;    }    else if (args->room_angle == 90)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;    }    else if (args->room_angle == 180)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;    }    else if (args->room_angle == 270)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;    }}static void _Eff_position_u8_c6(int chan, void *stream, int len, void *udata){    volatile position_args *args = (volatile position_args *) udata;    Uint8 *ptr = (Uint8 *) stream;    int i;        /*         * if there's only a mono channnel (the only way we wouldn't have         *  a len divisible by 2 here), then left_f and right_f are always         *  1.0, and are therefore throwaways.         */    if (len % sizeof (Uint16) != 0) {        *ptr = (Uint8) (((float) *ptr) * args->distance_f);        ptr++;        len--;    }    if (args->room_angle == 0)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->center_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->lfe_f) * args->distance_f) + 128);        ptr++;    }    else if (args->room_angle == 90)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f/2) + 128)            + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f/2) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->lfe_f) * args->distance_f) + 128);        ptr++;    }    else if (args->room_angle == 180)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f/2) + 128)            + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f/2) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->lfe_f) * args->distance_f) + 128);        ptr++;    }    else if (args->room_angle == 270)    for (i = 0; i < len; i += sizeof (Uint8) * 6) {        /* must adjust the sample so that 0 is the center */        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_rear_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->right_f) * args->distance_f) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_f) * args->distance_f/2) + 128)            + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->left_rear_f) * args->distance_f/2) + 128);        ptr++;        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128))             * args->lfe_f) * args->distance_f) + 128);        ptr++;    }}/* * This one runs about 10.1 times faster than the non-table version, with *  no loss in quality. It does, however, require 64k of memory for the *  lookup table. Also, this will only update position information once per *  call; the non-table version always checks the arguments for each sample, *  in case the user has called Mix_SetPanning() or whatnot again while this *  callback is running. */static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata){    volatile position_args *args = (volatile position_args *) udata;    Uint8 *ptr = (Uint8 *) stream;    Uint32 *p;    int i;    Uint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);    Uint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);    Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8);    if (args->room_angle == 180) {	    Uint8 *temp = l;	    l = r;	    r = temp;    }        /*         * if there's only a mono channnel, then l[] and r[] are always         *  volume 255, and are therefore throwaways. Still, we have to         *  be sure not to overrun the audio buffer...         */    while (len % sizeof (Uint32) != 0) {        *ptr = d[l[*ptr]];        ptr++;        if (args->channels > 1) {            *ptr = d[r[*ptr]];            ptr++;        }        len -= args->channels;    }    p = (Uint32 *) ptr;    for (i = 0; i < len; i += sizeof (Uint32)) {#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)        *(p++) = (d[l[(*p & 0xFF000000) >> 24]] << 24) |                 (d[r[(*p & 0x00FF0000) >> 16]] << 16) |                 (d[l[(*p & 0x0000FF00) >>  8]] <<  8) |                 (d[r[(*p & 0x000000FF)      ]]      ) ;#else        *(p++) = (d[r[(*p & 0xFF000000) >> 24]] << 24) |                 (d[l[(*p & 0x00FF0000) >> 16]] << 16) |                 (d[r[(*p & 0x0000FF00) >>  8]] <<  8) |                 (d[l[(*p & 0x000000FF)      ]]      ) ;#endif    }}static void _Eff_position_s8(int chan, void *stream, int len, void *udata){    volatile position_args *args = (volatile position_args *) udata;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -