File: pcm.c

package info (click to toggle)
brltty 5.2~20141018-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, jessie-kfreebsd-proposed-updates
  • size: 22,640 kB
  • sloc: ansic: 108,275; sh: 6,339; java: 4,473; xml: 2,650; makefile: 1,841; tcl: 1,478; awk: 599; ml: 293; python: 250
file content (165 lines) | stat: -rw-r--r-- 3,459 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2014 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any
 * later version. Please see the file LICENSE-GPL for details.
 *
 * Web Page: http://mielke.cc/brltty/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include "pcm.h"

typedef struct {
  unsigned char size;
} PcmSampleEntry;

static const PcmSampleEntry pcmSampleTable[] = {
  [PCM_FMT_U8] = {
    .size = 1
  },

  [PCM_FMT_S8] = {
    .size = 1
  },

  [PCM_FMT_U16B] = {
    .size = 2
  },

  [PCM_FMT_S16B] = {
    .size = 2
  },

  [PCM_FMT_U16L] = {
    .size = 2
  },

  [PCM_FMT_S16L] = {
    .size = 2
  },

  [PCM_FMT_U16N] = {
    .size = 2
  },

  [PCM_FMT_S16N] = {
    .size = 2
  },

  [PCM_FMT_ULAW] = {
    .size = 1
  },

  [PCM_FMT_ALAW] = {
    .size = 1
  }
};

size_t
getPcmSampleLength (PcmAmplitudeFormat format) {
  return (format < ARRAY_COUNT(pcmSampleTable))? pcmSampleTable[format].size: 0;
}

size_t
makePcmSample (
  PcmAmplitudeFormat format, int16_t amplitude,
  void *buffer, size_t size
) {
  size_t length = getPcmSampleLength(format);

  if (size >= length) {
    typedef union {
      unsigned char bytes[4];
      int16_t s16N;
    } Sample;

    Sample *sample = buffer;
    const int U2S = 0X8000;

    switch (format) {
      case PCM_FMT_U8:
        amplitude += U2S;
      case PCM_FMT_S8:
        sample->bytes[0] = amplitude >> 8;
        break;

      case PCM_FMT_U16B:
        amplitude += U2S;
      case PCM_FMT_S16B:
        sample->bytes[0] = amplitude >> 8;
        sample->bytes[1] = amplitude;
        break;

      case PCM_FMT_U16L:
        amplitude += U2S;
      case PCM_FMT_S16L:
        sample->bytes[0] = amplitude;
        sample->bytes[1] = amplitude >> 8;
        break;

      case PCM_FMT_U16N:
        amplitude += U2S;
      case PCM_FMT_S16N:
        sample->s16N = amplitude;
        break;

      case PCM_FMT_ULAW: {
        int negative = amplitude < 0;
        int exponent = 0X7;
        unsigned char value;
        const unsigned int bias = 0X84;
        const unsigned int clip = 0X7FFF - bias;

        if (negative) amplitude = -amplitude;
        if (amplitude > clip) amplitude = clip;
        amplitude += bias;

        while ((exponent > 0) && !(amplitude & 0X4000)) {
          amplitude <<= 1;
          --exponent;
        }

        value = (exponent << 4) | ((amplitude >> 10) & 0X0F);
        if (negative) value |= 0X80;
        sample->bytes[0] = ~value;
        break;
      }

      case PCM_FMT_ALAW: {
        int negative = amplitude < 0;
        int exponent = 0X7;
        unsigned char value;

        if (negative) amplitude = -amplitude;

        while ((exponent > 0) && !(amplitude & 0X4000)) {
          amplitude <<= 1;
          --exponent;
        }

        if (!exponent) amplitude >>= 1;
        value = (exponent << 4) | ((amplitude >> 10) & 0X0F);
        if (negative) value |= 0X80;
        sample->bytes[0] = value ^ 0X55;
        break;
      }

      default:
        length = 0;
        break;
    }
  }

  return length;
}