audio_convert.c
author Ozkan Sezer <sezero@users.sourceforge.net>
Wed, 24 May 2017 11:28:05 -0400
changeset 599 f0d57c9b72d8
parent 483 9e761a594df1
permissions -rw-r--r--
timidity.c: fix potential buffer overrun in RWgets (num_read check was off-by-one.) also simplify the procedure a bit. (transplanted from 0c4026dd32742e8b7d33fb96d40fe3c03b02f90c)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
    modify it under the terms of the GNU Library General Public
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
    version 2 of the License, or (at your option) any later version.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    Library General Public License for more details.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    You should have received a copy of the GNU Library General Public
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    Sam Lantinga
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    slouken@devolution.com
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
*/
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
/*
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 * This file was derived from SDL's SDL_audiocvt.c and is an attempt to
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 * address the shortcomings of it.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 * Perhaps we can adapt some good filters from SoX?
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#if HAVE_CONFIG_H
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#  include <config.h>
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#endif
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#include "SDL.h"
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include "SDL_sound.h"
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#define __SDL_SOUND_INTERNAL__
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#include "SDL_sound_internal.h"
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
/* Functions for audio drivers to perform runtime conversion of audio format */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
/*
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
 * Toggle endianness. This filter is, of course, only applied to 16-bit
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
 * audio data.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
    48
static void Sound_ConvertEndian(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    Uint8 *data, tmp;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    /* SNDDBG(("Converting audio endianness\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    data = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        tmp = data[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        data[0] = data[1];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        data[1] = tmp;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
        data += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    *format = (*format ^ 0x1000);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
} /* Sound_ConvertEndian */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
/*
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
 * Toggle signed/unsigned. Apparently this is done by toggling the most
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
 * significant bit of each sample.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
    74
static void Sound_ConvertSign(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    Uint8 *data;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    /* SNDDBG(("Converting audio signedness\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    data = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
        /* 16-bit sound? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    if ((*format & 0xFF) == 16)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
            /* Little-endian? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
        if ((*format & 0x1000) != 0x1000)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
            ++data;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
            *data ^= 0x80;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
            data += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        for (i = cvt->len_cvt; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
            *data++ ^= 0x80;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    *format = (*format ^ 0x8000);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
} /* Sound_ConvertSign */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
/*
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
 * Convert 16-bit to 8-bit. This is done by taking the most significant byte
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
 * of each 16-bit sample.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   111
static void Sound_Convert8(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    Uint8 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    /* SNDDBG(("Converting to 8-bit\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
        /* Little-endian? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    if ((*format & 0x1000) != 0x1000)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
        ++src;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
        *dst = *src;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
        src += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
        dst += 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    *format = ((*format & ~0x9010) | AUDIO_U8);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    cvt->len_cvt /= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
} /* Sound_Convert8 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
/* Convert 8-bit to 16-bit - LSB */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   139
static void Sound_Convert16LSB(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    Uint8 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    /* SNDDBG(("Converting to 16-bit LSB\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    src = cvt->buf + cvt->len_cvt;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    dst = cvt->buf + cvt->len_cvt * 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    for (i = cvt->len_cvt; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
        src -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
        dst -= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
        dst[1] = *src;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
        dst[0] = 0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    *format = ((*format & ~0x0008) | AUDIO_U16LSB);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    cvt->len_cvt *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
} /* Sound_Convert16LSB */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
/* Convert 8-bit to 16-bit - MSB */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   164
static void Sound_Convert16MSB(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    Uint8 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    /* SNDDBG(("Converting to 16-bit MSB\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    src = cvt->buf + cvt->len_cvt;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    dst = cvt->buf + cvt->len_cvt * 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    for (i = cvt->len_cvt; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
        src -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        dst -= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
        dst[0] = *src;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        dst[1] = 0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    *format = ((*format & ~0x0008) | AUDIO_U16MSB);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    cvt->len_cvt *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
} /* Sound_Convert16MSB */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
/* Duplicate a mono channel to both stereo channels */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   189
static void Sound_ConvertStereo(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    /* SNDDBG(("Converting to stereo\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        /* 16-bit sound? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    if ((*format & 0xFF) == 16)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        Uint16 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        src = (Uint16 *) (cvt->buf + cvt->len_cvt);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
        dst = (Uint16 *) (cvt->buf + cvt->len_cvt * 2);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        for (i = cvt->len_cvt/2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
            dst -= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
            src -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
            dst[0] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
            dst[1] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
        } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
    else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        Uint8 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
        src = cvt->buf + cvt->len_cvt;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
        dst = cvt->buf + cvt->len_cvt * 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        for (i = cvt->len_cvt; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
            dst -= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
            src -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
            dst[0] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
            dst[1] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    cvt->len_cvt *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
} /* Sound_ConvertStereo */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
/* Effectively mix right and left channels into a single channel */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   233
static void Sound_ConvertMono(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    Sint32 sample;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    Uint8 *u_src, *u_dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    Sint8 *s_src, *s_dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    /* SNDDBG(("Converting to mono\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    switch (*format)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        case AUDIO_U8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
            u_src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
            u_dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
            for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
                sample = u_src[0] + u_src[1];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
                *u_dst = (sample > 255) ? 255 : sample;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
                u_src += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
                u_dst += 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
        case AUDIO_S8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
            s_src = (Sint8 *) cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
            s_dst = (Sint8 *) cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
            for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
                sample = s_src[0] + s_src[1];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
                if (sample > 127)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
                    *s_dst = 127;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
                else if (sample < -128)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
                    *s_dst = -128;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
                else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
                    *s_dst = sample;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
                s_src += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
                s_dst += 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
        case AUDIO_U16MSB:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
            u_src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
            u_dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
            for (i = cvt->len_cvt / 4; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
                sample = (Uint16) ((u_src[0] << 8) | u_src[1])
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
                       + (Uint16) ((u_src[2] << 8) | u_src[3]);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
                if (sample > 65535)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
                    u_dst[0] = 0xFF;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
                    u_dst[1] = 0xFF;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
                } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
                else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
                    u_dst[1] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
                    sample >>= 8;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
                    u_dst[0] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
                } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
                u_src += 4;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
                u_dst += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        case AUDIO_U16LSB:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
            u_src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
            u_dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
            for (i = cvt->len_cvt / 4; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
                sample = (Uint16) ((u_src[1] << 8) | u_src[0])
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
                       + (Uint16) ((u_src[3] << 8) | u_src[2]);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
                if (sample > 65535)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
                    u_dst[0] = 0xFF;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
                    u_dst[1] = 0xFF;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
                } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
                else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
                    u_dst[0] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
                    sample >>= 8;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
                    u_dst[1] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
                } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
                u_src += 4;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
                u_dst += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
        case AUDIO_S16MSB:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
            u_src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
            u_dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
            for (i = cvt->len_cvt / 4; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
                sample = (Sint16) ((u_src[0] << 8) | u_src[1])
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
                       + (Sint16) ((u_src[2] << 8) | u_src[3]);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
                if (sample > 32767)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
                    u_dst[0] = 0x7F;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
                    u_dst[1] = 0xFF;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
                } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
                else if (sample < -32768)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
                    u_dst[0] = 0x80;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
                    u_dst[1] = 0x00;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
                } /* else if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
                else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
                    u_dst[1] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
                    sample >>= 8;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
                    u_dst[0] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
                } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
                u_src += 4;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
                u_dst += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        case AUDIO_S16LSB:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
            u_src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
            u_dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
            for (i = cvt->len_cvt / 4; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
                sample = (Sint16) ((u_src[1] << 8) | u_src[0])
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
                       + (Sint16) ((u_src[3] << 8) | u_src[2]);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
                if (sample > 32767)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
                    u_dst[1] = 0x7F;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
                    u_dst[0] = 0xFF;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
                } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
                else if (sample < -32768)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
                    u_dst[1] = 0x80;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
                    u_dst[0] = 0x00;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
                } /* else if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
                else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
                    u_dst[0] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
                    sample >>= 8;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
                    u_dst[1] = (sample & 0xFF);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
                } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
                u_src += 4;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
                u_dst += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    } /* switch */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    cvt->len_cvt /= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
} /* Sound_ConvertMono */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
/* Convert rate up by multiple of 2 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   389
static void Sound_RateMUL2(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    Uint8 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    /* SNDDBG(("Converting audio rate * 2\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
    src = cvt->buf + cvt->len_cvt;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    dst = cvt->buf + cvt->len_cvt*2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
        /* 8- or 16-bit sound? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    switch (*format & 0xFF)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
        case 8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
            for (i = cvt->len_cvt; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
                src -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
                dst -= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
                dst[0] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
                dst[1] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
        case 16:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
            for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
                src -= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
                dst -= 4;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
                dst[0] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
                dst[1] = src[1];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
                dst[2] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
                dst[3] = src[1];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    } /* switch */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    cvt->len_cvt *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
} /* Sound_RateMUL2 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
/* Convert rate down by multiple of 2 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   431
static void Sound_RateDIV2(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    int i;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    Uint8 *src, *dst;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    /* SNDDBG(("Converting audio rate / 2\n")); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    src = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    dst = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
        /* 8- or 16-bit sound? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    switch (*format & 0xFF)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
        case 8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
            for (i = cvt->len_cvt / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
                dst[0] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
                src += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
                dst += 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
            } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
        case 16:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
            for (i = cvt->len_cvt / 4; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
                dst[0] = src[0];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
                dst[1] = src[1];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
                src += 4;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
                dst += 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
            }
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
            break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    } /* switch */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    cvt->len_cvt /= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
} /* Sound_RateDIV2 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
/* Very slow rate conversion routine */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 141
diff changeset
   470
static void Sound_RateSLOW(Sound_AudioCVT *cvt, Uint16 *format)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    double ipos;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
    int i, clen;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    Uint8 *output8;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    Uint16 *output16;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    /* SNDDBG(("Converting audio rate * %4.4f\n", 1.0/cvt->rate_incr)); */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
    clen = (int) ((double) cvt->len_cvt / cvt->rate_incr);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
    if (cvt->rate_incr > 1.0)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
            /* 8- or 16-bit sound? */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
        switch (*format & 0xFF)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
            case 8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
                output8 = cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
                ipos = 0.0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
                for (i = clen; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
                    *output8 = cvt->buf[(int) ipos];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
                    ipos += cvt->rate_incr;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
                    output8 += 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
                } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
            case 16:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
                output16 = (Uint16 *) cvt->buf;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
                clen &= ~1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
                ipos = 0.0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
                for (i = clen / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
                    *output16 = ((Uint16 *) cvt->buf)[(int) ipos];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
                    ipos += cvt->rate_incr;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
                    output16 += 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
                } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        } /* switch */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
    else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
            /* 8- or 16-bit sound */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
        switch (*format & 0xFF)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
            case 8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
                output8 = cvt->buf + clen;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
                ipos = (double) cvt->len_cvt;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
                for (i = clen; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
                    ipos -= cvt->rate_incr;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
                    output8 -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
                    *output8 = cvt->buf[(int) ipos];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
                } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
            case 16:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
                clen &= ~1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
                output16 = (Uint16 *) (cvt->buf + clen);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
                ipos = (double) cvt->len_cvt / 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
                for (i = clen / 2; i; --i)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
                {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
                    ipos -= cvt->rate_incr;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
                    output16 -= 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
                    *output16 = ((Uint16 *) cvt->buf)[(int) ipos];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
                } /* for */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
        } /* switch */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
    } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
    cvt->len_cvt = clen;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
} /* Sound_RateSLOW */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
int Sound_ConvertAudio(Sound_AudioCVT *cvt)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    Uint16 format;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
        /* Make sure there's data to convert */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    if (cvt->buf == NULL)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    {
387
fb519e6028e3 Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents: 382
diff changeset
   554
        __Sound_SetError("No buffer allocated for conversion");
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
        return(-1);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        /* Return okay if no conversion is necessary */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
    cvt->len_cvt = cvt->len;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
    if (cvt->filters[0] == NULL)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        return(0);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
        /* Set up the conversion and go! */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
    format = cvt->src_format;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
    for (cvt->filter_index = 0; cvt->filters[cvt->filter_index];
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
         cvt->filter_index++)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
        cvt->filters[cvt->filter_index](cvt, &format);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
    }
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    return(0);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
} /* Sound_ConvertAudio */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
/*
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
 * Creates a set of audio filters to convert from one format to another. 
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
 * Returns -1 if the format conversion is not supported, or 1 if the
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
 * audio filter is set up.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
 */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
int Sound_BuildAudioCVT(Sound_AudioCVT *cvt,
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
                        Uint16 src_format, Uint8 src_channels, Uint32 src_rate,
382
ce998ee6194f Sync'd with latest altcvt.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
   582
                        Uint16 dst_format, Uint8 dst_channels, Uint32 dst_rate,
ce998ee6194f Sync'd with latest altcvt.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
   583
                        Uint32 dst_size)
141
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
{
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
        /* Start off with no conversion necessary */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
    cvt->needed = 0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    cvt->filter_index = 0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
    cvt->filters[0] = NULL;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
    cvt->len_mult = 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
    cvt->len_ratio = 1.0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
        /* First filter:  Endian conversion from src to dst */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    if ((src_format & 0x1000) != (dst_format & 0x1000) &&
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
       ((src_format & 0xff) != 8))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
        SNDDBG(("Adding filter: Sound_ConvertEndian\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
        cvt->filters[cvt->filter_index++] = Sound_ConvertEndian;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
	
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
        /* Second filter: Sign conversion -- signed/unsigned */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
    if ((src_format & 0x8000) != (dst_format & 0x8000))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
        SNDDBG(("Adding filter: Sound_ConvertSign\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
        cvt->filters[cvt->filter_index++] = Sound_ConvertSign;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
        /* Next filter:  Convert 16 bit <--> 8 bit PCM. */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
    if ((src_format & 0xFF) != (dst_format & 0xFF))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
        switch (dst_format & 0x10FF)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
            case AUDIO_U8:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
                SNDDBG(("Adding filter: Sound_Convert8\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
                cvt->filters[cvt->filter_index++] = Sound_Convert8;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
                cvt->len_ratio /= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
            case AUDIO_U16LSB:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
                SNDDBG(("Adding filter: Sound_Convert16LSB\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
                cvt->filters[cvt->filter_index++] = Sound_Convert16LSB;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
                cvt->len_mult *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
                cvt->len_ratio *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
            case AUDIO_U16MSB:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
                SNDDBG(("Adding filter: Sound_Convert16MSB\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
                cvt->filters[cvt->filter_index++] = Sound_Convert16MSB;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
                cvt->len_mult *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
                cvt->len_ratio *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
                break;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
        } /* switch */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
        /* Next filter:  Mono/Stereo conversion */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
    if (src_channels != dst_channels)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
        while ((src_channels * 2) <= dst_channels)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
            SNDDBG(("Adding filter: Sound_ConvertStereo\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
            cvt->filters[cvt->filter_index++] = Sound_ConvertStereo;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
            cvt->len_mult *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
            src_channels *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
            cvt->len_ratio *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
        } /* while */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
        /* This assumes that 4 channel audio is in the format:
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
         *     Left {front/back} + Right {front/back}
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
         * so converting to L/R stereo works properly.
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
         */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
        while (((src_channels % 2) == 0) &&
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
               ((src_channels / 2) >= dst_channels))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
            SNDDBG(("Adding filter: Sound_ConvertMono\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
            cvt->filters[cvt->filter_index++] = Sound_ConvertMono;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
            src_channels /= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
            cvt->len_ratio /= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
        } /* while */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
        if ( src_channels != dst_channels ) {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
            /* Uh oh.. */;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
        } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    /* Do rate conversion */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
    cvt->rate_incr = 0.0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
    if ((src_rate / 100) != (dst_rate / 100))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
        Uint32 hi_rate, lo_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
        int len_mult;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
        double len_ratio;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
        void (*rate_cvt)(Sound_AudioCVT *cvt, Uint16 *format);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
        if (src_rate > dst_rate)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
            hi_rate = src_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
            lo_rate = dst_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
            SNDDBG(("Adding filter: Sound_RateDIV2\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
            rate_cvt = Sound_RateDIV2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
            len_mult = 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
            len_ratio = 0.5;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
        } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
        else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
            hi_rate = dst_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
            lo_rate = src_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
            SNDDBG(("Adding filter: Sound_RateMUL2\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
            rate_cvt = Sound_RateMUL2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
            len_mult = 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
            len_ratio = 2.0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
        } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
            /* If hi_rate = lo_rate*2^x then conversion is easy */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
        while (((lo_rate * 2) / 100) <= (hi_rate / 100))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
            cvt->filters[cvt->filter_index++] = rate_cvt;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
            cvt->len_mult *= len_mult;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
            lo_rate *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
            cvt->len_ratio *= len_ratio;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
        } /* while */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
            /* We may need a slow conversion here to finish up */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
        if ((lo_rate / 100) != (hi_rate / 100))
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
        {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
            if (src_rate < dst_rate)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
                cvt->rate_incr = (double) lo_rate / hi_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
                cvt->len_mult *= 2;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
                cvt->len_ratio /= cvt->rate_incr;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
            } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
            else
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
            {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
                cvt->rate_incr = (double) hi_rate / lo_rate;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
                cvt->len_ratio *= cvt->rate_incr;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
            } /* else */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
            SNDDBG(("Adding filter: Sound_RateSLOW\n"));
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
            cvt->filters[cvt->filter_index++] = Sound_RateSLOW;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
        } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
        /* Set up the filter information */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
    if (cvt->filter_index != 0)
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
    {
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
        cvt->needed = 1;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
        cvt->src_format = src_format;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
        cvt->dst_format = dst_format;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
        cvt->len = 0;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
        cvt->buf = NULL;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
        cvt->filters[cvt->filter_index] = NULL;
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
    } /* if */
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
    return(cvt->needed);
907e3776d2f4 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
} /* Sound_BuildAudioCVT */
465
636796aed4e2 Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 387
diff changeset
   733
636796aed4e2 Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 387
diff changeset
   734
/* end of audio_convert.c ... */
636796aed4e2 Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 387
diff changeset
   735