WiiFlow_Lite/source/music/BufferCircle.cpp

148 lines
3.2 KiB
C++
Raw Normal View History

2012-01-21 21:57:41 +01:00
/***************************************************************************
* Copyright (C) 2010
* by Dimok
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* for WiiXplorer 2010
***************************************************************************/
#include "BufferCircle.hpp"
#include "loader/utils.h"
#include "memory/mem2.hpp"
2012-01-21 21:57:41 +01:00
BufferCircle::BufferCircle()
{
2012-06-08 21:00:02 +02:00
which = 0;
BufferBlockSize = 0;
2012-01-21 21:57:41 +01:00
}
BufferCircle::~BufferCircle()
{
2012-06-08 21:00:02 +02:00
FreeBuffer();
SoundBuffer.clear();
BufferSize.clear();
BufferReady.clear();
2012-01-21 21:57:41 +01:00
}
void BufferCircle::SetBufferBlockSize(int size)
{
2012-06-08 21:00:02 +02:00
if(size < 0)
return;
2012-01-21 21:57:41 +01:00
2012-06-08 21:00:02 +02:00
BufferBlockSize = size;
2012-01-21 21:57:41 +01:00
2012-06-08 21:00:02 +02:00
for(int i = 0; i < Size(); i++)
{
if(SoundBuffer[i] != NULL)
MEM2_free(SoundBuffer[i]);
2012-06-08 21:00:02 +02:00
SoundBuffer[i] = (u8 *)MEM2_memalign(32, BufferBlockSize);
memset(SoundBuffer[i], 0, BufferBlockSize);
2012-06-08 21:00:02 +02:00
BufferSize[i] = 0;
BufferReady[i] = false;
}
2012-01-21 21:57:41 +01:00
}
void BufferCircle::Resize(int size)
{
// this while() only gets done if new size is smaller than old size
2012-06-08 21:00:02 +02:00
while(size < Size())
RemoveBuffer(Size()-1);
int oldSize = Size();
SoundBuffer.resize(size);
BufferSize.resize(size);
BufferReady.resize(size);
// this for() only gets done if new size is bigger than old size
2012-06-08 21:00:02 +02:00
for(int i = oldSize; i < Size(); i++)
{
if(BufferBlockSize > 0)
{
SoundBuffer[i] = (u8 *)MEM2_memalign(32, BufferBlockSize);
memset(SoundBuffer[i], 0, BufferBlockSize);
}
2012-06-08 21:00:02 +02:00
else
SoundBuffer[i] = NULL;
BufferSize[i] = 0;
BufferReady[i] = false;
}
2012-01-21 21:57:41 +01:00
}
void BufferCircle::RemoveBuffer(int pos)
{
2012-06-08 21:00:02 +02:00
if(!Valid(pos))
return;
2012-01-21 21:57:41 +01:00
if(SoundBuffer[pos] != NULL)
MEM2_free(SoundBuffer[pos]);
2012-01-21 21:57:41 +01:00
2012-06-08 21:00:02 +02:00
SoundBuffer.erase(SoundBuffer.begin()+pos);
BufferSize.erase(BufferSize.begin()+pos);
BufferReady.erase(BufferReady.begin()+pos);
2012-01-21 21:57:41 +01:00
}
void BufferCircle::ClearBuffer()
{
2012-06-08 21:00:02 +02:00
for(int i = 0; i < Size(); i++)
{
BufferSize[i] = 0;
BufferReady[i] = false;
}
which = 0;
2012-01-21 21:57:41 +01:00
}
void BufferCircle::FreeBuffer()
{
2012-06-08 21:00:02 +02:00
for(int i = 0; i < Size(); i++)
{
if(SoundBuffer[i] != NULL)
MEM2_free(SoundBuffer[i]);
2012-06-08 21:00:02 +02:00
BufferSize[i] = 0;
BufferReady[i] = false;
}
2012-01-21 21:57:41 +01:00
}
void BufferCircle::LoadNext()
{
2012-06-08 21:00:02 +02:00
int pos = (which+Size()-1) % Size();
BufferReady[pos] = false;
BufferSize[pos] = 0;
2012-01-21 21:57:41 +01:00
2012-06-08 21:00:02 +02:00
which = (which+1) % Size();
2012-01-21 21:57:41 +01:00
}
void BufferCircle::SetBufferReady(int pos, bool state)
{
2012-06-08 21:00:02 +02:00
if(!Valid(pos))
return;
2012-01-21 21:57:41 +01:00
2012-06-08 21:00:02 +02:00
BufferReady[pos] = state;
2012-01-21 21:57:41 +01:00
}
void BufferCircle::SetBufferSize(int pos, int size)
{
2012-06-08 21:00:02 +02:00
if(!Valid(pos))
return;
2012-01-21 21:57:41 +01:00
2012-06-08 21:00:02 +02:00
BufferSize[pos] = size;
2012-01-21 21:57:41 +01:00
}