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, float frame)
70 {
71         if(!m_handle.isNull())
72         {
73                 if(position >= m_entry->m_end && m_entry->m_end >= 0)
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                 float value;
115
116                 m_entry->m_volume.read(frame, &value);
117                 m_handle->setVolume(value);
118                 m_entry->m_pitch.read(frame, &value);
119                 m_handle->setPitch(value);
120                 m_entry->m_panning.read(frame, &value);
121                 AUD_SoftwareDevice::setPanning(m_handle.get(), value);
122
123                 AUD_Vector3 v, v2;
124                 AUD_Quaternion q;
125
126                 m_entry->m_orientation.read(frame, q.get());
127                 m_3dhandle->setSourceOrientation(q);
128                 m_entry->m_location.read(frame, v.get());
129                 m_3dhandle->setSourceLocation(v);
130                 m_entry->m_location.read(frame + 1, v2.get());
131                 v2 -= v;
132                 m_3dhandle->setSourceVelocity(v2);
133
134                 if(m_entry->m_muted)
135                         m_handle->setVolume(0);
136         }
137 }
138
139 void AUD_SequencerHandle::seek(float position)
140 {
141         if(!m_handle.isNull())
142         {
143                 if(position >= m_entry->m_end && m_entry->m_end >= 0)
144                 {
145                         m_handle->pause();
146                         return;
147                 }
148
149                 float seekpos = position - m_entry->m_begin;
150                 if(seekpos < 0)
151                         seekpos = 0;
152                 seekpos += m_entry->m_skip;
153                 m_handle->seek(seekpos);
154                 if(position < m_entry->m_begin)
155                         m_handle->pause();
156                 else
157                         m_handle->resume();
158         }
159 }