3D Audio GSoC:
[blender.git] / intern / audaspace / intern / AUD_SequencerHandle.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * Copyright 2009-2011 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * Audaspace is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Audaspace; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file audaspace/intern/AUD_SequencerHandle.cpp
28  *  \ingroup audaspaceintern
29  */
30
31
32 #include "AUD_SequencerHandle.h"
33 #include "AUD_ReadDevice.h"
34
35 AUD_SequencerHandle::AUD_SequencerHandle(AUD_Reference<AUD_SequencerEntry> entry, AUD_ReadDevice& device) :
36         m_entry(entry),
37         m_status(0),
38         m_pos_status(0),
39         m_sound_status(0),
40         m_device(device)
41 {
42         if(!entry->m_sound.isNull())
43         {
44                 m_handle = device.play(entry->m_sound, true);
45                 m_3dhandle = AUD_Reference<AUD_I3DHandle>(m_handle);
46         }
47 }
48
49 AUD_SequencerHandle::~AUD_SequencerHandle()
50 {
51         stop();
52 }
53
54 int AUD_SequencerHandle::compare(AUD_Reference<AUD_SequencerEntry> entry) const
55 {
56         if(m_entry->getID() < entry->getID())
57                 return -1;
58         else if(m_entry->getID() == entry->getID())
59                 return 0;
60         return 1;
61 }
62
63 void AUD_SequencerHandle::stop()
64 {
65         if(!m_handle.isNull())
66                 m_handle->stop();
67 }
68
69 void AUD_SequencerHandle::update(float position)
70 {
71         if(!m_handle.isNull())
72         {
73                 if(position >= m_entry->m_end)
74                         m_handle->pause();
75                 else if(position >= m_entry->m_begin)
76                         m_handle->resume();
77
78                 if(m_sound_status != m_entry->m_sound_status)
79                 {
80                         if(!m_handle.isNull())
81                                 m_handle->stop();
82
83                         if(!m_entry->m_sound.isNull())
84                         {
85                                 m_handle = m_device.play(m_entry->m_sound, true);
86                                 m_3dhandle = AUD_Reference<AUD_I3DHandle>(m_handle);
87                         }
88
89                         m_sound_status = m_entry->m_sound_status;
90                 }
91
92                 if(m_pos_status != m_entry->m_pos_status)
93                 {
94                         seek(position);
95
96                         m_pos_status = m_entry->m_pos_status;
97                 }
98
99                 if(m_status != m_entry->m_status)
100                 {
101                         m_3dhandle->setRelative(m_entry->m_relative);
102                         m_3dhandle->setVolumeMaximum(m_entry->m_volume_max);
103                         m_3dhandle->setVolumeMinimum(m_entry->m_volume_min);
104                         m_3dhandle->setDistanceMaximum(m_entry->m_distance_max);
105                         m_3dhandle->setDistanceReference(m_entry->m_distance_reference);
106                         m_3dhandle->setAttenuation(m_entry->m_attenuation);
107                         m_3dhandle->setConeAngleOuter(m_entry->m_cone_angle_outer);
108                         m_3dhandle->setConeAngleInner(m_entry->m_cone_angle_inner);
109                         m_3dhandle->setConeVolumeOuter(m_entry->m_cone_volume_outer);
110
111                         m_status = m_entry->m_status;
112                 }
113
114                 // AUD_XXX TODO: Animation data
115
116                 if(m_entry->m_muted)
117                         m_handle->setVolume(0);
118         }
119 }
120
121 void AUD_SequencerHandle::seek(float position)
122 {
123         if(!m_handle.isNull())
124         {
125                 if(position >= m_entry->m_end)
126                 {
127                         m_handle->pause();
128                         return;
129                 }
130
131                 float seekpos = position - m_entry->m_begin;
132                 if(seekpos < 0)
133                         seekpos = 0;
134                 seekpos += m_entry->m_skip;
135                 m_handle->seek(seekpos);
136                 if(position < m_entry->m_begin)
137                         m_handle->pause();
138                 else
139                         m_handle->resume();
140         }
141 }