Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://10rem.net/blog/2013/01/13/a-simple-bitcrusher-and-sample-rate-reducer-in-cplusplus-for-a-windows-store-app
- //
- // *************** StereoSample ****************
- //
- /*
- Original :
- struct StereoSample
- {
- public:
- SAMPLE_t Left;
- SAMPLE_t Right;
- };
- */
- // "struct X { ... }" is equivalent to "class X { public: ... }",
- // so remove the redundant "public:".
- // Moreover, a constructor to init the sample may come in handy.
- struct StereoSample
- {
- SAMPLE_t Left;
- SAMPLE_t Right;
- StereoSample()
- : Left(0.0), Right(0.0)
- {}
- StereoSample(SAMPLE_t left, SAMPLE_t right)
- : Left(left), Right(right)
- {}
- };
- //
- // *************** Voice::Voice() ****************
- //
- /*
- Use std::vector<StereoSample> instead of raw new[]-allocated array.
- std::vector<StereoSample> _bufferData;
- It's automatically destructed, and it's exception-safe.
- It seems that _oscillators is a container (vector?) of shared_ptr.
- Then just use push_back( make_shared<Oscillator>() ).
- You don't need the cast in _audioEngine->CreateSourceVoice() "&wfx" parameter.
- "&wfx" is already a "WAVEFORMATEX*".
- */
- Voice::Voice(IXAudio2* audioEngine, int stereoBufferSize) :
- _audioEngine(audioEngine),
- _stereoBufferSize(stereoBufferSize),
- _bufferData(stereoBufferSize) // <--- Init std::vector
- {
- for (int i = 0; i < MAX_OSCILLATORS; i++)
- {
- // *** Use make_shared<>: ***
- _oscillators.push_back(make_shared<Oscillator>());
- }
- // set up wave format using my good friend WAVEFORMATEX
- WAVEFORMATEX wfx;
- wfx.wBitsPerSample = SAMPLE_BITS;
- wfx.nAvgBytesPerSec = SAMPLE_RATE * SAMPLE_CHANNELS * SAMPLE_BITS / 8;
- wfx.nChannels = SAMPLE_CHANNELS;
- wfx.nBlockAlign = SAMPLE_CHANNELS * SAMPLE_BITS / 8;
- wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; // or could use WAVE_FORMAT_PCM
- wfx.nSamplesPerSec = SAMPLE_RATE;
- wfx.cbSize = 0; // set to zero for PCM or IEEE float
- DX::ThrowIfFailed(_audioEngine->CreateSourceVoice(
- &_xavoice,
- // *** No cast needed: ***
- // (WAVEFORMATEX*)&wfx,
- &wfx,
- 0,
- XAUDIO2_DEFAULT_FREQ_RATIO,
- // Not sure if you need the cast here, I don't know what _voiceCallbackHandler is...
- reinterpret_cast<IXAudio2VoiceCallback*>(&_voiceCallbackHandler),
- nullptr,
- nullptr));
- }
- //
- // *************** Voice::Render() ****************
- //
- /*
- Original
- vector<shared_ptr<Oscillator>>::const_iterator cii;
- for (cii = _oscillators.begin(); cii < _oscillators.end(); cii++)
- {
- (*cii)->Render(...);
- }
- */
- // Use C++11 auto (available in VS2010):
- for (auto it = _oscillators.begin(); it != oscillators.end(); ++it)
- (*it)->Render(...);
- // Or C++11 range-based for loops (available in VS2012):
- for (auto& o : _oscillators)
- o->Render(...);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement