Bitcrush.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Bitcrush.cpp - A native bitcrusher
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include "Bitcrush.h"
  26. #include "embed.h"
  27. #include "plugin_export.h"
  28. const int OS_RATE = 5;
  29. const float OS_RATIO = 1.0f / OS_RATE;
  30. const float CUTOFF_RATIO = 0.353553391f;
  31. const int SILENCEFRAMES = 10;
  32. const float OS_RESAMPLE [5] = { 0.0001490062883964112, 0.1645978376763992, 0.6705063120704088,
  33. 0.1645978376763992, 0.0001490062883964112 };
  34. extern "C"
  35. {
  36. Plugin::Descriptor PLUGIN_EXPORT bitcrush_plugin_descriptor =
  37. {
  38. STRINGIFY( PLUGIN_NAME ),
  39. "Bitcrush",
  40. QT_TRANSLATE_NOOP( "PluginBrowser", "An oversampling bitcrusher" ),
  41. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  42. 0x0100,
  43. Plugin::Effect,
  44. new PluginPixmapLoader( "logo" ),
  45. NULL,
  46. NULL
  47. };
  48. }
  49. BitcrushEffect::BitcrushEffect( Model * parent, const Descriptor::SubPluginFeatures::Key * key ) :
  50. Effect( &bitcrush_plugin_descriptor, parent, key ),
  51. m_controls( this ),
  52. m_sampleRate( Engine::mixer()->processingSampleRate() ),
  53. m_filter( m_sampleRate )
  54. {
  55. m_buffer = MM_ALLOC( sampleFrame, Engine::mixer()->framesPerPeriod() * OS_RATE );
  56. m_filter.setLowpass( m_sampleRate * ( CUTOFF_RATIO * OS_RATIO ) );
  57. m_needsUpdate = true;
  58. m_bitCounterL = 0.0f;
  59. m_bitCounterR = 0.0f;
  60. m_left = 0.0f;
  61. m_right = 0.0f;
  62. m_silenceCounter = 0;
  63. }
  64. BitcrushEffect::~BitcrushEffect()
  65. {
  66. MM_FREE( m_buffer );
  67. }
  68. void BitcrushEffect::sampleRateChanged()
  69. {
  70. m_sampleRate = Engine::mixer()->processingSampleRate();
  71. m_filter.setSampleRate( m_sampleRate );
  72. m_filter.setLowpass( m_sampleRate * ( CUTOFF_RATIO * OS_RATIO ) );
  73. m_needsUpdate = true;
  74. }
  75. inline float BitcrushEffect::depthCrush( float in )
  76. {
  77. return roundf( in * (float) m_levels ) * m_levelsRatio;
  78. }
  79. inline float BitcrushEffect::noise( float amt )
  80. {
  81. return fastRandf( amt * 2.0f ) - amt;
  82. }
  83. bool BitcrushEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  84. {
  85. if( !isEnabled() || !isRunning () )
  86. {
  87. return( false );
  88. }
  89. // update values
  90. if( m_needsUpdate || m_controls.m_rateEnabled.isValueChanged() )
  91. {
  92. m_rateEnabled = m_controls.m_rateEnabled.value();
  93. m_bitCounterL = 0.0f;
  94. m_bitCounterR = 0.0f;
  95. }
  96. if( m_needsUpdate || m_controls.m_depthEnabled.isValueChanged() )
  97. {
  98. m_depthEnabled = m_controls.m_depthEnabled.value();
  99. }
  100. if( m_needsUpdate || m_controls.m_rate.isValueChanged() || m_controls.m_stereoDiff.isValueChanged() )
  101. {
  102. const float rate = m_controls.m_rate.value();
  103. const float diff = m_controls.m_stereoDiff.value() * 0.005 * rate;
  104. m_rateCoeffL = ( m_sampleRate * OS_RATE ) / ( rate - diff );
  105. m_rateCoeffR = ( m_sampleRate * OS_RATE ) / ( rate + diff );
  106. m_bitCounterL = 0.0f;
  107. m_bitCounterR = 0.0f;
  108. }
  109. if( m_needsUpdate || m_controls.m_levels.isValueChanged() )
  110. {
  111. m_levels = m_controls.m_levels.value();
  112. m_levelsRatio = 1.0f / (float) m_levels;
  113. }
  114. if( m_needsUpdate || m_controls.m_inGain.isValueChanged() )
  115. {
  116. m_inGain = dbfsToAmp( m_controls.m_inGain.value() );
  117. }
  118. if( m_needsUpdate || m_controls.m_outGain.isValueChanged() )
  119. {
  120. m_outGain = dbfsToAmp( m_controls.m_outGain.value() );
  121. }
  122. if( m_needsUpdate || m_controls.m_outClip.isValueChanged() )
  123. {
  124. m_outClip = dbfsToAmp( m_controls.m_outClip.value() );
  125. }
  126. m_needsUpdate = false;
  127. const float noiseAmt = m_controls.m_inNoise.value() * 0.01f;
  128. // read input buffer and write it to oversampled buffer
  129. if( m_rateEnabled ) // rate crushing enabled so do that
  130. {
  131. for( int f = 0; f < frames; ++f )
  132. {
  133. for( int o = 0; o < OS_RATE; ++o )
  134. {
  135. m_buffer[f * OS_RATE + o][0] = m_left;
  136. m_buffer[f * OS_RATE + o][1] = m_right;
  137. m_bitCounterL += 1.0f;
  138. m_bitCounterR += 1.0f;
  139. if( m_bitCounterL > m_rateCoeffL )
  140. {
  141. m_bitCounterL -= m_rateCoeffL;
  142. m_left = m_depthEnabled
  143. ? depthCrush( buf[f][0] * m_inGain + noise( buf[f][0] * noiseAmt ) )
  144. : buf[f][0] * m_inGain + noise( buf[f][0] * noiseAmt );
  145. }
  146. if( m_bitCounterR > m_rateCoeffR )
  147. {
  148. m_bitCounterR -= m_rateCoeffR;
  149. m_right = m_depthEnabled
  150. ? depthCrush( buf[f][1] * m_inGain + noise( buf[f][1] * noiseAmt ) )
  151. : buf[f][1] * m_inGain + noise( buf[f][1] * noiseAmt );
  152. }
  153. }
  154. }
  155. }
  156. else // rate crushing disabled: simply oversample with zero-order hold
  157. {
  158. for( int f = 0; f < frames; ++f )
  159. {
  160. for( int o = 0; o < OS_RATE; ++o )
  161. {
  162. m_buffer[f * OS_RATE + o][0] = m_depthEnabled
  163. ? depthCrush( buf[f][0] * m_inGain + noise( buf[f][0] * noiseAmt ) )
  164. : buf[f][0] * m_inGain + noise( buf[f][0] * noiseAmt );
  165. m_buffer[f * OS_RATE + o][1] = m_depthEnabled
  166. ? depthCrush( buf[f][1] * m_inGain + noise( buf[f][1] * noiseAmt ) )
  167. : buf[f][1] * m_inGain + noise( buf[f][1] * noiseAmt );
  168. }
  169. }
  170. }
  171. // the oversampled buffer is now written, so filter it to reduce aliasing
  172. for( int f = 0; f < frames * OS_RATE; ++f )
  173. {
  174. if( qMax( qAbs( m_buffer[f][0] ), qAbs( m_buffer[f][1] ) ) >= 1.0e-10f )
  175. {
  176. m_silenceCounter = 0;
  177. m_buffer[f][0] = m_filter.update( m_buffer[f][0], 0 );
  178. m_buffer[f][1] = m_filter.update( m_buffer[f][1], 1 );
  179. }
  180. else
  181. {
  182. if( m_silenceCounter > SILENCEFRAMES )
  183. {
  184. m_buffer[f][0] = m_buffer[f][1] = 0.0f;
  185. }
  186. else
  187. {
  188. ++m_silenceCounter;
  189. m_buffer[f][0] = m_filter.update( m_buffer[f][0], 0 );
  190. m_buffer[f][1] = m_filter.update( m_buffer[f][1], 1 );
  191. }
  192. }
  193. }
  194. // now downsample and write it back to main buffer
  195. double outSum = 0.0;
  196. const float d = dryLevel();
  197. const float w = wetLevel();
  198. for( int f = 0; f < frames; ++f )
  199. {
  200. float lsum = 0.0f;
  201. float rsum = 0.0f;
  202. for( int o = 0; o < OS_RATE; ++o )
  203. {
  204. lsum += m_buffer[f * OS_RATE + o][0] * OS_RESAMPLE[o];
  205. rsum += m_buffer[f * OS_RATE + o][1] * OS_RESAMPLE[o];
  206. }
  207. buf[f][0] = d * buf[f][0] + w * qBound( -m_outClip, lsum, m_outClip ) * m_outGain;
  208. buf[f][1] = d * buf[f][1] + w * qBound( -m_outClip, rsum, m_outClip ) * m_outGain;
  209. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  210. }
  211. checkGate( outSum / frames );
  212. return isRunning();
  213. }
  214. extern "C"
  215. {
  216. // necessary for getting instance out of shared lib
  217. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  218. {
  219. return new BitcrushEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  220. }
  221. }