Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    422064

Untitled

Sign in to follow this  

55 views

Ok, the sound stuff is a bit harder than I thought. I can't even get a single note playing properly. Finding the frequencies was easy though. So I played with it a little bit and came up with this. Feel free to debug my code or tell me where I'm going wrong here:

//==========================================================================//
// WaveformStream.h - Derived CSoundStream for generated waveforms
//==========================================================================//
#include "Common/mmgr.h"
#include "WaveformStream.h"
#include "Common/Log.h"
#include "Common/Assert.h"
#include

static const size_t s_nBufferLen = 4096;
static const int s_nFrequency = 22050;


CWaveformStream::CWaveformStream() :
m_dwBufferPos(s_nBufferLen),
m_fPos(0.0f)
{
Assert(s_nBufferLen*sizeof(m_pBuffer[0]) == sizeof(m_pBuffer));
}

CWaveformStream::~CWaveformStream()
{
}

int CWaveformStream::GetFrequency() const
{
return s_nFrequency;
}

int CWaveformStream::GetChannels() const
{
return 1;
}

int CWaveformStream::GetLength() const
{
return 0;
}

void CWaveformStream::Rewind()
{
}

bool CWaveformStream::FillBuffer(void* pBuffer, size_t dwLength)
{
bool bEOF;

// Copy chunks from our buffer till we run out
do {
// Are we able to just use what's in our buffer?
const size_t nLenRemain = (s_nBufferLen-m_dwBufferPos);
if(dwLength <= nLenRemain)
{
memcpy(pBuffer, m_pBuffer+m_dwBufferPos, dwLength);
m_dwBufferPos += dwLength;
m_fPos += (float)dwLength;
return true;
}

// Apparently not. Empty our buffer
memcpy(pBuffer, m_pBuffer+m_dwBufferPos, nLenRemain);
dwLength -= nLenRemain;
pBuffer = ((unsigned char*)pBuffer) + nLenRemain;
m_fPos += (float)nLenRemain;
while(m_fPos >= 44100.0f) m_fPos -= 44100.0f;

// Refill our buffer
bEOF = RefillBuffer();
if(bEOF && m_bLoop)
{
Rewind();
bEOF = false;
}
} while(!bEOF);

// End of file
return false;
}

bool CWaveformStream::RefillBuffer()
{
for(size_t i=m_dwBufferPos; i {
float fI = ((m_fPos+i)/22050.0f)*2.0f*3.1415926535897932384626433832795f;
fI *= 261.63f;
float fSample = sinf(fI);
m_pBuffer = (unsigned short)(fSample*65536.0f);
}
m_dwBufferPos = 0;
return false;
}


m_pBuffer is declared as: unsigned short m_pBuffer[4096];.

I got my memory manager written, however. And it's tracking STL allocations too, which is nice. Although it can't track the file and line numbers, it can track the number of allocated bytes. It works perfectly, and I found and fixed 2 leaks in 5 minutes or so. One was me not deleting a manager singleton, and one was a bit more subtle - my sound manager's thread wasn't getting terminated properly, so it timed out and was forcefully terminated, leaking memory from a std::vector. Easy enough to fix - I posted the terminate event but forgot to signal the thread to pick up the event. So it sat in the even queue till the manager decided the thread was fubar and killed it.
I also discovered toggling to fullscreen wasn't working properly either - it wasn't changing the window style, so you could still click the taskbar. Fixed that on the train on the way home, then found that if the app is fullscreen and you hit the windows key, then restore the app again, the device won't reset (D3D tells me there's still objects allocated). So I'll have to find out what that's about. I have a feeling that I'm not resetting the flag that tells my device to call OnLostDevice() for all render objects after I reset the device. So it resets fine the first time (windowed -> fullscreen), but then forgets to do it in future. Anyway. That can be done on the train tomorrow.

I'm not going to do any coding tonight, I was in work late and I'm only just in (and it's 8:45pm). I was supposed to be going to a friends house to watch a film tonight, but I'd be half an hour late, and I'd have to get a taxi home. And this being Edinburgh, that costs about GBP8 which I don't have.

I'm off to watch some crap TV then goto bed.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now