src/audio/SDL_audiotypecvt.c
author Sam Lantinga
Mon, 09 Jan 2017 11:58:01 -0800
changeset 10802 6afc9b833867
parent 10799 234f71894a52
child 10814 938218064f67
permissions -rw-r--r--
We only need the first few keymaps corresponding to the following constants:
K_NORMTAB, K_SHIFTTAB, K_ALTTAB, K_ALTSHIFTTAB

In the normal case we'll load all the keymaps from the kernel, but this reduces the size of the SDL library for the fallback case when we can't get to the tty.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "../SDL_internal.h"
    23 #include "SDL_audio.h"
    24 #include "SDL_audio_c.h"
    25 #include "SDL_assert.h"
    26 
    27 #define DIVBY127 0.0078740157480315f
    28 #define DIVBY32767 3.05185094759972e-05f
    29 #define DIVBY2147483647 4.6566128752458e-10f
    30 
    31 void SDLCALL
    32 SDL_Convert_S8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    33 {
    34     const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
    35     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    36     int i;
    37 
    38     LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32");
    39 
    40     for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    41         *dst = (((float) ((Sint8) *src)) * DIVBY127);
    42     }
    43 
    44     cvt->len_cvt *= 4;
    45     if (cvt->filters[++cvt->filter_index]) {
    46         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    47     }
    48 }
    49 
    50 void SDLCALL
    51 SDL_Convert_U8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    52 {
    53     const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
    54     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    55     int i;
    56 
    57     LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32");
    58 
    59     for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    60         *dst = ((((float) *src) * DIVBY127) - 1.0f);
    61     }
    62 
    63     cvt->len_cvt *= 4;
    64     if (cvt->filters[++cvt->filter_index]) {
    65         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    66     }
    67 }
    68 
    69 void SDLCALL
    70 SDL_Convert_S16_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    71 {
    72     const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
    73     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
    74     int i;
    75 
    76     LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32");
    77 
    78     for (i = cvt->len_cvt / sizeof (Sint16); i; --i, --src, --dst) {
    79         *dst = (((float) *src) * DIVBY32767);
    80     }
    81 
    82     cvt->len_cvt *= 2;
    83     if (cvt->filters[++cvt->filter_index]) {
    84         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    85     }
    86 }
    87 
    88 void SDLCALL
    89 SDL_Convert_U16_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    90 {
    91     const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
    92     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
    93     int i;
    94 
    95     LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32");
    96 
    97     for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
    98         *dst = ((((float) *src) * DIVBY32767) - 1.0f);
    99     }
   100 
   101     cvt->len_cvt *= 2;
   102     if (cvt->filters[++cvt->filter_index]) {
   103         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   104     }
   105 }
   106 
   107 void SDLCALL
   108 SDL_Convert_S32_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   109 {
   110     const Uint32 *src = (const Uint32 *) cvt->buf;
   111     float *dst = (float *) cvt->buf;
   112     int i;
   113 
   114     LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32");
   115 
   116     for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) {
   117         *dst = (((float) *src) * DIVBY2147483647);
   118     }
   119 
   120     if (cvt->filters[++cvt->filter_index]) {
   121         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   122     }
   123 }
   124 
   125 void SDLCALL
   126 SDL_Convert_F32_to_S8(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   127 {
   128     const float *src = (const float *) cvt->buf;
   129     Sint8 *dst = (Sint8 *) cvt->buf;
   130     int i;
   131 
   132     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8");
   133 
   134     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   135         *dst = (Sint8) (*src * 127.0f);
   136     }
   137 
   138     cvt->len_cvt /= 4;
   139     if (cvt->filters[++cvt->filter_index]) {
   140         cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
   141     }
   142 }
   143 
   144 void SDLCALL
   145 SDL_Convert_F32_to_U8(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   146 {
   147     const float *src = (const float *) cvt->buf;
   148     Uint8 *dst = (Uint8 *) cvt->buf;
   149     int i;
   150 
   151     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8");
   152 
   153     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   154         *dst = (Uint8) ((*src + 1.0f) * 127.0f);
   155     }
   156 
   157     cvt->len_cvt /= 4;
   158     if (cvt->filters[++cvt->filter_index]) {
   159         cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
   160     }
   161 }
   162 
   163 void SDLCALL
   164 SDL_Convert_F32_to_S16(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   165 {
   166     const float *src = (const float *) cvt->buf;
   167     Sint16 *dst = (Sint16 *) cvt->buf;
   168     int i;
   169 
   170     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16");
   171 
   172     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   173         *dst = (Sint16) (*src * 32767.0f);
   174     }
   175 
   176     cvt->len_cvt /= 2;
   177     if (cvt->filters[++cvt->filter_index]) {
   178         cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
   179     }
   180 }
   181 
   182 void SDLCALL
   183 SDL_Convert_F32_to_U16(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   184 {
   185     const float *src = (const float *) cvt->buf;
   186     Uint16 *dst = (Uint16 *) cvt->buf;
   187     int i;
   188 
   189     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16");
   190 
   191     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   192         *dst = (Uint16) ((*src + 1.0f) * 32767.0f);
   193     }
   194 
   195     cvt->len_cvt /= 2;
   196     if (cvt->filters[++cvt->filter_index]) {
   197         cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
   198     }
   199 }
   200 
   201 void SDLCALL
   202 SDL_Convert_F32_to_S32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   203 {
   204     const float *src = (const float *) cvt->buf;
   205     Sint32 *dst = (Sint32 *) cvt->buf;
   206     int i;
   207 
   208     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32");
   209 
   210     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   211         *dst = (Sint32) (*src * 2147483647.0);
   212     }
   213 
   214     if (cvt->filters[++cvt->filter_index]) {
   215         cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
   216     }
   217 }
   218 
   219 /* vi: set ts=4 sw=4 expandtab: */