Multi_Buffer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Blip_Buffer 0.4.1. http://www.slack.net/~ant/
  2. #include "Multi_Buffer.h"
  3. /* Copyright (C) 2003-2006 Shay Green. This module is free software; you
  4. can redistribute it and/or modify it under the terms of the GNU Lesser
  5. General Public License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version. This
  7. module is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  10. details. You should have received a copy of the GNU Lesser General Public
  11. License along with this module; if not, write to the Free Software Foundation,
  12. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
  13. #include "blargg_source.h"
  14. #ifdef BLARGG_ENABLE_OPTIMIZER
  15. #include BLARGG_ENABLE_OPTIMIZER
  16. #endif
  17. Multi_Buffer::Multi_Buffer( int spf ) : samples_per_frame_( spf )
  18. {
  19. length_ = 0;
  20. sample_rate_ = 0;
  21. channels_changed_count_ = 1;
  22. }
  23. blargg_err_t Multi_Buffer::set_channel_count( int ) { return 0; }
  24. // Silent_Buffer
  25. Silent_Buffer::Silent_Buffer() : Multi_Buffer( 1 ) // 0 channels would probably confuse
  26. {
  27. // TODO: better to use empty Blip_Buffer so caller never has to check for NULL?
  28. chan.left = 0;
  29. chan.center = 0;
  30. chan.right = 0;
  31. }
  32. // Mono_Buffer
  33. Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 )
  34. {
  35. chan.center = &buf;
  36. chan.left = &buf;
  37. chan.right = &buf;
  38. }
  39. Mono_Buffer::~Mono_Buffer() { }
  40. blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec )
  41. {
  42. RETURN_ERR( buf.set_sample_rate( rate, msec ) );
  43. return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() );
  44. }
  45. // Stereo_Buffer
  46. Stereo_Buffer::Stereo_Buffer() : Multi_Buffer( 2 )
  47. {
  48. chan.center = &bufs [0];
  49. chan.left = &bufs [1];
  50. chan.right = &bufs [2];
  51. }
  52. Stereo_Buffer::~Stereo_Buffer() { }
  53. blargg_err_t Stereo_Buffer::set_sample_rate( long rate, int msec )
  54. {
  55. for ( int i = 0; i < buf_count; i++ )
  56. RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) );
  57. return Multi_Buffer::set_sample_rate( bufs [0].sample_rate(), bufs [0].length() );
  58. }
  59. void Stereo_Buffer::clock_rate( long rate )
  60. {
  61. for ( int i = 0; i < buf_count; i++ )
  62. bufs [i].clock_rate( rate );
  63. }
  64. void Stereo_Buffer::bass_freq( int bass )
  65. {
  66. for ( unsigned i = 0; i < buf_count; i++ )
  67. bufs [i].bass_freq( bass );
  68. }
  69. void Stereo_Buffer::clear()
  70. {
  71. stereo_added = 0;
  72. was_stereo = false;
  73. for ( int i = 0; i < buf_count; i++ )
  74. bufs [i].clear();
  75. }
  76. void Stereo_Buffer::end_frame( blip_time_t clock_count )
  77. {
  78. stereo_added = 0;
  79. for ( unsigned i = 0; i < buf_count; i++ )
  80. {
  81. stereo_added |= bufs [i].clear_modified() << i;
  82. bufs [i].end_frame( clock_count );
  83. }
  84. }
  85. long Stereo_Buffer::read_samples( blip_sample_t* out, long count )
  86. {
  87. require( !(count & 1) ); // count must be even
  88. count = (unsigned) count / 2;
  89. long avail = bufs [0].samples_avail();
  90. if ( count > avail )
  91. count = avail;
  92. if ( count )
  93. {
  94. int bufs_used = stereo_added | was_stereo;
  95. //debug_printf( "%X\n", bufs_used );
  96. if ( bufs_used <= 1 )
  97. {
  98. mix_mono( out, count );
  99. bufs [0].remove_samples( count );
  100. bufs [1].remove_silence( count );
  101. bufs [2].remove_silence( count );
  102. }
  103. else if ( bufs_used & 1 )
  104. {
  105. mix_stereo( out, count );
  106. bufs [0].remove_samples( count );
  107. bufs [1].remove_samples( count );
  108. bufs [2].remove_samples( count );
  109. }
  110. else
  111. {
  112. mix_stereo_no_center( out, count );
  113. bufs [0].remove_silence( count );
  114. bufs [1].remove_samples( count );
  115. bufs [2].remove_samples( count );
  116. }
  117. // to do: this might miss opportunities for optimization
  118. if ( !bufs [0].samples_avail() )
  119. {
  120. was_stereo = stereo_added;
  121. stereo_added = 0;
  122. }
  123. }
  124. return count * 2;
  125. }
  126. void Stereo_Buffer::mix_stereo( blip_sample_t* out_, blargg_long count )
  127. {
  128. blip_sample_t* BLIP_RESTRICT out = out_;
  129. int const bass = BLIP_READER_BASS( bufs [1] );
  130. BLIP_READER_BEGIN( left, bufs [1] );
  131. BLIP_READER_BEGIN( right, bufs [2] );
  132. BLIP_READER_BEGIN( center, bufs [0] );
  133. for ( ; count; --count )
  134. {
  135. int c = BLIP_READER_READ( center );
  136. blargg_long l = c + BLIP_READER_READ( left );
  137. blargg_long r = c + BLIP_READER_READ( right );
  138. if ( (BOOST::int16_t) l != l )
  139. l = 0x7FFF - (l >> 24);
  140. BLIP_READER_NEXT( center, bass );
  141. if ( (BOOST::int16_t) r != r )
  142. r = 0x7FFF - (r >> 24);
  143. BLIP_READER_NEXT( left, bass );
  144. BLIP_READER_NEXT( right, bass );
  145. out [0] = l;
  146. out [1] = r;
  147. out += 2;
  148. }
  149. BLIP_READER_END( center, bufs [0] );
  150. BLIP_READER_END( right, bufs [2] );
  151. BLIP_READER_END( left, bufs [1] );
  152. }
  153. void Stereo_Buffer::mix_stereo_no_center( blip_sample_t* out_, blargg_long count )
  154. {
  155. blip_sample_t* BLIP_RESTRICT out = out_;
  156. int const bass = BLIP_READER_BASS( bufs [1] );
  157. BLIP_READER_BEGIN( left, bufs [1] );
  158. BLIP_READER_BEGIN( right, bufs [2] );
  159. for ( ; count; --count )
  160. {
  161. blargg_long l = BLIP_READER_READ( left );
  162. if ( (BOOST::int16_t) l != l )
  163. l = 0x7FFF - (l >> 24);
  164. blargg_long r = BLIP_READER_READ( right );
  165. if ( (BOOST::int16_t) r != r )
  166. r = 0x7FFF - (r >> 24);
  167. BLIP_READER_NEXT( left, bass );
  168. BLIP_READER_NEXT( right, bass );
  169. out [0] = l;
  170. out [1] = r;
  171. out += 2;
  172. }
  173. BLIP_READER_END( right, bufs [2] );
  174. BLIP_READER_END( left, bufs [1] );
  175. }
  176. void Stereo_Buffer::mix_mono( blip_sample_t* out_, blargg_long count )
  177. {
  178. blip_sample_t* BLIP_RESTRICT out = out_;
  179. int const bass = BLIP_READER_BASS( bufs [0] );
  180. BLIP_READER_BEGIN( center, bufs [0] );
  181. for ( ; count; --count )
  182. {
  183. blargg_long s = BLIP_READER_READ( center );
  184. if ( (BOOST::int16_t) s != s )
  185. s = 0x7FFF - (s >> 24);
  186. BLIP_READER_NEXT( center, bass );
  187. out [0] = s;
  188. out [1] = s;
  189. out += 2;
  190. }
  191. BLIP_READER_END( center, bufs [0] );
  192. }