Blender V2.61 - r43446

AUD_SequencerReader.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * Copyright 2009-2011 Jörg Hermann Müller
00005  *
00006  * This file is part of AudaSpace.
00007  *
00008  * Audaspace is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * AudaSpace is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Audaspace; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #include "AUD_SequencerReader.h"
00031 
00032 typedef std::list<AUD_Reference<AUD_SequencerHandle> >::iterator AUD_HandleIterator;
00033 typedef std::list<AUD_Reference<AUD_SequencerEntry> >::iterator AUD_EntryIterator;
00034 
00035 AUD_SequencerReader::AUD_SequencerReader(AUD_Reference<AUD_SequencerFactory> factory, bool quality) :
00036     m_position(0), m_device(factory->m_specs), m_factory(factory), m_status(0), m_entry_status(0)
00037 {
00038     m_device.setQuality(quality);
00039 }
00040 
00041 AUD_SequencerReader::~AUD_SequencerReader()
00042 {
00043 }
00044 
00045 bool AUD_SequencerReader::isSeekable() const
00046 {
00047     return true;
00048 }
00049 
00050 void AUD_SequencerReader::seek(int position)
00051 {
00052     m_position = position;
00053 
00054     for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
00055     {
00056         (*it)->seek(position / m_factory->m_specs.rate);
00057     }
00058 }
00059 
00060 int AUD_SequencerReader::getLength() const
00061 {
00062     return -1;
00063 }
00064 
00065 int AUD_SequencerReader::getPosition() const
00066 {
00067     return m_position;
00068 }
00069 
00070 AUD_Specs AUD_SequencerReader::getSpecs() const
00071 {
00072     return m_factory->m_specs;
00073 }
00074 
00075 void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer)
00076 {
00077     m_factory->lock();
00078 
00079     if(m_factory->m_status != m_status)
00080     {
00081         m_device.changeSpecs(m_factory->m_specs);
00082         m_device.setSpeedOfSound(m_factory->m_speed_of_sound);
00083         m_device.setDistanceModel(m_factory->m_distance_model);
00084         m_device.setDopplerFactor(m_factory->m_doppler_factor);
00085 
00086         m_status = m_factory->m_status;
00087     }
00088 
00089     if(m_factory->m_entry_status != m_entry_status)
00090     {
00091         std::list<AUD_Reference<AUD_SequencerHandle> > handles;
00092 
00093         AUD_HandleIterator hit = m_handles.begin();
00094         AUD_EntryIterator  eit = m_factory->m_entries.begin();
00095 
00096         int result;
00097         AUD_Reference<AUD_SequencerHandle> handle;
00098 
00099         while(hit != m_handles.end() && eit != m_factory->m_entries.end())
00100         {
00101             handle = *hit;
00102             AUD_Reference<AUD_SequencerEntry> entry = *eit;
00103 
00104             result = handle->compare(entry);
00105 
00106             if(result < 0)
00107             {
00108                 try
00109                 {
00110                     handle = new AUD_SequencerHandle(entry, m_device);
00111                     handles.push_front(handle);
00112                 }
00113                 catch(AUD_Exception&)
00114                 {
00115                 }
00116                 eit++;
00117             }
00118             else if(result == 0)
00119             {
00120                 handles.push_back(handle);
00121                 hit++;
00122                 eit++;
00123             }
00124             else
00125             {
00126                 handle->stop();
00127                 hit++;
00128             }
00129         }
00130 
00131         while(hit != m_handles.end())
00132         {
00133             (*hit)->stop();
00134             hit++;
00135         }
00136 
00137         while(eit != m_factory->m_entries.end())
00138         {
00139             try
00140             {
00141                 handle = new AUD_SequencerHandle(*eit, m_device);
00142                 handles.push_front(handle);
00143             }
00144             catch(AUD_Exception&)
00145             {
00146             }
00147             eit++;
00148         }
00149 
00150         m_handles = handles;
00151 
00152         m_entry_status = m_factory->m_entry_status;
00153     }
00154 
00155     AUD_Specs specs = m_factory->m_specs;
00156     int pos = 0;
00157     float time = float(m_position) / float(specs.rate);
00158     float volume, frame;
00159     int len, cfra;
00160     AUD_Vector3 v, v2;
00161     AUD_Quaternion q;
00162 
00163 
00164     while(pos < length)
00165     {
00166         frame = time * m_factory->m_fps;
00167         cfra = int(floor(frame));
00168 
00169         len = int(ceil((cfra + 1) / m_factory->m_fps * specs.rate)) - m_position;
00170         len = AUD_MIN(length - pos, len);
00171         len = AUD_MAX(len, 1);
00172 
00173         for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
00174         {
00175             (*it)->update(time, frame, m_factory->m_fps);
00176         }
00177 
00178         m_factory->m_volume.read(frame, &volume);
00179         m_device.setVolume(volume);
00180 
00181         m_factory->m_orientation.read(frame, q.get());
00182         m_device.setListenerOrientation(q);
00183         m_factory->m_location.read(frame, v.get());
00184         m_device.setListenerLocation(v);
00185         m_factory->m_location.read(frame + 1, v2.get());
00186         v2 -= v;
00187         m_device.setListenerVelocity(v2 * m_factory->m_fps);
00188 
00189         m_device.read(reinterpret_cast<data_t*>(buffer + specs.channels * pos), len);
00190 
00191         pos += len;
00192         time += float(len) / float(specs.rate);
00193     }
00194 
00195     m_factory->unlock();
00196 
00197     m_position += length;
00198 
00199     eos = false;
00200 }