38d2ed38a7edaeb02cba9b02953cd723b39f3ed3
[blender.git] / intern / audaspace / intern / AUD_SequencerHandle.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * Copyright 2009-2011 Jörg Hermann Müller
5  *
6  * This file is part of AudaSpace.
7  *
8  * Audaspace is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * AudaSpace is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Audaspace; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file audaspace/intern/AUD_SequencerHandle.cpp
26  *  \ingroup audaspaceintern
27  */
28
29
30 #include "AUD_SequencerHandle.h"
31 #include "AUD_ReadDevice.h"
32 #include "AUD_MutexLock.h"
33
34 AUD_SequencerHandle::AUD_SequencerHandle(AUD_Reference<AUD_SequencerEntry> entry, AUD_ReadDevice& device) :
35         m_entry(entry),
36         m_status(0),
37         m_pos_status(0),
38         m_sound_status(0),
39         m_device(device)
40 {
41         if(!entry->m_sound.isNull())
42         {
43                 m_handle = device.play(entry->m_sound, true);
44                 m_3dhandle = AUD_Reference<AUD_I3DHandle>(m_handle);
45         }
46 }
47
48 AUD_SequencerHandle::~AUD_SequencerHandle()
49 {
50         stop();
51 }
52
53 int AUD_SequencerHandle::compare(AUD_Reference<AUD_SequencerEntry> entry) const
54 {
55         if(m_entry->getID() < entry->getID())
56                 return -1;
57         else if(m_entry->getID() == entry->getID())
58                 return 0;
59         return 1;
60 }
61
62 void AUD_SequencerHandle::stop()
63 {
64         if(!m_handle.isNull())
65                 m_handle->stop();
66 }
67
68 void AUD_SequencerHandle::update(float position, float frame, float fps)
69 {
70         if(!m_handle.isNull())
71         {
72                 AUD_MutexLock lock(*m_entry);
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                         m_pos_status--;
91                         m_status--;
92                 }
93
94                 if(m_pos_status != m_entry->m_pos_status)
95                 {
96                         seek(position);
97
98                         m_pos_status = m_entry->m_pos_status;
99                 }
100
101                 if(m_status != m_entry->m_status)
102                 {
103                         m_3dhandle->setRelative(m_entry->m_relative);
104                         m_3dhandle->setVolumeMaximum(m_entry->m_volume_max);
105                         m_3dhandle->setVolumeMinimum(m_entry->m_volume_min);
106                         m_3dhandle->setDistanceMaximum(m_entry->m_distance_max);
107                         m_3dhandle->setDistanceReference(m_entry->m_distance_reference);
108                         m_3dhandle->setAttenuation(m_entry->m_attenuation);
109                         m_3dhandle->setConeAngleOuter(m_entry->m_cone_angle_outer);
110                         m_3dhandle->setConeAngleInner(m_entry->m_cone_angle_inner);
111                         m_3dhandle->setConeVolumeOuter(m_entry->m_cone_volume_outer);
112
113                         m_status = m_entry->m_status;
114                 }
115
116                 float value;
117
118                 m_entry->m_volume.read(frame, &value);
119                 m_handle->setVolume(value);
120                 m_entry->m_pitch.read(frame, &value);
121                 m_handle->setPitch(value);
122                 m_entry->m_panning.read(frame, &value);
123                 AUD_SoftwareDevice::setPanning(m_handle.get(), value);
124
125                 AUD_Vector3 v, v2;
126                 AUD_Quaternion q;
127
128                 m_entry->m_orientation.read(frame, q.get());
129                 m_3dhandle->setSourceOrientation(q);
130                 m_entry->m_location.read(frame, v.get());
131                 m_3dhandle->setSourceLocation(v);
132                 m_entry->m_location.read(frame + 1, v2.get());
133                 v2 -= v;
134                 m_3dhandle->setSourceVelocity(v2 * fps);
135
136                 if(m_entry->m_muted)
137                         m_handle->setVolume(0);
138         }
139 }
140
141 void AUD_SequencerHandle::seek(float position)
142 {
143         if(!m_handle.isNull())
144         {
145                 AUD_MutexLock lock(*m_entry);
146                 if(position >= m_entry->m_end && m_entry->m_end >= 0)
147                 {
148                         m_handle->pause();
149                         return;
150                 }
151
152                 float seekpos = position - m_entry->m_begin;
153                 if(seekpos < 0)
154                         seekpos = 0;
155                 seekpos += m_entry->m_skip;
156                 m_handle->setPitch(1.0f);
157                 m_handle->seek(seekpos);
158                 if(position < m_entry->m_begin)
159                         m_handle->pause();
160                 else
161                         m_handle->resume();
162         }
163 }