hx3d  1
2D/3D Simple Game Framework
s16_converter.cpp
1 /*
2  Audio Converter: Signed Short.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/audio/converters/s16_converter.hpp"
22 
23 namespace hx3d {
24 namespace audio {
25 
26 S16Converter::S16Converter(): Effect() {}
27 S16Converter::~S16Converter() {}
28 
29 void S16Converter::onFunction(const int channel, const void* stream, const int length) {
30  if (!_stream)
31  _stream = new short[length/2];
32  _length = length/2;
33 
34  short* my_stream = (short*)_stream;
35  char* str = (char*)stream;
36 
37  for (int i = 0; i < length; i += 2) {
38  char a = str[i+1];
39  char b = str[i];
40  unsigned char ua = a < 0 ? 127 - a : a;
41  unsigned char ub = b < 0 ? 127 - b : b;
42  unsigned short us = (ua << 8 | ub);
43  short s = us > 32767 ? -(us - 32767) : us;
44 
45  my_stream[i/2] = s;
46  }
47 
48  _processed = true;
49 }
50 
51 void S16Converter::onDone(const int channel)
52 {}
53 
55  return (short*)_stream;
56 }
57 
58 } /* audio */
59 } /* hx3d */
hx3d framework namespace
Definition: audio.hpp:26
virtual void onFunction(const int channel, const void *stream, const int length) override
Audio effect function.
short * getS16Stream()
Get the stream in signed short format.
virtual void onDone(const int channel) override
Audio effect done function.