Advertisement
Guest User

C++ BitCrusher Code Review

a guest
Jan 14th, 2013
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. // http://10rem.net/blog/2013/01/13/a-simple-bitcrusher-and-sample-rate-reducer-in-cplusplus-for-a-windows-store-app
  2.  
  3. //
  4. // *************** StereoSample ****************
  5. //
  6.  
  7. /*
  8.   Original :
  9.  
  10. struct StereoSample
  11. {
  12. public:
  13.     SAMPLE_t Left;
  14.     SAMPLE_t Right;
  15. };
  16.  
  17. */
  18.  
  19. // "struct X { ... }" is equivalent to "class X { public: ... }",
  20. // so remove the redundant "public:".
  21. // Moreover, a constructor to init the sample may come in handy.
  22. struct StereoSample
  23. {
  24.     SAMPLE_t Left;
  25.     SAMPLE_t Right;
  26.  
  27.     StereoSample()
  28.       : Left(0.0), Right(0.0)
  29.     {}
  30.  
  31.     StereoSample(SAMPLE_t left, SAMPLE_t right)
  32.       : Left(left), Right(right)
  33.     {}
  34. };
  35.  
  36.  
  37.  
  38.  
  39. //
  40. // *************** Voice::Voice() ****************
  41. //
  42.  
  43. /*
  44.  
  45. Use std::vector<StereoSample> instead of raw new[]-allocated array.
  46.  
  47.   std::vector<StereoSample> _bufferData;
  48.  
  49. It's automatically destructed, and it's exception-safe.
  50.  
  51. It seems that _oscillators is a container (vector?) of shared_ptr.
  52. Then just use push_back( make_shared<Oscillator>() ).
  53.  
  54. You don't need the cast in _audioEngine->CreateSourceVoice() "&wfx" parameter.
  55. "&wfx" is already a "WAVEFORMATEX*".
  56.  
  57. */
  58.  
  59. Voice::Voice(IXAudio2* audioEngine, int stereoBufferSize) :
  60.     _audioEngine(audioEngine),
  61.     _stereoBufferSize(stereoBufferSize),
  62.     _bufferData(stereoBufferSize)         // <--- Init std::vector
  63. {
  64.     for (int i = 0; i < MAX_OSCILLATORS; i++)
  65.     {
  66.         // *** Use make_shared<>: ***
  67.         _oscillators.push_back(make_shared<Oscillator>());
  68.     }
  69.    
  70.     // set up wave format using my good friend WAVEFORMATEX
  71.     WAVEFORMATEX wfx;
  72.     wfx.wBitsPerSample = SAMPLE_BITS;
  73.     wfx.nAvgBytesPerSec = SAMPLE_RATE * SAMPLE_CHANNELS * SAMPLE_BITS / 8;
  74.     wfx.nChannels = SAMPLE_CHANNELS;
  75.     wfx.nBlockAlign = SAMPLE_CHANNELS * SAMPLE_BITS / 8;
  76.     wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; // or could use WAVE_FORMAT_PCM
  77.     wfx.nSamplesPerSec = SAMPLE_RATE;
  78.     wfx.cbSize = 0;    // set to zero for PCM or IEEE float
  79.  
  80.     DX::ThrowIfFailed(_audioEngine->CreateSourceVoice(
  81.         &_xavoice,
  82.        
  83.         // *** No cast needed: ***
  84.         //   (WAVEFORMATEX*)&wfx,
  85.         &wfx,
  86.        
  87.         0,
  88.         XAUDIO2_DEFAULT_FREQ_RATIO,      
  89.        
  90.         // Not sure if you need the cast here, I don't know what _voiceCallbackHandler is...
  91.         reinterpret_cast<IXAudio2VoiceCallback*>(&_voiceCallbackHandler),
  92.  
  93.         nullptr,
  94.         nullptr));
  95. }
  96.  
  97.  
  98.  
  99.  
  100. //
  101. // *************** Voice::Render() ****************
  102. //
  103.  
  104.  
  105. /*
  106.     Original
  107.    
  108.     vector<shared_ptr<Oscillator>>::const_iterator cii;
  109.     for (cii = _oscillators.begin(); cii < _oscillators.end(); cii++)
  110.     {
  111.         (*cii)->Render(...);
  112.     }
  113. */
  114.  
  115. // Use C++11 auto (available in VS2010):    
  116. for (auto it = _oscillators.begin(); it != oscillators.end(); ++it)
  117.   (*it)->Render(...);
  118.  
  119.  
  120. // Or C++11 range-based for loops (available in VS2012):
  121. for (auto& o : _oscillators)
  122.     o->Render(...);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement