2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * Copyright 2009-2011 Jörg Hermann Müller
6 * This file is part of AudaSpace.
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.
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.
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.
22 * ***** END GPL LICENSE BLOCK *****
40 #include "AUD_Space.h"
42 /// Supported output devices.
51 /// Sound information structure.
58 #ifndef AUD_CAPI_IMPLEMENTATION
59 typedef void AUD_Sound;
60 typedef void AUD_Handle;
61 typedef void AUD_Device;
62 typedef void AUD_SEntry;
63 typedef float (*AUD_volumeFunction)(void*, void*, float);
64 typedef void (*AUD_syncFunction)(void*, int, float);
68 * Initializes FFMPEG if it is enabled.
70 extern void AUD_initOnce(void);
73 * Initializes an audio device.
74 * \param device The device type that should be used.
75 * \param specs The audio specification to be used.
76 * \param buffersize The buffersize for the device.
77 * \return Whether the device has been initialized.
79 extern int AUD_init(AUD_DeviceType device, AUD_DeviceSpecs specs, int buffersize);
82 * Unitinitializes an audio device.
84 extern void AUD_exit(void);
87 * Locks the playback device.
89 extern void AUD_lock(void);
94 extern void AUD_unlock(void);
97 * Returns information about a sound.
98 * \param sound The sound to get the info about.
99 * \return The AUD_SoundInfo structure with filled in data.
101 extern AUD_SoundInfo AUD_getInfo(AUD_Sound* sound);
104 * Loads a sound file.
105 * \param filename The filename of the sound file.
106 * \return A handle of the sound file.
108 extern AUD_Sound* AUD_load(const char* filename);
111 * Loads a sound file.
112 * \param buffer The buffer which contains the sound file.
113 * \param size The size of the buffer.
114 * \return A handle of the sound file.
116 extern AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size);
120 * \param sound The sound to buffer.
121 * \return A handle of the sound buffer.
123 extern AUD_Sound* AUD_bufferSound(AUD_Sound* sound);
126 * Rechannels the sound to be mono.
127 * \param sound The sound to rechannel.
128 * \return The mono sound.
130 extern AUD_Sound* AUD_monoSound(AUD_Sound* sound);
134 * \param sound The sound to dealy.
135 * \param delay The delay in seconds.
136 * \return A handle of the delayed sound.
138 extern AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay);
142 * \param sound The sound to limit.
143 * \param start The start time in seconds.
144 * \param end The stop time in seconds.
145 * \return A handle of the limited sound.
147 extern AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end);
150 * Ping pongs a sound.
151 * \param sound The sound to ping pong.
152 * \return A handle of the ping pong sound.
154 extern AUD_Sound* AUD_pingpongSound(AUD_Sound* sound);
158 * \param sound The sound to loop.
159 * \return A handle of the looped sound.
161 extern AUD_Sound* AUD_loopSound(AUD_Sound* sound);
164 * Sets a remaining loop count of a looping sound that currently plays.
165 * \param handle The playback handle.
166 * \param loops The count of remaining loops, -1 for infinity.
167 * \return Whether the handle is valid.
169 extern int AUD_setLoop(AUD_Handle* handle, int loops);
173 * \param sound The sound to rectify.
174 * \return A handle of the rectified sound.
176 extern AUD_Sound* AUD_rectifySound(AUD_Sound* sound);
179 * Unloads a sound of any type.
180 * \param sound The handle of the sound.
182 extern void AUD_unload(AUD_Sound* sound);
185 * Plays back a sound file.
186 * \param sound The handle of the sound file.
187 * \param keep When keep is true the sound source will not be deleted but set to
188 * paused when its end has been reached.
189 * \return A handle to the played back sound.
191 extern AUD_Handle* AUD_play(AUD_Sound* sound, int keep);
194 * Pauses a played back sound.
195 * \param handle The handle to the sound.
196 * \return Whether the handle has been playing or not.
198 extern int AUD_pause(AUD_Handle* handle);
201 * Resumes a paused sound.
202 * \param handle The handle to the sound.
203 * \return Whether the handle has been paused or not.
205 extern int AUD_resume(AUD_Handle* handle);
208 * Stops a playing or paused sound.
209 * \param handle The handle to the sound.
210 * \return Whether the handle has been valid or not.
212 extern int AUD_stop(AUD_Handle* handle);
215 * Sets the end behaviour of a playing or paused sound.
216 * \param handle The handle to the sound.
217 * \param keep When keep is true the sound source will not be deleted but set to
218 * paused when its end has been reached.
219 * \return Whether the handle has been valid or not.
221 extern int AUD_setKeep(AUD_Handle* handle, int keep);
224 * Seeks a playing or paused sound.
225 * \param handle The handle to the sound.
226 * \param seekTo From where the sound file should be played back in seconds.
227 * \return Whether the handle has been valid or not.
229 extern int AUD_seek(AUD_Handle* handle, float seekTo);
232 * Retrieves the playback position of a handle.
233 * \param handle The handle to the sound.
234 * \return The current playback position in seconds or 0.0 if the handle is
237 extern float AUD_getPosition(AUD_Handle* handle);
240 * Returns the status of a playing, paused or stopped sound.
241 * \param handle The handle to the sound.
242 * \return The status of the sound behind the handle.
244 extern AUD_Status AUD_getStatus(AUD_Handle* handle);
247 * Sets the listener location.
248 * \param location The new location.
250 extern int AUD_setListenerLocation(const float* location);
253 * Sets the listener velocity.
254 * \param velocity The new velocity.
256 extern int AUD_setListenerVelocity(const float* velocity);
259 * Sets the listener orientation.
260 * \param orientation The new orientation as quaternion.
262 extern int AUD_setListenerOrientation(const float* orientation);
265 * Sets the speed of sound.
266 * This value is needed for doppler effect calculation.
267 * \param speed The new speed of sound.
269 extern int AUD_setSpeedOfSound(float speed);
272 * Sets the doppler factor.
273 * This value is a scaling factor for the velocity vectors of sources and
274 * listener which is used while calculating the doppler effect.
275 * \param factor The new doppler factor.
277 extern int AUD_setDopplerFactor(float factor);
280 * Sets the distance model.
281 * \param model distance model.
283 extern int AUD_setDistanceModel(AUD_DistanceModel model);
286 * Sets the location of a source.
287 * \param handle The handle of the source.
288 * \param location The new location.
289 * \return Whether the action succeeded.
291 extern int AUD_setSourceLocation(AUD_Handle* handle, const float* location);
294 * Sets the velocity of a source.
295 * \param handle The handle of the source.
296 * \param velocity The new velocity.
297 * \return Whether the action succeeded.
299 extern int AUD_setSourceVelocity(AUD_Handle* handle, const float* velocity);
302 * Sets the orientation of a source.
303 * \param handle The handle of the source.
304 * \param orientation The new orientation as quaternion.
305 * \return Whether the action succeeded.
307 extern int AUD_setSourceOrientation(AUD_Handle* handle, const float* orientation);
310 * Sets whether the source location, velocity and orientation are relative
312 * \param handle The handle of the source.
313 * \param relative Whether the source is relative.
314 * \return Whether the action succeeded.
316 extern int AUD_setRelative(AUD_Handle* handle, int relative);
319 * Sets the maximum volume of a source.
320 * \param handle The handle of the source.
321 * \param volume The new maximum volume.
322 * \return Whether the action succeeded.
324 extern int AUD_setVolumeMaximum(AUD_Handle* handle, float volume);
327 * Sets the minimum volume of a source.
328 * \param handle The handle of the source.
329 * \param volume The new minimum volume.
330 * \return Whether the action succeeded.
332 extern int AUD_setVolumeMinimum(AUD_Handle* handle, float volume);
335 * Sets the maximum distance of a source.
336 * If a source is further away from the reader than this distance, the
337 * volume will automatically be set to 0.
338 * \param handle The handle of the source.
339 * \param distance The new maximum distance.
340 * \return Whether the action succeeded.
342 extern int AUD_setDistanceMaximum(AUD_Handle* handle, float distance);
345 * Sets the reference distance of a source.
346 * \param handle The handle of the source.
347 * \param distance The new reference distance.
348 * \return Whether the action succeeded.
350 extern int AUD_setDistanceReference(AUD_Handle* handle, float distance);
353 * Sets the attenuation of a source.
354 * This value is used for distance calculation.
355 * \param handle The handle of the source.
356 * \param factor The new attenuation.
357 * \return Whether the action succeeded.
359 extern int AUD_setAttenuation(AUD_Handle* handle, float factor);
362 * Sets the outer angle of the cone of a source.
363 * \param handle The handle of the source.
364 * \param angle The new outer angle of the cone.
365 * \return Whether the action succeeded.
367 extern int AUD_setConeAngleOuter(AUD_Handle* handle, float angle);
370 * Sets the inner angle of the cone of a source.
371 * \param handle The handle of the source.
372 * \param angle The new inner angle of the cone.
373 * \return Whether the action succeeded.
375 extern int AUD_setConeAngleInner(AUD_Handle* handle, float angle);
378 * Sets the outer volume of the cone of a source.
379 * The volume between inner and outer angle is interpolated between inner
380 * volume and this value.
381 * \param handle The handle of the source.
382 * \param volume The new outer volume of the cone.
383 * \return Whether the action succeeded.
385 extern int AUD_setConeVolumeOuter(AUD_Handle* handle, float volume);
388 * Sets the volume of a played back sound.
389 * \param handle The handle to the sound.
390 * \param volume The new volume, must be between 0.0 and 1.0.
391 * \return Whether the action succeeded.
393 extern int AUD_setSoundVolume(AUD_Handle* handle, float volume);
396 * Sets the pitch of a played back sound.
397 * \param handle The handle to the sound.
398 * \param pitch The new pitch.
399 * \return Whether the action succeeded.
401 extern int AUD_setSoundPitch(AUD_Handle* handle, float pitch);
404 * Opens a read device, with which audio data can be read.
405 * \param specs The specification of the audio data.
406 * \return A device handle.
408 extern AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs);
411 * Sets the main volume of a device.
412 * \param device The device.
413 * \param volume The new volume, must be between 0.0 and 1.0.
414 * \return Whether the action succeeded.
416 extern int AUD_setDeviceVolume(AUD_Device* device, float volume);
419 * Plays back a sound file through a read device.
420 * \param device The read device.
421 * \param sound The handle of the sound file.
422 * \param seek The position where the sound should be seeked to.
423 * \return A handle to the played back sound.
425 extern AUD_Handle* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek);
428 * Reads the next samples into the supplied buffer.
429 * \param device The read device.
430 * \param buffer The target buffer.
431 * \param length The length in samples to be filled.
432 * \return True if the reading succeeded, false if there are no sounds
433 * played back currently, in that case the buffer is filled with
436 extern int AUD_readDevice(AUD_Device* device, data_t* buffer, int length);
439 * Closes a read device.
440 * \param device The read device.
442 extern void AUD_closeReadDevice(AUD_Device* device);
445 * Reads a sound file into a newly created float buffer.
446 * The sound is therefore bandpassed, rectified and resampled.
448 extern float* AUD_readSoundBuffer(const char* filename, float low, float high,
449 float attack, float release, float threshold,
450 int accumulate, int additive, int square,
451 float sthreshold, double samplerate,
455 * Pauses a playing sound after a specific amount of time.
456 * \param handle The handle to the sound.
457 * \param time The time in seconds.
458 * \return The silence handle.
460 extern AUD_Handle* AUD_pauseAfter(AUD_Handle* handle, float seconds);
463 * Creates a new sequenced sound scene.
464 * \param fps The FPS of the scene.
465 * \param muted Whether the scene is muted.
466 * \return The new sound scene.
468 extern AUD_Sound* AUD_createSequencer(float fps, int muted);
471 * Deletes a sound scene.
472 * \param sequencer The sound scene.
474 extern void AUD_destroySequencer(AUD_Sound* sequencer);
477 * Sets the muting state of the scene.
478 * \param sequencer The sound scene.
479 * \param muted Whether the scene is muted.
481 extern void AUD_setSequencerMuted(AUD_Sound* sequencer, int muted);
484 * Sets the scene's FPS.
485 * \param sequencer The sound scene.
486 * \param fps The new FPS.
488 extern void AUD_setSequencerFPS(AUD_Sound* sequencer, float fps);
491 * Adds a new entry to the scene.
492 * \param sequencer The sound scene.
493 * \param sound The sound this entry should play.
494 * \param begin The start time.
495 * \param end The end time or a negative value if determined by the sound.
496 * \param skip How much seconds should be skipped at the beginning.
497 * \return The entry added.
499 extern AUD_SEntry* AUD_addSequence(AUD_Sound* sequencer, AUD_Sound* sound,
500 float begin, float end, float skip);
503 * Removes an entry from the scene.
504 * \param sequencer The sound scene.
505 * \param entry The entry to remove.
507 extern void AUD_removeSequence(AUD_Sound* sequencer, AUD_SEntry* entry);
511 * \param entry The sequenced entry.
512 * \param begin The new start time.
513 * \param end The new end time or a negative value if unknown.
514 * \param skip How many seconds to skip at the beginning.
516 extern void AUD_moveSequence(AUD_SEntry* entry, float begin, float end, float skip);
519 * Sets the muting state of the entry.
520 * \param entry The sequenced entry.
521 * \param mute Whether the entry should be muted or not.
523 extern void AUD_muteSequence(AUD_SEntry* entry, char mute);
526 * Sets whether the entrie's location, velocity and orientation are relative
528 * \param entry The sequenced entry.
529 * \param relative Whether the source is relative.
530 * \return Whether the action succeeded.
532 extern void AUD_setRelativeSequence(AUD_SEntry* entry, char relative);
535 * Sets the sound of the entry.
536 * \param entry The sequenced entry.
537 * \param sound The new sound.
539 extern void AUD_updateSequenceSound(AUD_SEntry* entry, AUD_Sound* sound);
542 * Writes animation data to a sequenced entry.
543 * \param entry The sequenced entry.
544 * \param type The type of animation data.
545 * \param frame The frame this data is for.
546 * \param data The data to write.
547 * \param animated Whether the attribute is animated.
549 extern void AUD_setSequenceAnimData(AUD_SEntry* entry, AUD_AnimateablePropertyType type, int frame, float* data, char animated);
552 * Writes animation data to a sequenced entry.
553 * \param sequencer The sound scene.
554 * \param type The type of animation data.
555 * \param frame The frame this data is for.
556 * \param data The data to write.
557 * \param animated Whether the attribute is animated.
559 extern void AUD_setSequencerAnimData(AUD_Sound* sequencer, AUD_AnimateablePropertyType type, int frame, float* data, char animated);
562 * Updates all non-animated parameters of the entry.
563 * \param entry The sequenced entry.
564 * \param volume_max The maximum volume.
565 * \param volume_min The minimum volume.
566 * \param distance_max The maximum distance.
567 * \param distance_reference The reference distance.
568 * \param attenuation The attenuation.
569 * \param cone_angle_outer The outer cone opening angle.
570 * \param cone_angle_inner The inner cone opening angle.
571 * \param cone_volume_outer The volume outside the outer cone.
573 extern void AUD_updateSequenceData(AUD_SEntry* entry, float volume_max, float volume_min,
574 float distance_max, float distance_reference, float attenuation,
575 float cone_angle_outer, float cone_angle_inner, float cone_volume_outer);
578 * Updates all non-animated parameters of the entry.
579 * \param sequencer The sound scene.
580 * \param speed_of_sound The speed of sound for doppler calculation.
581 * \param factor The doppler factor to control the effect's strength.
582 * \param model The distance model for distance calculation.
584 extern void AUD_updateSequencerData(AUD_Sound* sequencer, float speed_of_sound,
585 float factor, AUD_DistanceModel model);
588 * Sets the audio output specification of the sound scene to the specs of the
589 * current playback device.
590 * \param sequencer The sound scene.
592 extern void AUD_setSequencerDeviceSpecs(AUD_Sound* sequencer);
595 * Sets the audio output specification of the sound scene.
596 * \param sequencer The sound scene.
597 * \param specs The new specification.
599 extern void AUD_setSequencerSpecs(AUD_Sound* sequencer, AUD_Specs specs);
602 * Seeks sequenced sound scene playback.
603 * \param handle Playback handle.
604 * \param time Time in seconds to seek to.
606 extern void AUD_seekSequencer(AUD_Handle* handle, float time);
609 * Returns the current sound scene playback time.
610 * \param handle Playback handle.
611 * \return The playback time in seconds.
613 extern float AUD_getSequencerPosition(AUD_Handle* handle);
616 * Starts the playback of jack transport if possible.
618 extern void AUD_startPlayback(void);
621 * Stops the playback of jack transport if possible.
623 extern void AUD_stopPlayback(void);
627 * Sets the sync callback for jack transport.
628 * \param function The callback function.
629 * \param data The data parameter for the callback.
631 extern void AUD_setSyncCallback(AUD_syncFunction function, void* data);
635 * Returns whether jack transport is currently playing.
636 * \return Whether jack transport is currently playing.
638 extern int AUD_doesPlayback(void);
641 * Reads a sound into a buffer for drawing at a specific sampling rate.
642 * \param sound The sound to read.
643 * \param buffer The buffer to write to. Must have a size of 3*4*length.
644 * \param length How many samples to read from the sound.
645 * \param samples_per_second How many samples to read per second of the sound.
646 * \return How many samples really have been read. Always <= length.
648 extern int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length, int samples_per_second);
652 * \param sound Sound to copy.
653 * \return Copied sound.
655 extern AUD_Sound* AUD_copy(AUD_Sound* sound);
659 * \param channel Handle to free.
661 extern void AUD_freeHandle(AUD_Handle* channel);
665 * \return The new set.
667 extern void* AUD_createSet(void);
671 * \param set The set to delete.
673 extern void AUD_destroySet(void* set);
676 * Removes an entry from a set.
677 * \param set The set work on.
678 * \param entry The entry to remove.
679 * \return Whether the entry was in the set or not.
681 extern char AUD_removeSet(void* set, void* entry);
684 * Adds a new entry to a set.
685 * \param set The set work on.
686 * \param entry The entry to add.
688 extern void AUD_addSet(void* set, void* entry);
691 * Removes one entry from a set and returns it.
692 * \param set The set work on.
693 * \return The entry or NULL if the set is empty.
695 extern void* AUD_getSet(void* set);
698 * Mixes a sound down into a file.
699 * \param sound The sound scene to mix down.
700 * \param start The start frame.
701 * \param length The count of frames to write.
702 * \param buffersize How many samples should be written at once.
703 * \param filename The file to write to.
704 * \param specs The file's audio specification.
705 * \param format The file's container format.
706 * \param codec The codec used for encoding the audio data.
707 * \param bitrate The bitrate for encoding.
708 * \return An error message or NULL in case of success.
710 extern const char* AUD_mixdown(AUD_Sound* sound, unsigned int start, unsigned int length, unsigned int buffersize, const char* filename, AUD_DeviceSpecs specs, AUD_Container format, AUD_Codec codec, unsigned int bitrate);
713 * Opens a read device and prepares it for mixdown of the sound scene.
714 * \param specs Output audio specifications.
715 * \param sequencer The sound scene to mix down.
716 * \param volume The overall mixdown volume.
717 * \param start The start time of the mixdown in the sound scene.
718 * \return The read device for the mixdown.
720 extern AUD_Device* AUD_openMixdownDevice(AUD_DeviceSpecs specs, AUD_Sound* sequencer, float volume, float start);
724 * Retrieves the python factory of a sound.
725 * \param sound The sound factory.
726 * \return The python factory.
728 extern PyObject* AUD_getPythonFactory(AUD_Sound* sound);
731 * Retrieves the sound factory of a python factory.
732 * \param sound The python factory.
733 * \return The sound factory.
735 extern AUD_Sound* AUD_getPythonSound(PyObject* sound);
741 #include "AUD_Reference.h"
746 * Returns the current playback device.
747 * \return The playback device.
749 AUD_Reference<AUD_IDevice> AUD_getDevice();
752 * Returns the current playback 3D device.
753 * \return The playback 3D device.
755 AUD_I3DDevice* AUD_get3DDevice();