Main Page · Modules · All Classes · Class Hierarchy
MASoundMemory.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MASoundMemory.hpp"
23 
24 #include <MCDefs.hpp>
25 
26 MASoundMemory::MASoundMemory(const std::string& display_name, MC::BinaryDataSPtr sound_data) :
27  MASoundBase(display_name), Data(sound_data)
28 {
29  Data->SetPosition(0);
30 }
31 
32 
34 {
35  return Data->GetSize() / sizeof(int16_t);
36 }
37 
38 
40 {
41  return true;
42 }
43 
44 
46 {
47  return true;
48 }
49 
50 
51 void MASoundMemory::AddToBuffer(int position, int count, int32_t* buffer)
52 {
53  if (count < 1)
54  return;
55 
56  const int Size = GetSize();
57  const int Position = MCBound(0, position, Size-1);
58  int Duration = (Position+count > Size ? Size-Position : count);
59 
60  // An other safety check
61  if (Duration < 0)
62  Duration = 0;
63 
64  int16_t* SrcPtr = ((int16_t*)(void*)Data->GetData())+Position;
65  int32_t* DestPtr = buffer;
66 
67  for (int i = 0; i < Duration; ++i)
68  {
69  *DestPtr += (int32_t)*SrcPtr;
70  DestPtr++;
71  SrcPtr++;
72  }
73 }
virtual int GetSize() const
Get the sound data size.
Base class for sounds.
Definition: MASoundBase.hpp:45
virtual void AddToBuffer(int position, int count, int32_t *buffer)
Add sound data to a buffer.
const T & MCBound(const T &min, const T &value, const T &max)
Check a value bound according to a range.
Definition: MCDefs.hpp:527
virtual bool IsCached() const
Check if the sound data is cached.
MASoundMemory(const std::string &display_name, MC::BinaryDataSPtr sound_data)
Class constructor.
MC::BinaryDataSPtr Data
Sound data.
virtual bool IsValid() const
Check if the sound data is valid.