aRts audio server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

audiotobytestream_impl.cc 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (C) 2000 Stefan Westerfeld
  3. stefan@space.twc.de
  4. 2001, 2003 Matthias Kretz
  5. kretz@kde.org
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  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. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public License
  15. along with this library; see the file COPYING.LIB. If not, write to
  16. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. Boston, MA 02110-1301, USA.
  18. */
  19. #include "artsflow.h"
  20. #include "stdsynthmodule.h"
  21. #include "debug.h"
  22. #include <vector>
  23. #include <iostream>
  24. #include <math.h>
  25. #undef DEBUG_MESSAGES
  26. using namespace std;
  27. using namespace Arts;
  28. namespace Arts {
  29. class AudioToByteStream_impl : public AudioToByteStream_skel,
  30. public StdSynthModule
  31. {
  32. long _samplingRate, _channels, _bits;
  33. long sampleSize;
  34. double step;
  35. bool interpolate;
  36. vector<float> leftbuffer;
  37. vector<float> rightbuffer;
  38. int range;
  39. double _pos;
  40. protected:
  41. void updateSampleSize()
  42. {
  43. sampleSize = _channels * _bits / 8;
  44. }
  45. public:
  46. AudioToByteStream_impl() :
  47. _pos(0)
  48. {
  49. samplingRate(44100);
  50. channels(2);
  51. bits(16);
  52. }
  53. long samplingRate() { return _samplingRate; }
  54. void samplingRate(long newRate) {
  55. double newStep = samplingRateFloat / (float)newRate;
  56. arts_return_if_fail(newStep > 0);
  57. _samplingRate = newRate;
  58. step = newStep;
  59. double delta = step - floor(step);
  60. interpolate = fabs(delta) > 0.001;
  61. #ifdef DEBUG_MESSAGES
  62. arts_debug( "samplingRate(%i): step = %e, delta = %e, interpolate: %i", newRate, step, delta, interpolate );
  63. #endif
  64. }
  65. long channels() { return _channels; }
  66. void channels(long newChannels) {
  67. arts_return_if_fail(newChannels == 1 || newChannels == 2);
  68. _channels = newChannels;
  69. updateSampleSize();
  70. }
  71. long bits() { return _bits; }
  72. void bits(long newBits) {
  73. arts_return_if_fail(newBits == 8 || newBits == 16);
  74. _bits = newBits;
  75. range = (newBits == 8 ) ? 128 : 32768;
  76. updateSampleSize();
  77. }
  78. void calculateBlock(unsigned long samples)
  79. {
  80. leftbuffer.resize( 1 + samples );
  81. rightbuffer.resize( 1 + samples );
  82. for( unsigned long i = 0; i < samples; ++i ) {
  83. leftbuffer[1 + i] = (left[i] > 1.0f) ? 1.0f : (left[i] < -1.0f) ? -1.0f : left[i];
  84. rightbuffer[1 + i] = (right[i] > 1.0f) ? 1.0f : (right[i] < -1.0f) ? -1.0f : right[i];
  85. }
  86. int samplestosend = (int)ceil(leftbuffer.size() / step);
  87. DataPacket<mcopbyte> *packet = outdata.allocPacket( samplestosend * sampleSize );
  88. #ifdef DEBUG_MESSAGES
  89. arts_debug( "calculateBlock(%i): send %i samples", samples, samplestosend );
  90. #endif
  91. int processed = 0;
  92. if( interpolate ) {
  93. double pos = 0;
  94. if( _channels == 2 ) {
  95. if( _bits == 16 ) {
  96. while( _pos < (double)leftbuffer.size() - 1 ) {
  97. double error = modf(_pos, &pos);
  98. int intpos = (int)pos;
  99. #ifdef DEBUG_MESSAGES
  100. arts_debug( "pos=%i, error=%e", intpos, error );
  101. #endif
  102. long leftchannel = long((leftbuffer[intpos] * (1.0 - error) + leftbuffer[intpos + 1] * error) * 32768) + 32768;
  103. long rightchannel = long((rightbuffer[intpos] * (1.0 - error) + rightbuffer[intpos + 1] * error) * 32768) + 32768;
  104. packet->contents[processed ] = leftchannel;
  105. packet->contents[processed + 1] = (leftchannel >> 8) - 128;
  106. packet->contents[processed + 2] = rightchannel;
  107. packet->contents[processed + 3] = (rightchannel >> 8) - 128;
  108. _pos += step;
  109. processed += 4;
  110. }
  111. } else if( _bits == 8 ) {
  112. while( _pos < (double)leftbuffer.size() - 1 ) {
  113. double error = modf(_pos, &pos);
  114. int intpos = (int)pos;
  115. long leftchannel = long((leftbuffer[intpos] * (1.0 - error) + leftbuffer[intpos + 1] * error) * 128) + 128;
  116. long rightchannel = long((rightbuffer[intpos] * (1.0 - error) + rightbuffer[intpos + 1] * error) * 128) + 128;
  117. packet->contents[processed ] = leftchannel;
  118. packet->contents[processed + 1] = rightchannel;
  119. _pos += step;
  120. processed += 2;
  121. }
  122. }
  123. } else {
  124. if( _bits == 16 ) {
  125. while( _pos < (double)leftbuffer.size() - 1 ) {
  126. double error = modf(_pos, &pos);
  127. int intpos = (int)pos;
  128. long leftchannel = long(((leftbuffer[intpos] + rightbuffer[intpos]) * (1.0 - error) + (leftbuffer[intpos + 1] + rightbuffer[intpos + 1]) * error) * 16384) + 32768;
  129. packet->contents[processed ] = leftchannel;
  130. packet->contents[processed + 1] = (leftchannel >> 8) - 128;
  131. _pos += step;
  132. processed += 2;
  133. }
  134. } else if( _bits == 8 ) {
  135. while( _pos < (double)leftbuffer.size() - 1 ) {
  136. double error = modf(_pos, &pos);
  137. int intpos = (int)pos;
  138. long leftchannel = long(((leftbuffer[intpos] + rightbuffer[intpos]) * (1.0 - error) + (leftbuffer[intpos + 1] + rightbuffer[intpos + 1]) * error) * 64) + 128;
  139. packet->contents[processed] = leftchannel;
  140. _pos += step;
  141. ++processed;
  142. }
  143. }
  144. }
  145. } else {
  146. if( _channels == 2 ) {
  147. if( _bits == 16 ) {
  148. while( _pos < (double)leftbuffer.size() - 1 ) {
  149. int intpos = (int)_pos;
  150. long leftchannel = long(leftbuffer[intpos] * 32768) + 32768;
  151. long rightchannel = long(rightbuffer[intpos] * 32768) + 32768;
  152. packet->contents[processed ] = leftchannel;
  153. packet->contents[processed + 1] = (leftchannel >> 8) - 128;
  154. packet->contents[processed + 2] = rightchannel;
  155. packet->contents[processed + 3] = (rightchannel >> 8) - 128;
  156. _pos += step;
  157. processed += 4;
  158. }
  159. } else if( _bits == 8 ) {
  160. while( _pos < (double)leftbuffer.size() - 1 ) {
  161. int intpos = (int)_pos;
  162. long leftchannel = long(leftbuffer[intpos] * 128) + 128;
  163. long rightchannel = long(rightbuffer[intpos] * 128) + 128;
  164. packet->contents[processed ] = leftchannel;
  165. packet->contents[processed + 1] = rightchannel;
  166. _pos += step;
  167. processed += 2;
  168. }
  169. }
  170. } else {
  171. if( _bits == 16 ) {
  172. while( _pos < (double)leftbuffer.size() - 1 ) {
  173. int intpos = (int)_pos;
  174. long leftchannel = long((leftbuffer[intpos] + rightbuffer[intpos]) * 16384) + 32768;
  175. packet->contents[processed ] = leftchannel;
  176. packet->contents[processed + 1] = (leftchannel >> 8) - 128;
  177. _pos += step;
  178. processed += 2;
  179. }
  180. } else if( _bits == 8 ) {
  181. while( _pos < (double)leftbuffer.size() - 1 ) {
  182. int intpos = (int)_pos;
  183. long leftchannel = long((leftbuffer[intpos] + rightbuffer[intpos]) * 64) + 128;
  184. packet->contents[processed] = leftchannel;
  185. _pos += step;
  186. ++processed;
  187. }
  188. }
  189. }
  190. }
  191. leftbuffer[0] = leftbuffer.back();
  192. rightbuffer[0] = rightbuffer.back();
  193. _pos = _pos - floor(_pos);
  194. packet->size = processed;
  195. packet->send();
  196. #ifdef DEBUG_MESSAGES
  197. arts_debug( "calculateBlock(%i): %i samples sent, _pos = %e", samples, processed / sampleSize, _pos );
  198. #endif
  199. }
  200. };
  201. REGISTER_IMPLEMENTATION(AudioToByteStream_impl);
  202. }