SoftwareDevice.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*******************************************************************************
  2. * Copyright 2009-2016 Jörg Müller
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. #pragma once
  17. /**
  18. * @file SoftwareDevice.h
  19. * @ingroup devices
  20. * The SoftwareDevice class.
  21. */
  22. #include "devices/IDevice.h"
  23. #include "devices/IHandle.h"
  24. #include "devices/I3DDevice.h"
  25. #include "devices/I3DHandle.h"
  26. #include "devices/DefaultSynchronizer.h"
  27. #include "util/Buffer.h"
  28. #include <list>
  29. #include <mutex>
  30. AUD_NAMESPACE_BEGIN
  31. class Mixer;
  32. class PitchReader;
  33. class ResampleReader;
  34. class ChannelMapperReader;
  35. /**
  36. * The software device is a generic device with software mixing.
  37. * It is a base class for all software mixing classes.
  38. * Classes implementing this have to:
  39. * - Implement the playing function.
  40. * - Prepare the m_specs, m_mixer variables.
  41. * - Call the create and destroy functions.
  42. * - Call the mix function to retrieve their audio data.
  43. */
  44. class AUD_API SoftwareDevice : public IDevice, public I3DDevice
  45. {
  46. protected:
  47. /// Saves the data for playback.
  48. class AUD_API SoftwareHandle : public IHandle, public I3DHandle
  49. {
  50. private:
  51. // delete copy constructor and operator=
  52. SoftwareHandle(const SoftwareHandle&) = delete;
  53. SoftwareHandle& operator=(const SoftwareHandle&) = delete;
  54. public:
  55. /// The reader source.
  56. std::shared_ptr<IReader> m_reader;
  57. /// The pitch reader in between.
  58. std::shared_ptr<PitchReader> m_pitch;
  59. /// The resample reader in between.
  60. std::shared_ptr<ResampleReader> m_resampler;
  61. /// The channel mapper reader in between.
  62. std::shared_ptr<ChannelMapperReader> m_mapper;
  63. /// Whether to keep the source if end of it is reached.
  64. bool m_keep;
  65. /// The user set pitch of the source.
  66. float m_user_pitch;
  67. /// The user set volume of the source.
  68. float m_user_volume;
  69. /// The user set panning for non-3D sources
  70. float m_user_pan;
  71. /// The calculated final volume of the source.
  72. float m_volume;
  73. /// The previous calculated final volume of the source.
  74. float m_old_volume;
  75. /// The loop count of the source.
  76. int m_loopcount;
  77. /// Location in 3D Space.
  78. Vector3 m_location;
  79. /// Velocity in 3D Space.
  80. Vector3 m_velocity;
  81. /// Orientation in 3D Space.
  82. Quaternion m_orientation;
  83. /// Whether the position to the listener is relative or absolute
  84. bool m_relative;
  85. /// Maximum volume.
  86. float m_volume_max;
  87. /// Minimum volume.
  88. float m_volume_min;
  89. /// Maximum distance.
  90. float m_distance_max;
  91. /// Reference distance;
  92. float m_distance_reference;
  93. /// Attenuation
  94. float m_attenuation;
  95. /// Cone outer angle.
  96. float m_cone_angle_outer;
  97. /// Cone inner angle.
  98. float m_cone_angle_inner;
  99. /// Cone outer volume.
  100. float m_cone_volume_outer;
  101. /// Rendering flags
  102. int m_flags;
  103. /// The stop callback.
  104. stopCallback m_stop;
  105. /// Stop callback data.
  106. void* m_stop_data;
  107. /// Current status of the handle
  108. Status m_status;
  109. /// Own device.
  110. SoftwareDevice* m_device;
  111. /**
  112. * This method is for internal use only.
  113. * @param keep Whether the sound should be marked stopped or paused.
  114. * @return Whether the action succeeded.
  115. */
  116. bool pause(bool keep);
  117. public:
  118. /**
  119. * Creates a new software handle.
  120. * \param device The device this handle is from.
  121. * \param reader The reader to play.
  122. * \param pitch The pitch reader.
  123. * \param resampler The resampling reader.
  124. * \param mapper The channel mapping reader.
  125. * \param keep Whether to keep the handle when the sound ends.
  126. */
  127. SoftwareHandle(SoftwareDevice* device, std::shared_ptr<IReader> reader, std::shared_ptr<PitchReader> pitch, std::shared_ptr<ResampleReader> resampler, std::shared_ptr<ChannelMapperReader> mapper, bool keep);
  128. /**
  129. * Updates the handle's playback parameters.
  130. */
  131. void update();
  132. /**
  133. * Sets the audio output specification of the readers.
  134. * \param specs The output specification.
  135. */
  136. void setSpecs(Specs specs);
  137. virtual ~SoftwareHandle() {}
  138. virtual bool pause();
  139. virtual bool resume();
  140. virtual bool stop();
  141. virtual bool getKeep();
  142. virtual bool setKeep(bool keep);
  143. virtual bool seek(float position);
  144. virtual float getPosition();
  145. virtual Status getStatus();
  146. virtual float getVolume();
  147. virtual bool setVolume(float volume);
  148. virtual float getPitch();
  149. virtual bool setPitch(float pitch);
  150. virtual int getLoopCount();
  151. virtual bool setLoopCount(int count);
  152. virtual bool setStopCallback(stopCallback callback = 0, void* data = 0);
  153. virtual Vector3 getLocation();
  154. virtual bool setLocation(const Vector3& location);
  155. virtual Vector3 getVelocity();
  156. virtual bool setVelocity(const Vector3& velocity);
  157. virtual Quaternion getOrientation();
  158. virtual bool setOrientation(const Quaternion& orientation);
  159. virtual bool isRelative();
  160. virtual bool setRelative(bool relative);
  161. virtual float getVolumeMaximum();
  162. virtual bool setVolumeMaximum(float volume);
  163. virtual float getVolumeMinimum();
  164. virtual bool setVolumeMinimum(float volume);
  165. virtual float getDistanceMaximum();
  166. virtual bool setDistanceMaximum(float distance);
  167. virtual float getDistanceReference();
  168. virtual bool setDistanceReference(float distance);
  169. virtual float getAttenuation();
  170. virtual bool setAttenuation(float factor);
  171. virtual float getConeAngleOuter();
  172. virtual bool setConeAngleOuter(float angle);
  173. virtual float getConeAngleInner();
  174. virtual bool setConeAngleInner(float angle);
  175. virtual float getConeVolumeOuter();
  176. virtual bool setConeVolumeOuter(float volume);
  177. };
  178. /**
  179. * The specification of the device.
  180. */
  181. DeviceSpecs m_specs;
  182. /**
  183. * The mixer.
  184. */
  185. std::shared_ptr<Mixer> m_mixer;
  186. /**
  187. * Whether to do high or low quality resampling.
  188. */
  189. bool m_quality;
  190. /**
  191. * Initializes member variables.
  192. */
  193. void create();
  194. /**
  195. * Uninitializes member variables.
  196. */
  197. void destroy();
  198. /**
  199. * Mixes the next samples into the buffer.
  200. * \param buffer The target buffer.
  201. * \param length The length in samples to be filled.
  202. */
  203. void mix(data_t* buffer, int length);
  204. /**
  205. * This function tells the device, to start or pause playback.
  206. * \param playing True if device should playback.
  207. */
  208. virtual void playing(bool playing)=0;
  209. /**
  210. * Sets the audio output specification of the device.
  211. * \param specs The output specification.
  212. */
  213. void setSpecs(Specs specs);
  214. /**
  215. * Empty default constructor. To setup the device call the function create()
  216. * and to uninitialize call destroy().
  217. */
  218. SoftwareDevice();
  219. private:
  220. /**
  221. * The reading buffer.
  222. */
  223. Buffer m_buffer;
  224. /**
  225. * The list of sounds that are currently playing.
  226. */
  227. std::list<std::shared_ptr<SoftwareHandle> > m_playingSounds;
  228. /**
  229. * The list of sounds that are currently paused.
  230. */
  231. std::list<std::shared_ptr<SoftwareHandle> > m_pausedSounds;
  232. /**
  233. * Whether there is currently playback.
  234. */
  235. bool m_playback;
  236. /**
  237. * The mutex for locking.
  238. */
  239. std::recursive_mutex m_mutex;
  240. /**
  241. * The overall volume of the device.
  242. */
  243. float m_volume;
  244. /// Listener location.
  245. Vector3 m_location;
  246. /// Listener velocity.
  247. Vector3 m_velocity;
  248. /// Listener orientation.
  249. Quaternion m_orientation;
  250. /// Speed of Sound.
  251. float m_speed_of_sound;
  252. /// Doppler factor.
  253. float m_doppler_factor;
  254. /// Distance model.
  255. DistanceModel m_distance_model;
  256. /// Rendering flags
  257. int m_flags;
  258. /// Synchronizer.
  259. DefaultSynchronizer m_synchronizer;
  260. // delete copy constructor and operator=
  261. SoftwareDevice(const SoftwareDevice&) = delete;
  262. SoftwareDevice& operator=(const SoftwareDevice&) = delete;
  263. public:
  264. /**
  265. * Sets the panning of a specific handle.
  266. * \param handle The handle to set the panning from.
  267. * \param pan The new panning value, should be in the range [-2, 2].
  268. */
  269. static void setPanning(IHandle* handle, float pan);
  270. /**
  271. * Sets the resampling quality.
  272. * \param quality Low (false) or high (true) quality.
  273. */
  274. void setQuality(bool quality);
  275. virtual DeviceSpecs getSpecs() const;
  276. virtual std::shared_ptr<IHandle> play(std::shared_ptr<IReader> reader, bool keep = false);
  277. virtual std::shared_ptr<IHandle> play(std::shared_ptr<ISound> sound, bool keep = false);
  278. virtual void stopAll();
  279. virtual void lock();
  280. virtual void unlock();
  281. virtual float getVolume() const;
  282. virtual void setVolume(float volume);
  283. virtual ISynchronizer* getSynchronizer();
  284. virtual Vector3 getListenerLocation() const;
  285. virtual void setListenerLocation(const Vector3& location);
  286. virtual Vector3 getListenerVelocity() const;
  287. virtual void setListenerVelocity(const Vector3& velocity);
  288. virtual Quaternion getListenerOrientation() const;
  289. virtual void setListenerOrientation(const Quaternion& orientation);
  290. virtual float getSpeedOfSound() const;
  291. virtual void setSpeedOfSound(float speed);
  292. virtual float getDopplerFactor() const;
  293. virtual void setDopplerFactor(float factor);
  294. virtual DistanceModel getDistanceModel() const;
  295. virtual void setDistanceModel(DistanceModel model);
  296. };
  297. AUD_NAMESPACE_END