changed: Improve (fallback) logic for subtitle downloading
[vuplus_xbmc] / xbmc / cores / AudioEngine / Engines / CoreAudio / CoreAudioAESound.cpp
1 /*
2  *      Copyright (C) 2011-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <samplerate.h>
22
23 #include "CoreAudioAESound.h"
24
25 #include "CoreAudioAE.h"
26 #include "threads/SingleLock.h"
27 #include "cores/AudioEngine/AEFactory.h"
28 #include "cores/AudioEngine/Utils/AEAudioFormat.h"
29 #include "cores/AudioEngine/Interfaces/AESound.h"
30 #include "cores/AudioEngine/Utils/AEConvert.h"
31 #include "cores/AudioEngine/Utils/AERemap.h"
32 #include "cores/AudioEngine/Utils/AEUtil.h"
33 #include "utils/log.h"
34 #include "utils/EndianSwap.h"
35
36 /* typecast AE to CCoreAudioAE */
37 #define AE (*(CCoreAudioAE*)CAEFactory::GetEngine())
38
39 CCoreAudioAESound::CCoreAudioAESound(const std::string &filename) :
40   IAESound         (filename),
41   m_filename       (filename),
42   m_volume         (1.0f    ),
43   m_inUse          (0       )
44 {
45   m_wavLoader.Load(filename);
46 }
47
48 CCoreAudioAESound::~CCoreAudioAESound()
49 {
50   DeInitialize();
51 }
52
53
54 std::string CCoreAudioAESound::GetFileName()
55 {
56   return m_filename;
57 }
58
59 void CCoreAudioAESound::DeInitialize()
60 {
61 }
62
63 bool CCoreAudioAESound::Initialize()
64 {
65   if (!m_wavLoader.IsValid())
66     return false;
67
68   return m_wavLoader.Initialize(
69     AE.GetSampleRate   (),
70     AE.GetChannelLayout(),
71     AE_CH_LAYOUT_INVALID
72   );
73 }
74
75 void CCoreAudioAESound::SetVolume(float volume)
76 {
77   m_volume = std::max(0.0f, std::min(1.0f, volume));
78 }
79
80 float CCoreAudioAESound::GetVolume()
81 {
82   return m_volume;
83 }
84
85 unsigned int CCoreAudioAESound::GetSampleCount()
86 {
87   CSingleLock cs(m_critSection);
88   if (m_wavLoader.IsValid())
89     return m_wavLoader.GetSampleCount();
90   return 0;
91 }
92
93 float* CCoreAudioAESound::GetSamples()
94 {
95   CSingleLock cs(m_critSection);
96   if (!m_wavLoader.IsValid())
97     return NULL;
98
99   ++m_inUse;
100   return m_wavLoader.GetSamples();
101 }
102
103 void CCoreAudioAESound::ReleaseSamples()
104 {
105   CSingleLock cs(m_critSection);
106   if(m_inUse > 0)
107     --m_inUse;
108 }
109
110 bool CCoreAudioAESound::IsPlaying()
111 {
112   CSingleLock cs(m_critSection);
113   return (m_inUse > 0);
114 }
115
116 void CCoreAudioAESound::Play()
117 {
118   AE.PlaySound(this);
119 }
120
121 void CCoreAudioAESound::Stop()
122 {
123   AE.StopSound(this);
124 }