diff --git a/intern/audaspace/CMakeLists.txt b/intern/audaspace/CMakeLists.txt new file mode 100644 index 00000000000..b1ded10e539 --- /dev/null +++ b/intern/audaspace/CMakeLists.txt @@ -0,0 +1,72 @@ +# $Id$ +# ***** BEGIN LGPL LICENSE BLOCK ***** +# +# Copyright 2009 Jörg Hermann Müller +# +# This file is part of AudaSpace. +# +# AudaSpace is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# AudaSpace is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with AudaSpace. If not, see . +# +# ***** END LGPL LICENSE BLOCK ***** + +SET(INC . intern FX SRC ${PTHREADS_INC} ${LIBSAMPLERATE_INC}) + +FILE(GLOB SRC intern/*.cpp intern/*.h FX/*.cpp SRC/*.cpp) + +IF(WITH_FFMPEG) + SET(INC ${INC} ffmpeg ${FFMPEG_INC}) + FILE(GLOB FFMPEGSRC ffmpeg/*.cpp) + ADD_DEFINITIONS(-DWITH_FFMPEG) +ENDIF(WITH_FFMPEG) + +IF(WITH_SDL) + SET(INC ${INC} SDL ${SDL_INCLUDE_DIR}) + FILE(GLOB SDLSRC SDL/*.cpp) + ADD_DEFINITIONS(-DWITH_SDL) +ENDIF(WITH_SDL) + +IF(WITH_OPENAL) + SET(INC ${INC} OpenAL ${OPENAL_INCLUDE_DIR}) + FILE(GLOB OPENALSRC OpenAL/*.cpp) + ADD_DEFINITIONS(-DWITH_OPENAL) +ENDIF(WITH_OPENAL) + +IF(WITH_JACK) + SET(INC ${INC} jack ${JACK_INC}) + FILE(GLOB JACKSRC jack/*.cpp) + ADD_DEFINITIONS(-DWITH_JACK) +ENDIF(WITH_JACK) + +IF(WITH_SNDFILE) + SET(INC ${INC} sndfile ${SNDFILE_INC}) + FILE(GLOB SNDFILESRC sndfile/*.cpp) + ADD_DEFINITIONS(-DWITH_SNDFILE) +ENDIF(WITH_SNDFILE) + +#IF(WITH_FFTW3) +# SET(INC ${INC} fftw ${FFTW3_INC}) +# FILE(GLOB FFTW3SRC fftw/*.cpp) +# ADD_DEFINITIONS(-DWITH_FFTW3) +#ENDIF(WITH_FFTW3) + +IF(WITH_PYTHON) + SET(INC ${INC} Python ${PYTHON_INC}) + FILE(GLOB PYTHONSRC Python/*.cpp) +ELSE(WITH_PYTHON) + ADD_DEFINITIONS(-DDISABLE_PYTHON) +ENDIF(WITH_PYTHON) + +SET(SRC ${SRC} ${FFMPEGSRC} ${SNDFILESRC} ${FFTW3SRC} ${SDLSRC} ${OPENALSRC} ${JACKSRC} ${PYTHONSRC}) + +BLENDERLIB(bf_intern_audaspace "${SRC}" "${INC}") diff --git a/intern/audaspace/COPYING b/intern/audaspace/COPYING new file mode 100644 index 00000000000..94a9ed024d3 --- /dev/null +++ b/intern/audaspace/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/intern/audaspace/COPYING.LESSER b/intern/audaspace/COPYING.LESSER new file mode 100644 index 00000000000..cca7fc278f5 --- /dev/null +++ b/intern/audaspace/COPYING.LESSER @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/intern/audaspace/FX/AUD_AccumulatorFactory.cpp b/intern/audaspace/FX/AUD_AccumulatorFactory.cpp new file mode 100644 index 00000000000..0c51e5241e4 --- /dev/null +++ b/intern/audaspace/FX/AUD_AccumulatorFactory.cpp @@ -0,0 +1,60 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_AccumulatorFactory.h" +#include "AUD_CallbackIIRFilterReader.h" + +sample_t accumulatorFilterAdditive(AUD_CallbackIIRFilterReader* reader, void* useless) +{ + float in = reader->x(0); + float lastin = reader->x(-1); + float out = reader->y(-1) + in - lastin; + if(in > lastin) + out += in - lastin; + return out; +} + +sample_t accumulatorFilter(AUD_CallbackIIRFilterReader* reader, void* useless) +{ + float in = reader->x(0); + float lastin = reader->x(-1); + float out = reader->y(-1); + if(in > lastin) + out += in - lastin; + return out; +} + +AUD_AccumulatorFactory::AUD_AccumulatorFactory(AUD_IFactory* factory, + bool additive) : + AUD_EffectFactory(factory), + m_additive(additive) +{ +} + +AUD_IReader* AUD_AccumulatorFactory::createReader() const +{ + return new AUD_CallbackIIRFilterReader(getReader(), 2, 2, + m_additive ? accumulatorFilterAdditive : accumulatorFilter); +} diff --git a/intern/audaspace/FX/AUD_AccumulatorFactory.h b/intern/audaspace/FX/AUD_AccumulatorFactory.h new file mode 100644 index 00000000000..2b90fa43bdf --- /dev/null +++ b/intern/audaspace/FX/AUD_AccumulatorFactory.h @@ -0,0 +1,57 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_ACCUMULATORFACTORY +#define AUD_ACCUMULATORFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates an accumulator reader. + */ +class AUD_AccumulatorFactory : public AUD_EffectFactory +{ +private: + /** + * Whether the accumulator is additive. + */ + const bool m_additive; + + // hide copy constructor and operator= + AUD_AccumulatorFactory(const AUD_AccumulatorFactory&); + AUD_AccumulatorFactory& operator=(const AUD_AccumulatorFactory&); + +public: + /** + * Creates a new accumulator factory. + * \param factory The input factory. + * \param additive Whether the accumulator is additive. + */ + AUD_AccumulatorFactory(AUD_IFactory* factory, bool additive = false); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_ACCUMULATORFACTORY diff --git a/intern/audaspace/FX/AUD_BaseIIRFilterReader.cpp b/intern/audaspace/FX/AUD_BaseIIRFilterReader.cpp new file mode 100644 index 00000000000..9e14bcf0e40 --- /dev/null +++ b/intern/audaspace/FX/AUD_BaseIIRFilterReader.cpp @@ -0,0 +1,76 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_BaseIIRFilterReader.h" + +#include + +#define CC m_channels + m_channel + +AUD_BaseIIRFilterReader::AUD_BaseIIRFilterReader(AUD_IReader* reader, int in, + int out) : + AUD_EffectReader(reader), + m_channels(reader->getSpecs().channels), + m_xlen(in), m_ylen(out), + m_xpos(0), m_ypos(0), m_channel(0) +{ + m_x = new sample_t[in * m_channels]; + m_y = new sample_t[out * m_channels]; + + memset(m_x, 0, sizeof(sample_t) * in * m_channels); + memset(m_y, 0, sizeof(sample_t) * out * m_channels); +} + +AUD_BaseIIRFilterReader::~AUD_BaseIIRFilterReader() +{ + delete[] m_x; + delete[] m_y; +} + +void AUD_BaseIIRFilterReader::read(int & length, sample_t* & buffer) +{ + sample_t* buf; + + int samplesize = AUD_SAMPLE_SIZE(m_reader->getSpecs()); + + m_reader->read(length, buf); + + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + + buffer = m_buffer.getBuffer(); + + for(m_channel = 0; m_channel < m_channels; m_channel++) + { + for(int i = 0; i < length; i++) + { + m_x[m_xpos * CC] = buf[i * CC]; + m_y[m_ypos * CC] = buffer[i * CC] = filter(); + + m_xpos = (m_xpos + 1) % m_xlen; + m_ypos = (m_ypos + 1) % m_ylen; + } + } +} diff --git a/intern/audaspace/FX/AUD_BaseIIRFilterReader.h b/intern/audaspace/FX/AUD_BaseIIRFilterReader.h new file mode 100644 index 00000000000..7e2b71983a0 --- /dev/null +++ b/intern/audaspace/FX/AUD_BaseIIRFilterReader.h @@ -0,0 +1,114 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_BASEIIRFILTERREADER +#define AUD_BASEIIRFILTERREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This class is a base class for infinite impulse response filters. + */ +class AUD_BaseIIRFilterReader : public AUD_EffectReader +{ +private: + /** + * Channel count. + */ + const int m_channels; + + /** + * Length of input samples needed. + */ + const int m_xlen; + + /** + * Length of output samples needed. + */ + const int m_ylen; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * The last in samples array. + */ + sample_t* m_x; + + /** + * The last out samples array. + */ + sample_t* m_y; + + /** + * Position of the current input sample in the input array. + */ + int m_xpos; + + /** + * Position of the current output sample in the output array. + */ + int m_ypos; + + /** + * Current channel. + */ + int m_channel; + + // hide copy constructor and operator= + AUD_BaseIIRFilterReader(const AUD_BaseIIRFilterReader&); + AUD_BaseIIRFilterReader& operator=(const AUD_BaseIIRFilterReader&); + +protected: + /** + * Creates a new base IIR filter reader. + * \param reader The reader to read from. + * \param in The count of past input samples needed. + * \param out The count of past output samples needed. + */ + AUD_BaseIIRFilterReader(AUD_IReader* reader, int in, int out); + +public: + inline sample_t x(int pos) + { + return m_x[(m_xpos + pos + m_xlen) % m_xlen * m_channels + m_channel]; + } + + inline sample_t y(int pos) + { + return m_y[(m_ypos + pos + m_ylen) % m_ylen * m_channels + m_channel]; + } + + virtual ~AUD_BaseIIRFilterReader(); + + virtual void read(int & length, sample_t* & buffer); + + virtual sample_t filter()=0; +}; + +#endif //AUD_BASEIIRFILTERREADER diff --git a/intern/audaspace/FX/AUD_ButterworthFactory.cpp b/intern/audaspace/FX/AUD_ButterworthFactory.cpp new file mode 100644 index 00000000000..874ff0f6351 --- /dev/null +++ b/intern/audaspace/FX/AUD_ButterworthFactory.cpp @@ -0,0 +1,72 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ButterworthFactory.h" +#include "AUD_IIRFilterReader.h" + +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +#define BWPB41 0.76536686473 +#define BWPB42 1.84775906502 + +AUD_ButterworthFactory::AUD_ButterworthFactory(AUD_IFactory* factory, + float frequency) : + AUD_EffectFactory(factory), + m_frequency(frequency) +{ +} + +AUD_IReader* AUD_ButterworthFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + // calculate coefficients + float omega = 2 * tan(m_frequency * M_PI / reader->getSpecs().rate); + float o2 = omega * omega; + float o4 = o2 * o2; + float x1 = o2 + 2 * BWPB41 * omega + 4; + float x2 = o2 + 2 * BWPB42 * omega + 4; + float y1 = o2 - 2 * BWPB41 * omega + 4; + float y2 = o2 - 2 * BWPB42 * omega + 4; + float o228 = 2 * o2 - 8; + float norm = x1 * x2; + std::vector a, b; + a.push_back(1); + a.push_back((x1 + x2) * o228 / norm); + a.push_back((x1 * y2 + x2 * y1 + o228 * o228) / norm); + a.push_back((y1 + y2) * o228 / norm); + a.push_back(y1 * y2 / norm); + b.push_back(o4 / norm); + b.push_back(4 * o4 / norm); + b.push_back(6 * o4 / norm); + b.push_back(b[1]); + b.push_back(b[0]); + + return new AUD_IIRFilterReader(reader, b, a); +} diff --git a/intern/audaspace/FX/AUD_ButterworthFactory.h b/intern/audaspace/FX/AUD_ButterworthFactory.h new file mode 100644 index 00000000000..30b7a402c57 --- /dev/null +++ b/intern/audaspace/FX/AUD_ButterworthFactory.h @@ -0,0 +1,57 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_BUTTERWORTHFACTORY +#define AUD_BUTTERWORTHFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates a butterworth filter reader. + */ +class AUD_ButterworthFactory : public AUD_EffectFactory +{ +private: + /** + * The attack value in seconds. + */ + const float m_frequency; + + // hide copy constructor and operator= + AUD_ButterworthFactory(const AUD_ButterworthFactory&); + AUD_ButterworthFactory& operator=(const AUD_ButterworthFactory&); + +public: + /** + * Creates a new butterworth factory. + * \param factory The input factory. + * \param frequency The cutoff frequency. + */ + AUD_ButterworthFactory(AUD_IFactory* factory, float frequency); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_BUTTERWORTHFACTORY diff --git a/intern/audaspace/FX/AUD_CallbackIIRFilterReader.cpp b/intern/audaspace/FX/AUD_CallbackIIRFilterReader.cpp new file mode 100644 index 00000000000..02ab6e185fe --- /dev/null +++ b/intern/audaspace/FX/AUD_CallbackIIRFilterReader.cpp @@ -0,0 +1,47 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_CallbackIIRFilterReader.h" + +AUD_CallbackIIRFilterReader::AUD_CallbackIIRFilterReader(AUD_IReader* reader, + int in, int out, + doFilterIIR doFilter, + endFilterIIR endFilter, + void* data) : + AUD_BaseIIRFilterReader(reader, in, out), + m_filter(doFilter), m_endFilter(endFilter), m_data(data) +{ +} + +AUD_CallbackIIRFilterReader::~AUD_CallbackIIRFilterReader() +{ + if(m_endFilter) + m_endFilter(m_data); +} + +sample_t AUD_CallbackIIRFilterReader::filter() +{ + return m_filter(this, m_data); +} diff --git a/intern/audaspace/FX/AUD_CallbackIIRFilterReader.h b/intern/audaspace/FX/AUD_CallbackIIRFilterReader.h new file mode 100644 index 00000000000..6472c7baad3 --- /dev/null +++ b/intern/audaspace/FX/AUD_CallbackIIRFilterReader.h @@ -0,0 +1,83 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CALLBACKIIRFILTERREADER +#define AUD_CALLBACKIIRFILTERREADER + +#include "AUD_BaseIIRFilterReader.h" +#include "AUD_Buffer.h" + +class AUD_CallbackIIRFilterReader; + +typedef sample_t (*doFilterIIR)(AUD_CallbackIIRFilterReader*, void*); +typedef void (*endFilterIIR)(void*); + +/** + * This class provides an interface for infinite impulse response filters via a + * callback filter function. + */ +class AUD_CallbackIIRFilterReader : public AUD_BaseIIRFilterReader +{ +private: + /** + * Filter function. + */ + const doFilterIIR m_filter; + + /** + * End filter function. + */ + const endFilterIIR m_endFilter; + + /** + * Data pointer. + */ + void* m_data; + + // hide copy constructor and operator= + AUD_CallbackIIRFilterReader(const AUD_CallbackIIRFilterReader&); + AUD_CallbackIIRFilterReader& operator=(const AUD_CallbackIIRFilterReader&); + +public: + /** + * Creates a new callback IIR filter reader. + * \param reader The reader to read from. + * \param in The count of past input samples needed. + * \param out The count of past output samples needed. + * \param doFilter The filter callback. + * \param endFilter The finishing callback. + * \param data Data pointer for the callbacks. + */ + AUD_CallbackIIRFilterReader(AUD_IReader* reader, int in, int out, + doFilterIIR doFilter, + endFilterIIR endFilter = 0, + void* data = 0); + + virtual ~AUD_CallbackIIRFilterReader(); + + virtual sample_t filter(); +}; + +#endif //AUD_CALLBACKIIRFILTERREADER diff --git a/intern/audaspace/FX/AUD_DelayFactory.cpp b/intern/audaspace/FX/AUD_DelayFactory.cpp new file mode 100644 index 00000000000..f98743d6fb7 --- /dev/null +++ b/intern/audaspace/FX/AUD_DelayFactory.cpp @@ -0,0 +1,44 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_DelayFactory.h" +#include "AUD_DelayReader.h" +#include "AUD_Space.h" + +AUD_DelayFactory::AUD_DelayFactory(AUD_IFactory* factory, float delay) : + AUD_EffectFactory(factory), + m_delay(delay) +{ +} + +float AUD_DelayFactory::getDelay() const +{ + return m_delay; +} + +AUD_IReader* AUD_DelayFactory::createReader() const +{ + return new AUD_DelayReader(getReader(), m_delay); +} diff --git a/intern/audaspace/FX/AUD_DelayFactory.h b/intern/audaspace/FX/AUD_DelayFactory.h new file mode 100644 index 00000000000..721262fb73f --- /dev/null +++ b/intern/audaspace/FX/AUD_DelayFactory.h @@ -0,0 +1,62 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_DELAYFACTORY +#define AUD_DELAYFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory plays another factory delayed. + */ +class AUD_DelayFactory : public AUD_EffectFactory +{ +private: + /** + * The delay in samples. + */ + const float m_delay; + + // hide copy constructor and operator= + AUD_DelayFactory(const AUD_DelayFactory&); + AUD_DelayFactory& operator=(const AUD_DelayFactory&); + +public: + /** + * Creates a new delay factory. + * \param factory The input factory. + * \param delay The desired delay in seconds. + */ + AUD_DelayFactory(AUD_IFactory* factory, float delay = 0); + + /** + * Returns the delay in seconds. + */ + float getDelay() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_DELAYFACTORY diff --git a/intern/audaspace/FX/AUD_DelayReader.cpp b/intern/audaspace/FX/AUD_DelayReader.cpp new file mode 100644 index 00000000000..e9f0c15b9b4 --- /dev/null +++ b/intern/audaspace/FX/AUD_DelayReader.cpp @@ -0,0 +1,112 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_DelayReader.h" + +#include + +AUD_DelayReader::AUD_DelayReader(AUD_IReader* reader, float delay) : + AUD_EffectReader(reader), + m_delay(int(delay * reader->getSpecs().rate)), + m_remdelay(int(delay * reader->getSpecs().rate)), + m_empty(true) +{ +} + +void AUD_DelayReader::seek(int position) +{ + if(position < m_delay) + { + m_remdelay = m_delay - position; + m_reader->seek(0); + } + else + { + m_remdelay = 0; + m_reader->seek(position - m_delay); + } +} + +int AUD_DelayReader::getLength() const +{ + int len = m_reader->getLength(); + if(len < 0) + return len; + return len + m_delay; +} + +int AUD_DelayReader::getPosition() const +{ + if(m_remdelay > 0) + return m_delay - m_remdelay; + return m_reader->getPosition() + m_delay; +} + +void AUD_DelayReader::read(int & length, sample_t* & buffer) +{ + if(m_remdelay > 0) + { + AUD_Specs specs = m_reader->getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + + if(m_buffer.getSize() < length * samplesize) + { + m_buffer.resize(length * samplesize); + m_empty = false; + } + + buffer = m_buffer.getBuffer(); + + if(length > m_remdelay) + { + if(!m_empty) + memset(buffer, 0, m_remdelay * samplesize); + + int len = length - m_remdelay; + sample_t* buf; + m_reader->read(len, buf); + + memcpy(buffer + m_remdelay * specs.channels, + buf, len * samplesize); + + if(len < length-m_remdelay) + length = m_remdelay + len; + + m_remdelay = 0; + m_empty = false; + } + else + { + if(!m_empty) + { + memset(buffer, 0, length * samplesize); + m_empty = true; + } + m_remdelay -= length; + } + } + else + m_reader->read(length, buffer); +} diff --git a/intern/audaspace/FX/AUD_DelayReader.h b/intern/audaspace/FX/AUD_DelayReader.h new file mode 100644 index 00000000000..121842b0c6b --- /dev/null +++ b/intern/audaspace/FX/AUD_DelayReader.h @@ -0,0 +1,76 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_DELAYREADER +#define AUD_DELAYREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This class reads another reader and changes it's delay. + */ +class AUD_DelayReader : public AUD_EffectReader +{ +private: + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * The delay level. + */ + const int m_delay; + + /** + * The remaining delay for playback. + */ + int m_remdelay; + + /** + * Whether the buffer is currently filled with zeros. + */ + bool m_empty; + + // hide copy constructor and operator= + AUD_DelayReader(const AUD_DelayReader&); + AUD_DelayReader& operator=(const AUD_DelayReader&); + +public: + /** + * Creates a new delay reader. + * \param reader The reader to read from. + * \param delay The delay in seconds. + */ + AUD_DelayReader(AUD_IReader* reader, float delay); + + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_DELAYREADER diff --git a/intern/audaspace/FX/AUD_DoubleFactory.cpp b/intern/audaspace/FX/AUD_DoubleFactory.cpp new file mode 100644 index 00000000000..9f625d0763f --- /dev/null +++ b/intern/audaspace/FX/AUD_DoubleFactory.cpp @@ -0,0 +1,50 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_DoubleFactory.h" +#include "AUD_DoubleReader.h" + +AUD_DoubleFactory::AUD_DoubleFactory(AUD_IFactory* factory1, AUD_IFactory* factory2) : + m_factory1(factory1), m_factory2(factory2) +{ +} + +AUD_IReader* AUD_DoubleFactory::createReader() const +{ + AUD_IReader* reader1 = m_factory1->createReader(); + AUD_IReader* reader2; + + try + { + reader2 = m_factory2->createReader(); + } + catch(AUD_Exception&) + { + delete reader1; + throw; + } + + return new AUD_DoubleReader(reader1, reader2); +} diff --git a/intern/audaspace/FX/AUD_DoubleFactory.h b/intern/audaspace/FX/AUD_DoubleFactory.h new file mode 100644 index 00000000000..f2e83b2e27a --- /dev/null +++ b/intern/audaspace/FX/AUD_DoubleFactory.h @@ -0,0 +1,63 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_DOUBLEFACTORY +#define AUD_DOUBLEFACTORY + +#include "AUD_IFactory.h" + +/** + * This factory plays two other factories behind each other. + * \note Readers from the underlying factories must have the same sample rate and channel count. + */ +class AUD_DoubleFactory : public AUD_IFactory +{ +private: + /** + * First played factory. + */ + AUD_IFactory* m_factory1; + + /** + * Second played factory. + */ + AUD_IFactory* m_factory2; + + // hide copy constructor and operator= + AUD_DoubleFactory(const AUD_DoubleFactory&); + AUD_DoubleFactory& operator=(const AUD_DoubleFactory&); + +public: + /** + * Creates a new double factory. + * \param factory1 The first input factory. + * \param factory2 The second input factory. + */ + AUD_DoubleFactory(AUD_IFactory* factory1, AUD_IFactory* factory2); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_DOUBLEFACTORY diff --git a/intern/audaspace/FX/AUD_DoubleReader.cpp b/intern/audaspace/FX/AUD_DoubleReader.cpp new file mode 100644 index 00000000000..5c6ca6a1fc5 --- /dev/null +++ b/intern/audaspace/FX/AUD_DoubleReader.cpp @@ -0,0 +1,126 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_DoubleReader.h" + +#include + +static const char* specs_error = "AUD_DoubleReader: Both readers have to have " + "the same specs."; + +AUD_DoubleReader::AUD_DoubleReader(AUD_IReader* reader1, + AUD_IReader* reader2) : + m_reader1(reader1), m_reader2(reader2), m_finished1(false) +{ + AUD_Specs s1, s2; + s1 = reader1->getSpecs(); + s2 = reader2->getSpecs(); + if(memcmp(&s1, &s2, sizeof(AUD_Specs)) != 0) + { + delete reader1; + delete reader2; + AUD_THROW(AUD_ERROR_SPECS, specs_error); + } +} + +AUD_DoubleReader::~AUD_DoubleReader() +{ + delete m_reader1; + delete m_reader2; +} + +bool AUD_DoubleReader::isSeekable() const +{ + return m_reader1->isSeekable() && m_reader2->isSeekable(); +} + +void AUD_DoubleReader::seek(int position) +{ + m_reader1->seek(position); + + int pos1 = m_reader1->getPosition(); + + if((m_finished1 = (pos1 < position))) + m_reader2->seek(position - pos1); + else + m_reader2->seek(0); +} + +int AUD_DoubleReader::getLength() const +{ + int len1 = m_reader1->getLength(); + int len2 = m_reader2->getLength(); + if(len1 < 0 || len2 < 0) + return -1; + return len1 + len2; +} + +int AUD_DoubleReader::getPosition() const +{ + return m_reader1->getPosition() + m_reader2->getPosition(); +} + +AUD_Specs AUD_DoubleReader::getSpecs() const +{ + return m_reader1->getSpecs(); +} + +void AUD_DoubleReader::read(int & length, sample_t* & buffer) +{ + if(!m_finished1) + { + int len = length; + m_reader1->read(len, buffer); + + if(len < length) + { + AUD_Specs specs = m_reader1->getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + + sample_t* buf = buffer; + buffer = m_buffer.getBuffer(); + + memcpy(buffer, buf, len * samplesize); + + len = length - len; + length -= len; + m_reader2->read(len, buf); + + memcpy(buffer + length * specs.channels, buf, + len * samplesize); + + length += len; + + m_finished1 = true; + } + } + else + { + m_reader2->read(length, buffer); + } +} diff --git a/intern/audaspace/FX/AUD_DoubleReader.h b/intern/audaspace/FX/AUD_DoubleReader.h new file mode 100644 index 00000000000..d80ba33dfe3 --- /dev/null +++ b/intern/audaspace/FX/AUD_DoubleReader.h @@ -0,0 +1,84 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_DOUBLEREADER +#define AUD_DOUBLEREADER + +#include "AUD_IReader.h" +#include "AUD_Buffer.h" + +/** + * This reader plays two readers with the same specs sequently. + */ +class AUD_DoubleReader : public AUD_IReader +{ +private: + /** + * The first reader. + */ + AUD_IReader* m_reader1; + + /** + * The second reader. + */ + AUD_IReader* m_reader2; + + /** + * Whether we've reached the end of the first reader. + */ + bool m_finished1; + + /** + * The playback buffer for the intersecting part. + */ + AUD_Buffer m_buffer; + + // hide copy constructor and operator= + AUD_DoubleReader(const AUD_DoubleReader&); + AUD_DoubleReader& operator=(const AUD_DoubleReader&); + +public: + /** + * Creates a new ping pong reader. + * \param reader1 The first reader to read from. + * \param reader2 The second reader to read from. + * \exception AUD_Exception Thrown if the specs from the readers differ. + */ + AUD_DoubleReader(AUD_IReader* reader1, AUD_IReader* reader2); + + /** + * Destroys the reader. + */ + virtual ~AUD_DoubleReader(); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_DOUBLEREADER diff --git a/intern/audaspace/FX/AUD_EffectFactory.cpp b/intern/audaspace/FX/AUD_EffectFactory.cpp new file mode 100644 index 00000000000..eda4e4e04b2 --- /dev/null +++ b/intern/audaspace/FX/AUD_EffectFactory.cpp @@ -0,0 +1,41 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_EffectFactory.h" +#include "AUD_IReader.h" + +AUD_EffectFactory::AUD_EffectFactory(AUD_IFactory* factory) +{ + m_factory = factory; +} + +AUD_EffectFactory::~AUD_EffectFactory() +{ +} + +AUD_IFactory* AUD_EffectFactory::getFactory() const +{ + return m_factory; +} diff --git a/intern/audaspace/FX/AUD_EffectFactory.h b/intern/audaspace/FX/AUD_EffectFactory.h new file mode 100644 index 00000000000..fd3746d0da3 --- /dev/null +++ b/intern/audaspace/FX/AUD_EffectFactory.h @@ -0,0 +1,78 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_EFFECTFACTORY +#define AUD_EFFECTFACTORY + +#include "AUD_IFactory.h" + +/** + * This factory is a base class for all effect factories that take one other + * factory as input. + */ +class AUD_EffectFactory : public AUD_IFactory +{ +private: + // hide copy constructor and operator= + AUD_EffectFactory(const AUD_EffectFactory&); + AUD_EffectFactory& operator=(const AUD_EffectFactory&); + +protected: + /** + * If there is no reader it is created out of this factory. + */ + AUD_IFactory* m_factory; + + /** + * Returns the reader created out of the factory. + * This method can be used for the createReader function of the implementing + * classes. + * \return The reader created out of the factory. + */ + inline AUD_IReader* getReader() const + { + return m_factory->createReader(); + } + +public: + /** + * Creates a new factory. + * \param factory The input factory. + */ + AUD_EffectFactory(AUD_IFactory* factory); + + /** + * Destroys the factory. + */ + virtual ~AUD_EffectFactory(); + + /** + * Returns the saved factory. + * \return The factory or NULL if there has no factory been saved. + */ + AUD_IFactory* getFactory() const; +}; + +#endif //AUD_EFFECTFACTORY diff --git a/intern/audaspace/FX/AUD_EffectReader.cpp b/intern/audaspace/FX/AUD_EffectReader.cpp new file mode 100644 index 00000000000..b54ca279088 --- /dev/null +++ b/intern/audaspace/FX/AUD_EffectReader.cpp @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_EffectReader.h" + +AUD_EffectReader::AUD_EffectReader(AUD_IReader* reader) +{ + m_reader = reader; +} + +AUD_EffectReader::~AUD_EffectReader() +{ + delete m_reader; +} + +bool AUD_EffectReader::isSeekable() const +{ + return m_reader->isSeekable(); +} + +void AUD_EffectReader::seek(int position) +{ + m_reader->seek(position); +} + +int AUD_EffectReader::getLength() const +{ + return m_reader->getLength(); +} + +int AUD_EffectReader::getPosition() const +{ + return m_reader->getPosition(); +} + +AUD_Specs AUD_EffectReader::getSpecs() const +{ + return m_reader->getSpecs(); +} + +void AUD_EffectReader::read(int & length, sample_t* & buffer) +{ + m_reader->read(length, buffer); +} diff --git a/intern/audaspace/FX/AUD_EffectReader.h b/intern/audaspace/FX/AUD_EffectReader.h new file mode 100644 index 00000000000..c447f79bc6e --- /dev/null +++ b/intern/audaspace/FX/AUD_EffectReader.h @@ -0,0 +1,68 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_EFFECTREADER +#define AUD_EFFECTREADER + +#include "AUD_IReader.h" + +/** + * This reader is a base class for all effect readers that take one other reader + * as input. + */ +class AUD_EffectReader : public AUD_IReader +{ +private: + // hide copy constructor and operator= + AUD_EffectReader(const AUD_EffectReader&); + AUD_EffectReader& operator=(const AUD_EffectReader&); + +protected: + /** + * The reader to read from. + */ + AUD_IReader* m_reader; + +public: + /** + * Creates a new effect reader. + * \param reader The reader to read from. + */ + AUD_EffectReader(AUD_IReader* reader); + + /** + * Destroys the reader. + */ + virtual ~AUD_EffectReader(); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_EFFECTREADER diff --git a/intern/audaspace/FX/AUD_EnvelopeFactory.cpp b/intern/audaspace/FX/AUD_EnvelopeFactory.cpp new file mode 100644 index 00000000000..4777da70404 --- /dev/null +++ b/intern/audaspace/FX/AUD_EnvelopeFactory.cpp @@ -0,0 +1,78 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_EnvelopeFactory.h" +#include "AUD_CallbackIIRFilterReader.h" + +#include + +struct EnvelopeParameters +{ + float attack; + float release; + float threshold; + float arthreshold; +}; + +sample_t envelopeFilter(AUD_CallbackIIRFilterReader* reader, EnvelopeParameters* param) +{ + float in = fabs(reader->x(0)); + float out = reader->y(-1); + if(in < param->threshold) + in = 0.0f; + return (in > out ? param->attack : param->release) * (out - in) + in; +} + +void endEnvelopeFilter(EnvelopeParameters* param) +{ + delete param; +} + +AUD_EnvelopeFactory::AUD_EnvelopeFactory(AUD_IFactory* factory, float attack, + float release, float threshold, + float arthreshold) : + AUD_EffectFactory(factory), + m_attack(attack), + m_release(release), + m_threshold(threshold), + m_arthreshold(arthreshold) +{ +} + +AUD_IReader* AUD_EnvelopeFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + EnvelopeParameters* param = new EnvelopeParameters(); + param->arthreshold = m_arthreshold; + param->attack = pow(m_arthreshold, 1.0f/(reader->getSpecs().rate * m_attack)); + param->release = pow(m_arthreshold, 1.0f/(reader->getSpecs().rate * m_release)); + param->threshold = m_threshold; + + return new AUD_CallbackIIRFilterReader(reader, 1, 2, + (doFilterIIR) envelopeFilter, + (endFilterIIR) endEnvelopeFilter, + param); +} diff --git a/intern/audaspace/FX/AUD_EnvelopeFactory.h b/intern/audaspace/FX/AUD_EnvelopeFactory.h new file mode 100644 index 00000000000..c31c6727d03 --- /dev/null +++ b/intern/audaspace/FX/AUD_EnvelopeFactory.h @@ -0,0 +1,76 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_ENVELOPEFACTORY +#define AUD_ENVELOPEFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates an envelope follower reader. + */ +class AUD_EnvelopeFactory : public AUD_EffectFactory +{ +private: + /** + * The attack value in seconds. + */ + const float m_attack; + + /** + * The release value in seconds. + */ + const float m_release; + + /** + * The threshold value. + */ + const float m_threshold; + + /** + * The attack/release threshold value. + */ + const float m_arthreshold; + + // hide copy constructor and operator= + AUD_EnvelopeFactory(const AUD_EnvelopeFactory&); + AUD_EnvelopeFactory& operator=(const AUD_EnvelopeFactory&); + +public: + /** + * Creates a new envelope factory. + * \param factory The input factory. + * \param attack The attack value in seconds. + * \param release The release value in seconds. + * \param threshold The threshold value. + * \param arthreshold The attack/release threshold value. + */ + AUD_EnvelopeFactory(AUD_IFactory* factory, float attack, float release, + float threshold, float arthreshold); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_ENVELOPEFACTORY diff --git a/intern/audaspace/FX/AUD_FaderFactory.cpp b/intern/audaspace/FX/AUD_FaderFactory.cpp new file mode 100644 index 00000000000..bbe9319c928 --- /dev/null +++ b/intern/audaspace/FX/AUD_FaderFactory.cpp @@ -0,0 +1,56 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_FaderFactory.h" +#include "AUD_FaderReader.h" + +AUD_FaderFactory::AUD_FaderFactory(AUD_IFactory* factory, AUD_FadeType type, + float start, float length) : + AUD_EffectFactory(factory), + m_type(type), + m_start(start), + m_length(length) +{ +} + +AUD_FadeType AUD_FaderFactory::getType() const +{ + return m_type; +} + +float AUD_FaderFactory::getStart() const +{ + return m_start; +} + +float AUD_FaderFactory::getLength() const +{ + return m_length; +} + +AUD_IReader* AUD_FaderFactory::createReader() const +{ + return new AUD_FaderReader(getReader(), m_type, m_start, m_length); +} diff --git a/intern/audaspace/FX/AUD_FaderFactory.h b/intern/audaspace/FX/AUD_FaderFactory.h new file mode 100644 index 00000000000..af5d18538ea --- /dev/null +++ b/intern/audaspace/FX/AUD_FaderFactory.h @@ -0,0 +1,88 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_FADERFACTORY +#define AUD_FADERFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory fades another factory. + * If the fading type is AUD_FADE_IN, everything before the fading start will be + * silenced, for AUD_FADE_OUT that's true for everything after fading ends. + */ +class AUD_FaderFactory : public AUD_EffectFactory +{ +private: + /** + * The fading type. + */ + const AUD_FadeType m_type; + + /** + * The fading start. + */ + const float m_start; + + /** + * The fading length. + */ + const float m_length; + + // hide copy constructor and operator= + AUD_FaderFactory(const AUD_FaderFactory&); + AUD_FaderFactory& operator=(const AUD_FaderFactory&); + +public: + /** + * Creates a new fader factory. + * \param factory The input factory. + * \param type The fading type. + * \param start The time where fading should start in seconds. + * \param length How long fading should last in seconds. + */ + AUD_FaderFactory(AUD_IFactory* factory, + AUD_FadeType type = AUD_FADE_IN, + float start = 0.0f, float length = 1.0f); + + /** + * Returns the fading type. + */ + AUD_FadeType getType() const; + + /** + * Returns the fading start. + */ + float getStart() const; + + /** + * Returns the fading length. + */ + float getLength() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_FADERFACTORY diff --git a/intern/audaspace/FX/AUD_FaderReader.cpp b/intern/audaspace/FX/AUD_FaderReader.cpp new file mode 100644 index 00000000000..2292fa06102 --- /dev/null +++ b/intern/audaspace/FX/AUD_FaderReader.cpp @@ -0,0 +1,114 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_FaderReader.h" + +#include + +AUD_FaderReader::AUD_FaderReader(AUD_IReader* reader, AUD_FadeType type, + float start,float length) : + AUD_EffectReader(reader), + m_type(type), + m_start(start), + m_length(length), + m_empty(true) +{ +} + +void AUD_FaderReader::read(int & length, sample_t* & buffer) +{ + int position = m_reader->getPosition(); + AUD_Specs specs = m_reader->getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + + m_reader->read(length, buffer); + + if((position + length) / (float)specs.rate <= m_start) + { + if(m_type != AUD_FADE_OUT) + { + if(m_buffer.getSize() < length * samplesize) + { + m_buffer.resize(length * samplesize); + m_empty = false; + } + + buffer = m_buffer.getBuffer(); + + if(!m_empty) + { + memset(buffer, 0, length * samplesize); + m_empty = true; + } + } + } + else if(position / (float)specs.rate >= m_start+m_length) + { + if(m_type == AUD_FADE_OUT) + { + if(m_buffer.getSize() < length * samplesize) + { + m_buffer.resize(length * samplesize); + m_empty = false; + } + + buffer = m_buffer.getBuffer(); + + if(!m_empty) + { + memset(buffer, 0, length * samplesize); + m_empty = true; + } + } + } + else + { + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + + sample_t* buf = m_buffer.getBuffer(); + float volume = 1.0f; + + for(int i = 0; i < length * specs.channels; i++) + { + if(i % specs.channels == 0) + { + volume = (((position+i)/(float)specs.rate)-m_start) / m_length; + if(volume > 1.0f) + volume = 1.0f; + else if(volume < 0.0f) + volume = 0.0f; + + if(m_type == AUD_FADE_OUT) + volume = 1.0f - volume; + } + + buf[i] = buffer[i] * volume; + } + + buffer = buf; + m_empty = false; + } +} diff --git a/intern/audaspace/FX/AUD_FaderReader.h b/intern/audaspace/FX/AUD_FaderReader.h new file mode 100644 index 00000000000..d9d685af956 --- /dev/null +++ b/intern/audaspace/FX/AUD_FaderReader.h @@ -0,0 +1,82 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_FADERREADER +#define AUD_FADERREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This class fades another reader. + * If the fading type is AUD_FADE_IN, everything before the fading start will be + * silenced, for AUD_FADE_OUT that's true for everything after fading ends. + */ +class AUD_FaderReader : public AUD_EffectReader +{ +private: + /** + * The fading type. + */ + const AUD_FadeType m_type; + + /** + * The fading start. + */ + const float m_start; + + /** + * The fading length. + */ + const float m_length; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * Whether the buffer is empty. + */ + bool m_empty; + + // hide copy constructor and operator= + AUD_FaderReader(const AUD_FaderReader&); + AUD_FaderReader& operator=(const AUD_FaderReader&); + +public: + /** + * Creates a new fader reader. + * \param type The fading type. + * \param start The time where fading should start in seconds. + * \param length How long fading should last in seconds. + */ + AUD_FaderReader(AUD_IReader* reader, AUD_FadeType type, + float start,float length); + + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_FADERREADER diff --git a/intern/audaspace/FX/AUD_HighpassFactory.cpp b/intern/audaspace/FX/AUD_HighpassFactory.cpp new file mode 100644 index 00000000000..d222e7f615e --- /dev/null +++ b/intern/audaspace/FX/AUD_HighpassFactory.cpp @@ -0,0 +1,61 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_HighpassFactory.h" +#include "AUD_IIRFilterReader.h" + +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +AUD_HighpassFactory::AUD_HighpassFactory(AUD_IFactory* factory, float frequency, + float Q) : + AUD_EffectFactory(factory), + m_frequency(frequency), + m_Q(Q) +{ +} + +AUD_IReader* AUD_HighpassFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + // calculate coefficients + float w0 = 2 * M_PI * m_frequency / reader->getSpecs().rate; + float alpha = sin(w0) / (2 * m_Q); + float norm = 1 + alpha; + float c = cos(w0); + std::vector a, b; + a.push_back(1); + a.push_back(-2 * c / norm); + a.push_back((1 - alpha) / norm); + b.push_back((1 + c) / (2 * norm)); + b.push_back((-1 - c) / norm); + b.push_back(b[0]); + + return new AUD_IIRFilterReader(reader, b, a); +} diff --git a/intern/audaspace/FX/AUD_HighpassFactory.h b/intern/audaspace/FX/AUD_HighpassFactory.h new file mode 100644 index 00000000000..1220157a776 --- /dev/null +++ b/intern/audaspace/FX/AUD_HighpassFactory.h @@ -0,0 +1,63 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_HIGHPASSFACTORY +#define AUD_HIGHPASSFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates a highpass filter reader. + */ +class AUD_HighpassFactory : public AUD_EffectFactory +{ +private: + /** + * The attack value in seconds. + */ + const float m_frequency; + + /** + * The Q factor. + */ + const float m_Q; + + // hide copy constructor and operator= + AUD_HighpassFactory(const AUD_HighpassFactory&); + AUD_HighpassFactory& operator=(const AUD_HighpassFactory&); + +public: + /** + * Creates a new highpass factory. + * \param factory The input factory. + * \param frequency The cutoff frequency. + * \param Q The Q factor. + */ + AUD_HighpassFactory(AUD_IFactory* factory, float frequency, float Q = 1.0f); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_HIGHPASSFACTORY diff --git a/intern/audaspace/FX/AUD_IIRFilterFactory.cpp b/intern/audaspace/FX/AUD_IIRFilterFactory.cpp new file mode 100644 index 00000000000..8cd49a03708 --- /dev/null +++ b/intern/audaspace/FX/AUD_IIRFilterFactory.cpp @@ -0,0 +1,39 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_IIRFilterFactory.h" +#include "AUD_IIRFilterReader.h" + +AUD_IIRFilterFactory::AUD_IIRFilterFactory(AUD_IFactory* factory, + std::vector b, + std::vector a) : + AUD_EffectFactory(factory), m_a(a), m_b(b) +{ +} + +AUD_IReader* AUD_IIRFilterFactory::createReader() const +{ + return new AUD_IIRFilterReader(getReader(), m_b, m_a); +} diff --git a/intern/audaspace/FX/AUD_IIRFilterFactory.h b/intern/audaspace/FX/AUD_IIRFilterFactory.h new file mode 100644 index 00000000000..567d4f354fe --- /dev/null +++ b/intern/audaspace/FX/AUD_IIRFilterFactory.h @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_IIRFILTERFACTORY +#define AUD_IIRFILTERFACTORY + +#include "AUD_EffectFactory.h" + +#include + +/** + * This factory creates a IIR filter reader. + */ +class AUD_IIRFilterFactory : public AUD_EffectFactory +{ +private: + /** + * Output filter coefficients. + */ + std::vector m_a; + + /** + * Input filter coefficients. + */ + std::vector m_b; + + // hide copy constructor and operator= + AUD_IIRFilterFactory(const AUD_IIRFilterFactory&); + AUD_IIRFilterFactory& operator=(const AUD_IIRFilterFactory&); + +public: + /** + * Creates a new IIR filter factory. + * \param factory The input factory. + * \param b The input filter coefficients. + * \param a The output filter coefficients. + */ + AUD_IIRFilterFactory(AUD_IFactory* factory, std::vector b, + std::vector a); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_IIRFILTERFACTORY diff --git a/intern/audaspace/FX/AUD_IIRFilterReader.cpp b/intern/audaspace/FX/AUD_IIRFilterReader.cpp new file mode 100644 index 00000000000..120c9f8d0ae --- /dev/null +++ b/intern/audaspace/FX/AUD_IIRFilterReader.cpp @@ -0,0 +1,50 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_IIRFilterReader.h" + +AUD_IIRFilterReader::AUD_IIRFilterReader(AUD_IReader* reader, + std::vector b, + std::vector a) : + AUD_BaseIIRFilterReader(reader, b.size(), a.size()), m_a(a), m_b(b) +{ + for(int i = 1; i < m_a.size(); i++) + m_a[i] /= m_a[0]; + for(int i = 0; i < m_b.size(); i++) + m_b[i] /= m_a[0]; + m_a[0] = 1; +} + +sample_t AUD_IIRFilterReader::filter() +{ + sample_t out = 0; + + for(int i = 1; i < m_a.size(); i++) + out -= y(-i) * m_a[i]; + for(int i = 0; i < m_b.size(); i++) + out += x(-i) * m_b[i]; + + return out; +} diff --git a/intern/audaspace/FX/AUD_IIRFilterReader.h b/intern/audaspace/FX/AUD_IIRFilterReader.h new file mode 100644 index 00000000000..303bc6d92df --- /dev/null +++ b/intern/audaspace/FX/AUD_IIRFilterReader.h @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_IIRFILTERREADER +#define AUD_IIRFILTERREADER + +#include "AUD_BaseIIRFilterReader.h" + +#include + +/** + * This class is for infinite impulse response filters with simple coefficients. + */ +class AUD_IIRFilterReader : public AUD_BaseIIRFilterReader +{ +private: + /** + * Output filter coefficients. + */ + std::vector m_a; + + /** + * Input filter coefficients. + */ + std::vector m_b; + + // hide copy constructor and operator= + AUD_IIRFilterReader(const AUD_IIRFilterReader&); + AUD_IIRFilterReader& operator=(const AUD_IIRFilterReader&); + +public: + /** + * Creates a new IIR filter reader. + * \param reader The reader to read from. + * \param b The input filter coefficients. + * \param a The output filter coefficients. + */ + AUD_IIRFilterReader(AUD_IReader* reader, std::vector b, + std::vector a); + + virtual sample_t filter(); +}; + +#endif //AUD_IIRFILTERREADER diff --git a/intern/audaspace/FX/AUD_LimiterFactory.cpp b/intern/audaspace/FX/AUD_LimiterFactory.cpp new file mode 100644 index 00000000000..75501afcec3 --- /dev/null +++ b/intern/audaspace/FX/AUD_LimiterFactory.cpp @@ -0,0 +1,51 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LimiterFactory.h" +#include "AUD_LimiterReader.h" +#include "AUD_Space.h" + +AUD_LimiterFactory::AUD_LimiterFactory(AUD_IFactory* factory, + float start, float end) : + AUD_EffectFactory(factory), + m_start(start), + m_end(end) +{ +} + +float AUD_LimiterFactory::getStart() const +{ + return m_start; +} + +float AUD_LimiterFactory::getEnd() const +{ + return m_end; +} + +AUD_IReader* AUD_LimiterFactory::createReader() const +{ + return new AUD_LimiterReader(getReader(), m_start, m_end); +} diff --git a/intern/audaspace/FX/AUD_LimiterFactory.h b/intern/audaspace/FX/AUD_LimiterFactory.h new file mode 100644 index 00000000000..5d9491f60aa --- /dev/null +++ b/intern/audaspace/FX/AUD_LimiterFactory.h @@ -0,0 +1,75 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LIMITERFACTORY +#define AUD_LIMITERFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory limits another factory in start and end time. + */ +class AUD_LimiterFactory : public AUD_EffectFactory +{ +private: + /** + * The start time. + */ + const float m_start; + + /** + * The end time. + */ + const float m_end; + + // hide copy constructor and operator= + AUD_LimiterFactory(const AUD_LimiterFactory&); + AUD_LimiterFactory& operator=(const AUD_LimiterFactory&); + +public: + /** + * Creates a new limiter factory. + * \param factory The input factory. + * \param start The desired start time. + * \param end The desired end time, a negative value signals that it should + * play to the end. + */ + AUD_LimiterFactory(AUD_IFactory* factory, + float start = 0, float end = -1); + + /** + * Returns the start time. + */ + float getStart() const; + + /** + * Returns the end time. + */ + float getEnd() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_LIMITERFACTORY diff --git a/intern/audaspace/FX/AUD_LimiterReader.cpp b/intern/audaspace/FX/AUD_LimiterReader.cpp new file mode 100644 index 00000000000..dd7301213c9 --- /dev/null +++ b/intern/audaspace/FX/AUD_LimiterReader.cpp @@ -0,0 +1,91 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LimiterReader.h" +#include "AUD_Buffer.h" + +#include + +AUD_LimiterReader::AUD_LimiterReader(AUD_IReader* reader, + float start, float end) : + AUD_EffectReader(reader), + m_start(int(start * reader->getSpecs().rate)), + m_end(int(end * reader->getSpecs().rate)) +{ + if(m_start > 0) + { + if(m_reader->isSeekable()) + m_reader->seek(m_start); + else + { + // skip first m_start samples by reading them + int length = AUD_DEFAULT_BUFFER_SIZE; + sample_t* buffer; + for(int len = m_start; + length == AUD_DEFAULT_BUFFER_SIZE; + len -= AUD_DEFAULT_BUFFER_SIZE) + { + if(len < AUD_DEFAULT_BUFFER_SIZE) + length = len; + m_reader->read(length, buffer); + } + } + } +} + +void AUD_LimiterReader::seek(int position) +{ + m_reader->seek(position + m_start); +} + +int AUD_LimiterReader::getLength() const +{ + int len = m_reader->getLength(); + if(len < 0 || (len > m_end && m_end >= 0)) + len = m_end; + return len - m_start; +} + +int AUD_LimiterReader::getPosition() const +{ + int pos = m_reader->getPosition(); + return AUD_MIN(pos, m_end) - m_start; +} + +void AUD_LimiterReader::read(int & length, sample_t* & buffer) +{ + if(m_end >= 0) + { + int position = m_reader->getPosition(); + if(position + length > m_end) + length = m_end - position; + if(length < 0) + { + length = 0; + return; + } + } + m_reader->read(length, buffer); +} diff --git a/intern/audaspace/FX/AUD_LimiterReader.h b/intern/audaspace/FX/AUD_LimiterReader.h new file mode 100644 index 00000000000..59d6096dcba --- /dev/null +++ b/intern/audaspace/FX/AUD_LimiterReader.h @@ -0,0 +1,67 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LIMITERREADER +#define AUD_LIMITERREADER + +#include "AUD_EffectReader.h" + +/** + * This reader limits another reader in start and end sample. + */ +class AUD_LimiterReader : public AUD_EffectReader +{ +private: + /** + * The start sample: inclusive. + */ + const int m_start; + + /** + * The end sample: exlusive. + */ + const int m_end; + + // hide copy constructor and operator= + AUD_LimiterReader(const AUD_LimiterReader&); + AUD_LimiterReader& operator=(const AUD_LimiterReader&); + +public: + /** + * Creates a new limiter reader. + * \param reader The reader to read from. + * \param start The desired start sample (inclusive). + * \param end The desired end sample (exklusive), a negative value signals + * that it should play to the end. + */ + AUD_LimiterReader(AUD_IReader* reader, float start = 0, float end = -1); + + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_LIMITERREADER diff --git a/intern/audaspace/FX/AUD_LoopFactory.cpp b/intern/audaspace/FX/AUD_LoopFactory.cpp new file mode 100644 index 00000000000..6805a8e4b42 --- /dev/null +++ b/intern/audaspace/FX/AUD_LoopFactory.cpp @@ -0,0 +1,43 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LoopFactory.h" +#include "AUD_LoopReader.h" + +AUD_LoopFactory::AUD_LoopFactory(AUD_IFactory* factory, int loop) : + AUD_EffectFactory(factory), + m_loop(loop) +{ +} + +int AUD_LoopFactory::getLoop() const +{ + return m_loop; +} + +AUD_IReader* AUD_LoopFactory::createReader() const +{ + return new AUD_LoopReader(getReader(), m_loop); +} diff --git a/intern/audaspace/FX/AUD_LoopFactory.h b/intern/audaspace/FX/AUD_LoopFactory.h new file mode 100644 index 00000000000..f9e358af52d --- /dev/null +++ b/intern/audaspace/FX/AUD_LoopFactory.h @@ -0,0 +1,64 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LOOPFACTORY +#define AUD_LOOPFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory loops another factory. + * \note The reader has to be seekable. + */ +class AUD_LoopFactory : public AUD_EffectFactory +{ +private: + /** + * The loop count. + */ + const int m_loop; + + // hide copy constructor and operator= + AUD_LoopFactory(const AUD_LoopFactory&); + AUD_LoopFactory& operator=(const AUD_LoopFactory&); + +public: + /** + * Creates a new loop factory. + * \param factory The input factory. + * \param loop The desired loop count, negative values result in endless + * looping. + */ + AUD_LoopFactory(AUD_IFactory* factory, int loop = -1); + + /** + * Returns the loop count. + */ + int getLoop() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_LOOPFACTORY diff --git a/intern/audaspace/FX/AUD_LoopReader.cpp b/intern/audaspace/FX/AUD_LoopReader.cpp new file mode 100644 index 00000000000..7521f914a5c --- /dev/null +++ b/intern/audaspace/FX/AUD_LoopReader.cpp @@ -0,0 +1,111 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LoopReader.h" +#include "AUD_Buffer.h" + +#include + +AUD_LoopReader::AUD_LoopReader(AUD_IReader* reader, int loop) : + AUD_EffectReader(reader), m_count(loop), m_left(loop) +{ +} + +void AUD_LoopReader::seek(int position) +{ + int len = m_reader->getLength(); + if(len < 0) + m_reader->seek(position); + else + { + if(m_count >= 0) + { + m_left = m_count - (position / len); + if(m_left < 0) + m_left = 0; + } + m_reader->seek(position % len); + } +} + +int AUD_LoopReader::getLength() const +{ + if(m_count < 0) + return -1; + return m_reader->getLength() * m_count; +} + +int AUD_LoopReader::getPosition() const +{ + return m_reader->getPosition() * (m_count < 0 ? 1 : m_count); +} + +void AUD_LoopReader::read(int & length, sample_t* & buffer) +{ + AUD_Specs specs = m_reader->getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + + int len = length; + + m_reader->read(len, buffer); + + if(len < length && m_left) + { + int pos = 0; + + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + + sample_t* buf = m_buffer.getBuffer(); + + memcpy(buf + pos * specs.channels, buffer, len * samplesize); + + pos += len; + + while(pos < length && m_left) + { + if(m_left > 0) + m_left--; + + m_reader->seek(0); + + len = length - pos; + m_reader->read(len, buffer); + + // prevent endless loop + if(!len) + break; + + memcpy(buf + pos * specs.channels, buffer, len * samplesize); + + pos += len; + } + + length = pos; + buffer = buf; + } + else + length = len; +} diff --git a/intern/audaspace/FX/AUD_LoopReader.h b/intern/audaspace/FX/AUD_LoopReader.h new file mode 100644 index 00000000000..e0ed4cb6bf3 --- /dev/null +++ b/intern/audaspace/FX/AUD_LoopReader.h @@ -0,0 +1,73 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LOOPREADER +#define AUD_LOOPREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This class reads another reader and loops it. + * \note The other reader must be seekable. + */ +class AUD_LoopReader : public AUD_EffectReader +{ +private: + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * The loop count. + */ + const int m_count; + + /** + * The left loop count. + */ + int m_left; + + // hide copy constructor and operator= + AUD_LoopReader(const AUD_LoopReader&); + AUD_LoopReader& operator=(const AUD_LoopReader&); + +public: + /** + * Creates a new loop reader. + * \param reader The reader to read from. + * \param loop The desired loop count, negative values result in endless + * looping. + */ + AUD_LoopReader(AUD_IReader* reader, int loop); + + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_LOOPREADER diff --git a/intern/audaspace/FX/AUD_LowpassFactory.cpp b/intern/audaspace/FX/AUD_LowpassFactory.cpp new file mode 100644 index 00000000000..9244e07631b --- /dev/null +++ b/intern/audaspace/FX/AUD_LowpassFactory.cpp @@ -0,0 +1,61 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LowpassFactory.h" +#include "AUD_IIRFilterReader.h" + +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +AUD_LowpassFactory::AUD_LowpassFactory(AUD_IFactory* factory, float frequency, + float Q) : + AUD_EffectFactory(factory), + m_frequency(frequency), + m_Q(Q) +{ +} + +AUD_IReader* AUD_LowpassFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + // calculate coefficients + float w0 = 2 * M_PI * m_frequency / reader->getSpecs().rate; + float alpha = sin(w0) / (2 * m_Q); + float norm = 1 + alpha; + float c = cos(w0); + std::vector a, b; + a.push_back(1); + a.push_back(-2 * c / norm); + a.push_back((1 - alpha) / norm); + b.push_back((1 - c) / (2 * norm)); + b.push_back((1 - c) / norm); + b.push_back(b[0]); + + return new AUD_IIRFilterReader(reader, b, a); +} diff --git a/intern/audaspace/FX/AUD_LowpassFactory.h b/intern/audaspace/FX/AUD_LowpassFactory.h new file mode 100644 index 00000000000..61b76510a9e --- /dev/null +++ b/intern/audaspace/FX/AUD_LowpassFactory.h @@ -0,0 +1,63 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LOWPASSFACTORY +#define AUD_LOWPASSFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates a lowpass filter reader. + */ +class AUD_LowpassFactory : public AUD_EffectFactory +{ +private: + /** + * The attack value in seconds. + */ + const float m_frequency; + + /** + * The Q factor. + */ + const float m_Q; + + // hide copy constructor and operator= + AUD_LowpassFactory(const AUD_LowpassFactory&); + AUD_LowpassFactory& operator=(const AUD_LowpassFactory&); + +public: + /** + * Creates a new lowpass factory. + * \param factory The input factory. + * \param frequency The cutoff frequency. + * \param Q The Q factor. + */ + AUD_LowpassFactory(AUD_IFactory* factory, float frequency, float Q = 1.0f); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_LOWPASSFACTORY diff --git a/intern/audaspace/FX/AUD_PingPongFactory.cpp b/intern/audaspace/FX/AUD_PingPongFactory.cpp new file mode 100644 index 00000000000..b3aaa9e80a4 --- /dev/null +++ b/intern/audaspace/FX/AUD_PingPongFactory.cpp @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_PingPongFactory.h" +#include "AUD_DoubleReader.h" +#include "AUD_ReverseFactory.h" + +AUD_PingPongFactory::AUD_PingPongFactory(AUD_IFactory* factory) : + AUD_EffectFactory(factory) +{ +} + +AUD_IReader* AUD_PingPongFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + AUD_IReader* reader2; + AUD_ReverseFactory factory(m_factory); + + try + { + reader2 = factory.createReader(); + } + catch(AUD_Exception&) + { + delete reader; + throw; + } + + return new AUD_DoubleReader(reader, reader2); +} diff --git a/intern/audaspace/FX/AUD_PingPongFactory.h b/intern/audaspace/FX/AUD_PingPongFactory.h new file mode 100644 index 00000000000..82aedca8f7f --- /dev/null +++ b/intern/audaspace/FX/AUD_PingPongFactory.h @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_PINGPONGFACTORY +#define AUD_PINGPONGFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory plays another factory first normal, then reversed. + * \note Readers from the underlying factory must be from the buffer type. + */ +class AUD_PingPongFactory : public AUD_EffectFactory +{ +private: + // hide copy constructor and operator= + AUD_PingPongFactory(const AUD_PingPongFactory&); + AUD_PingPongFactory& operator=(const AUD_PingPongFactory&); + +public: + /** + * Creates a new ping pong factory. + * \param factory The input factory. + */ + AUD_PingPongFactory(AUD_IFactory* factory); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_PINGPONGFACTORY diff --git a/intern/audaspace/FX/AUD_PitchFactory.cpp b/intern/audaspace/FX/AUD_PitchFactory.cpp new file mode 100644 index 00000000000..be285562db3 --- /dev/null +++ b/intern/audaspace/FX/AUD_PitchFactory.cpp @@ -0,0 +1,39 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_PitchFactory.h" +#include "AUD_PitchReader.h" +#include "AUD_Space.h" + +AUD_PitchFactory::AUD_PitchFactory(AUD_IFactory* factory, float pitch) : + AUD_EffectFactory(factory), + m_pitch(pitch) +{ +} + +AUD_IReader* AUD_PitchFactory::createReader() const +{ + return new AUD_PitchReader(getReader(), m_pitch); +} diff --git a/intern/audaspace/FX/AUD_PitchFactory.h b/intern/audaspace/FX/AUD_PitchFactory.h new file mode 100644 index 00000000000..52b9b7d99e5 --- /dev/null +++ b/intern/audaspace/FX/AUD_PitchFactory.h @@ -0,0 +1,57 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_PITCHFACTORY +#define AUD_PITCHFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory changes the pitch of another factory. + */ +class AUD_PitchFactory : public AUD_EffectFactory +{ +private: + /** + * The pitch. + */ + const float m_pitch; + + // hide copy constructor and operator= + AUD_PitchFactory(const AUD_PitchFactory&); + AUD_PitchFactory& operator=(const AUD_PitchFactory&); + +public: + /** + * Creates a new pitch factory. + * \param factory The input factory. + * \param pitch The desired pitch. + */ + AUD_PitchFactory(AUD_IFactory* factory, float pitch); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_PITCHFACTORY diff --git a/intern/audaspace/FX/AUD_PitchReader.cpp b/intern/audaspace/FX/AUD_PitchReader.cpp new file mode 100644 index 00000000000..19c3be31968 --- /dev/null +++ b/intern/audaspace/FX/AUD_PitchReader.cpp @@ -0,0 +1,38 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_PitchReader.h" + +AUD_PitchReader::AUD_PitchReader(AUD_IReader* reader, float pitch) : + AUD_EffectReader(reader), m_pitch(pitch) +{ +} + +AUD_Specs AUD_PitchReader::getSpecs() const +{ + AUD_Specs specs = m_reader->getSpecs(); + specs.rate = (AUD_SampleRate)((int)(specs.rate * m_pitch)); + return specs; +} diff --git a/intern/audaspace/FX/AUD_PitchReader.h b/intern/audaspace/FX/AUD_PitchReader.h new file mode 100644 index 00000000000..cc188cf0ee4 --- /dev/null +++ b/intern/audaspace/FX/AUD_PitchReader.h @@ -0,0 +1,57 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_PITCHREADER +#define AUD_PITCHREADER + +#include "AUD_EffectReader.h" + +/** + * This class reads another reader and changes it's pitch. + */ +class AUD_PitchReader : public AUD_EffectReader +{ +private: + /** + * The pitch level. + */ + const float m_pitch; + + // hide copy constructor and operator= + AUD_PitchReader(const AUD_PitchReader&); + AUD_PitchReader& operator=(const AUD_PitchReader&); + +public: + /** + * Creates a new pitch reader. + * \param reader The reader to read from. + * \param pitch The size of the buffer. + */ + AUD_PitchReader(AUD_IReader* reader, float pitch); + + virtual AUD_Specs getSpecs() const; +}; + +#endif //AUD_PITCHREADER diff --git a/intern/audaspace/FX/AUD_RectifyFactory.cpp b/intern/audaspace/FX/AUD_RectifyFactory.cpp new file mode 100644 index 00000000000..2228f949dfe --- /dev/null +++ b/intern/audaspace/FX/AUD_RectifyFactory.cpp @@ -0,0 +1,44 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_RectifyFactory.h" +#include "AUD_CallbackIIRFilterReader.h" + +#include + +sample_t rectifyFilter(AUD_CallbackIIRFilterReader* reader, void* useless) +{ + return fabs(reader->x(0)); +} + +AUD_RectifyFactory::AUD_RectifyFactory(AUD_IFactory* factory) : + AUD_EffectFactory(factory) +{ +} + +AUD_IReader* AUD_RectifyFactory::createReader() const +{ + return new AUD_CallbackIIRFilterReader(getReader(), 1, 1, rectifyFilter); +} diff --git a/intern/audaspace/FX/AUD_RectifyFactory.h b/intern/audaspace/FX/AUD_RectifyFactory.h new file mode 100644 index 00000000000..d8b39e83fdf --- /dev/null +++ b/intern/audaspace/FX/AUD_RectifyFactory.h @@ -0,0 +1,51 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_RECTIFYFACTORY +#define AUD_RECTIFYFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory rectifies another factory. + */ +class AUD_RectifyFactory : public AUD_EffectFactory +{ +private: + // hide copy constructor and operator= + AUD_RectifyFactory(const AUD_RectifyFactory&); + AUD_RectifyFactory& operator=(const AUD_RectifyFactory&); + +public: + /** + * Creates a new rectify factory. + * \param factory The input factory. + */ + AUD_RectifyFactory(AUD_IFactory* factory); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_RECTIFYFACTORY diff --git a/intern/audaspace/FX/AUD_ReverseFactory.cpp b/intern/audaspace/FX/AUD_ReverseFactory.cpp new file mode 100644 index 00000000000..1002e2de87e --- /dev/null +++ b/intern/audaspace/FX/AUD_ReverseFactory.cpp @@ -0,0 +1,38 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ReverseFactory.h" +#include "AUD_ReverseReader.h" +#include "AUD_Space.h" + +AUD_ReverseFactory::AUD_ReverseFactory(AUD_IFactory* factory) : + AUD_EffectFactory(factory) +{ +} + +AUD_IReader* AUD_ReverseFactory::createReader() const +{ + return new AUD_ReverseReader(getReader()); +} diff --git a/intern/audaspace/FX/AUD_ReverseFactory.h b/intern/audaspace/FX/AUD_ReverseFactory.h new file mode 100644 index 00000000000..a1995ee68f9 --- /dev/null +++ b/intern/audaspace/FX/AUD_ReverseFactory.h @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_REVERSEFACTORY +#define AUD_REVERSEFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory reads another factory reverted. + * \note Readers from the underlying factory must be from the buffer type. + */ +class AUD_ReverseFactory : public AUD_EffectFactory +{ +private: + // hide copy constructor and operator= + AUD_ReverseFactory(const AUD_ReverseFactory&); + AUD_ReverseFactory& operator=(const AUD_ReverseFactory&); + +public: + /** + * Creates a new reverse factory. + * \param factory The input factory. + */ + AUD_ReverseFactory(AUD_IFactory* factory); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_REVERSEFACTORY diff --git a/intern/audaspace/FX/AUD_ReverseReader.cpp b/intern/audaspace/FX/AUD_ReverseReader.cpp new file mode 100644 index 00000000000..c651ea79577 --- /dev/null +++ b/intern/audaspace/FX/AUD_ReverseReader.cpp @@ -0,0 +1,101 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ReverseReader.h" + +#include + +static const char* props_error = "AUD_ReverseReader: The reader has to be " + "seekable and a finite length."; + +AUD_ReverseReader::AUD_ReverseReader(AUD_IReader* reader) : + AUD_EffectReader(reader), + m_length(reader->getLength()), + m_position(0) +{ + if(m_length < 0 || !reader->isSeekable()) + AUD_THROW(AUD_ERROR_PROPS, props_error); +} + +void AUD_ReverseReader::seek(int position) +{ + m_position = position; +} + +int AUD_ReverseReader::getLength() const +{ + return m_length; +} + +int AUD_ReverseReader::getPosition() const +{ + return m_position; +} + +void AUD_ReverseReader::read(int & length, sample_t* & buffer) +{ + // first correct the length + if(m_position + length > m_length) + length = m_length - m_position; + + if(length <= 0) + { + length = 0; + return; + } + + AUD_Specs specs = getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + + // resize buffer if needed + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + + buffer = m_buffer.getBuffer(); + + sample_t* buf; + int len = length; + + // read from reader + m_reader->seek(m_length - m_position - len); + m_reader->read(len, buf); + + // set null if reader didn't give enough data + if(len < length) + { + memset(buffer, 0, (length - len) * samplesize); + buffer += (length - len) * specs.channels; + } + + // copy the samples reverted + for(int i = 0; i < len; i++) + memcpy(buffer + i * specs.channels, + buf + (len - 1 - i) * specs.channels, + samplesize); + + m_position += length; + + buffer = m_buffer.getBuffer(); +} diff --git a/intern/audaspace/FX/AUD_ReverseReader.h b/intern/audaspace/FX/AUD_ReverseReader.h new file mode 100644 index 00000000000..8eb960a5dee --- /dev/null +++ b/intern/audaspace/FX/AUD_ReverseReader.h @@ -0,0 +1,73 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_REVERSEREADER +#define AUD_REVERSEREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This class reads another reader from back to front. + * \note The underlying reader must be a buffer. + */ +class AUD_ReverseReader : public AUD_EffectReader +{ +private: + /** + * The sample count. + */ + const int m_length; + + /** + * The current position. + */ + int m_position; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + // hide copy constructor and operator= + AUD_ReverseReader(const AUD_ReverseReader&); + AUD_ReverseReader& operator=(const AUD_ReverseReader&); + +public: + /** + * Creates a new reverse reader. + * \param reader The reader to read from. + * \exception AUD_Exception Thrown if the reader specified has an + * undeterminable/infinite length or is not seekable. + */ + AUD_ReverseReader(AUD_IReader* reader); + + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_REVERSEREADER diff --git a/intern/audaspace/FX/AUD_SquareFactory.cpp b/intern/audaspace/FX/AUD_SquareFactory.cpp new file mode 100644 index 00000000000..c321a13d79a --- /dev/null +++ b/intern/audaspace/FX/AUD_SquareFactory.cpp @@ -0,0 +1,62 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SquareFactory.h" +#include "AUD_CallbackIIRFilterReader.h" + +sample_t squareFilter(AUD_CallbackIIRFilterReader* reader, float* threshold) +{ + float in = reader->x(0); + if(in >= *threshold) + return 1; + else if(in <= -*threshold) + return -1; + else + return 0; +} + +void endSquareFilter(float* threshold) +{ + delete threshold; +} + +AUD_SquareFactory::AUD_SquareFactory(AUD_IFactory* factory, float threshold) : + AUD_EffectFactory(factory), + m_threshold(threshold) +{ +} + +float AUD_SquareFactory::getThreshold() const +{ + return m_threshold; +} + +AUD_IReader* AUD_SquareFactory::createReader() const +{ + return new AUD_CallbackIIRFilterReader(getReader(), 1, 1, + (doFilterIIR) squareFilter, + (endFilterIIR) endSquareFilter, + new float(m_threshold)); +} diff --git a/intern/audaspace/FX/AUD_SquareFactory.h b/intern/audaspace/FX/AUD_SquareFactory.h new file mode 100644 index 00000000000..da87dc6f888 --- /dev/null +++ b/intern/audaspace/FX/AUD_SquareFactory.h @@ -0,0 +1,62 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SQUAREFACTORY +#define AUD_SQUAREFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory Transforms any signal to a square signal. + */ +class AUD_SquareFactory : public AUD_EffectFactory +{ +private: + /** + * The threshold. + */ + const float m_threshold; + + // hide copy constructor and operator= + AUD_SquareFactory(const AUD_SquareFactory&); + AUD_SquareFactory& operator=(const AUD_SquareFactory&); + +public: + /** + * Creates a new square factory. + * \param factory The input factory. + * \param threshold The threshold. + */ + AUD_SquareFactory(AUD_IFactory* factory, float threshold = 0.0f); + + /** + * Returns the threshold. + */ + float getThreshold() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SQUAREFACTORY diff --git a/intern/audaspace/FX/AUD_SumFactory.cpp b/intern/audaspace/FX/AUD_SumFactory.cpp new file mode 100644 index 00000000000..a128e50504c --- /dev/null +++ b/intern/audaspace/FX/AUD_SumFactory.cpp @@ -0,0 +1,41 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SumFactory.h" +#include "AUD_IIRFilterReader.h" + +AUD_SumFactory::AUD_SumFactory(AUD_IFactory* factory) : + AUD_EffectFactory(factory) +{ +} + +AUD_IReader* AUD_SumFactory::createReader() const +{ + std::vector a, b; + a.push_back(1); + a.push_back(-1); + b.push_back(1); + return new AUD_IIRFilterReader(getReader(), b, a); +} diff --git a/intern/audaspace/FX/AUD_SumFactory.h b/intern/audaspace/FX/AUD_SumFactory.h new file mode 100644 index 00000000000..045a0a3a625 --- /dev/null +++ b/intern/audaspace/FX/AUD_SumFactory.h @@ -0,0 +1,51 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SUMFACTORY +#define AUD_SUMFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates a sum reader. + */ +class AUD_SumFactory : public AUD_EffectFactory +{ +private: + // hide copy constructor and operator= + AUD_SumFactory(const AUD_SumFactory&); + AUD_SumFactory& operator=(const AUD_SumFactory&); + +public: + /** + * Creates a new sum factory. + * \param factory The input factory. + */ + AUD_SumFactory(AUD_IFactory* factory); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SUMFACTORY diff --git a/intern/audaspace/FX/AUD_SuperposeFactory.cpp b/intern/audaspace/FX/AUD_SuperposeFactory.cpp new file mode 100644 index 00000000000..6bfc1125ca6 --- /dev/null +++ b/intern/audaspace/FX/AUD_SuperposeFactory.cpp @@ -0,0 +1,49 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SuperposeFactory.h" +#include "AUD_SuperposeReader.h" + +AUD_SuperposeFactory::AUD_SuperposeFactory(AUD_IFactory* factory1, AUD_IFactory* factory2) : + m_factory1(factory1), m_factory2(factory2) +{ +} + +AUD_IReader* AUD_SuperposeFactory::createReader() const +{ + AUD_IReader* reader1 = m_factory1->createReader(); + AUD_IReader* reader2; + try + { + reader2 = m_factory2->createReader(); + } + catch(AUD_Exception&) + { + delete reader1; + throw; + } + + return new AUD_SuperposeReader(reader1, reader2); +} diff --git a/intern/audaspace/FX/AUD_SuperposeFactory.h b/intern/audaspace/FX/AUD_SuperposeFactory.h new file mode 100644 index 00000000000..a7fde2c182f --- /dev/null +++ b/intern/audaspace/FX/AUD_SuperposeFactory.h @@ -0,0 +1,63 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SUPERPOSEFACTORY +#define AUD_SUPERPOSEFACTORY + +#include "AUD_IFactory.h" + +/** + * This factory plays two other factories behind each other. + * \note Readers from the underlying factories must have the same sample rate and channel count. + */ +class AUD_SuperposeFactory : public AUD_IFactory +{ +private: + /** + * First played factory. + */ + AUD_IFactory* m_factory1; + + /** + * Second played factory. + */ + AUD_IFactory* m_factory2; + + // hide copy constructor and operator= + AUD_SuperposeFactory(const AUD_SuperposeFactory&); + AUD_SuperposeFactory& operator=(const AUD_SuperposeFactory&); + +public: + /** + * Creates a new superpose factory. + * \param factory1 The first input factory. + * \param factory2 The second input factory. + */ + AUD_SuperposeFactory(AUD_IFactory* factory1, AUD_IFactory* factory2); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SUPERPOSEFACTORY diff --git a/intern/audaspace/FX/AUD_SuperposeReader.cpp b/intern/audaspace/FX/AUD_SuperposeReader.cpp new file mode 100644 index 00000000000..546b79a3a77 --- /dev/null +++ b/intern/audaspace/FX/AUD_SuperposeReader.cpp @@ -0,0 +1,115 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SuperposeReader.h" + +#include + +static const char* specs_error = "AUD_SuperposeReader: Both readers have to " + "have the same specs."; + +AUD_SuperposeReader::AUD_SuperposeReader(AUD_IReader* reader1, AUD_IReader* reader2) : + m_reader1(reader1), m_reader2(reader2) +{ + try + { + AUD_Specs s1, s2; + s1 = reader1->getSpecs(); + s2 = reader2->getSpecs(); + if(memcmp(&s1, &s2, sizeof(AUD_Specs))) + AUD_THROW(AUD_ERROR_SPECS, specs_error); + } + catch(AUD_Exception&) + { + delete reader1; + delete reader2; + + throw; + } +} + +AUD_SuperposeReader::~AUD_SuperposeReader() +{ + delete m_reader1; + delete m_reader2; +} + +bool AUD_SuperposeReader::isSeekable() const +{ + return m_reader1->isSeekable() && m_reader2->isSeekable(); +} + +void AUD_SuperposeReader::seek(int position) +{ + m_reader1->seek(position); + m_reader2->seek(position); +} + +int AUD_SuperposeReader::getLength() const +{ + int len1 = m_reader1->getLength(); + int len2 = m_reader2->getLength(); + if((len1 < 0) || (len2 < 0)) + return -1; + return AUD_MIN(len1, len2); +} + +int AUD_SuperposeReader::getPosition() const +{ + int pos1 = m_reader1->getPosition(); + int pos2 = m_reader2->getPosition(); + return AUD_MAX(pos1, pos2); +} + +AUD_Specs AUD_SuperposeReader::getSpecs() const +{ + return m_reader1->getSpecs(); +} + +void AUD_SuperposeReader::read(int & length, sample_t* & buffer) +{ + AUD_Specs specs = m_reader1->getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + buffer = m_buffer.getBuffer(); + + int len1 = length; + sample_t* buf; + m_reader1->read(len1, buf); + memcpy(buffer, buf, len1 * samplesize); + + if(len1 < length) + memset(buffer + len1 * specs.channels, 0, (length - len1) * samplesize); + + int len2 = length; + m_reader2->read(len2, buf); + + for(int i = 0; i < len2 * specs.channels; i++) + buffer[i] += buf[i]; + + length = AUD_MAX(len1, len2); +} diff --git a/intern/audaspace/FX/AUD_SuperposeReader.h b/intern/audaspace/FX/AUD_SuperposeReader.h new file mode 100644 index 00000000000..eeceb9adfeb --- /dev/null +++ b/intern/audaspace/FX/AUD_SuperposeReader.h @@ -0,0 +1,79 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SUPERPOSEREADER +#define AUD_SUPERPOSEREADER + +#include "AUD_IReader.h" +#include "AUD_Buffer.h" + +/** + * This reader plays two readers with the same specs sequently. + */ +class AUD_SuperposeReader : public AUD_IReader +{ +private: + /** + * The first reader. + */ + AUD_IReader* m_reader1; + + /** + * The second reader. + */ + AUD_IReader* m_reader2; + + /** + * The playback buffer for the intersecting part. + */ + AUD_Buffer m_buffer; + + // hide copy constructor and operator= + AUD_SuperposeReader(const AUD_SuperposeReader&); + AUD_SuperposeReader& operator=(const AUD_SuperposeReader&); + +public: + /** + * Creates a new superpose reader. + * \param reader1 The first reader to read from. + * \param reader2 The second reader to read from. + * \exception AUD_Exception Thrown if the specs from the readers differ. + */ + AUD_SuperposeReader(AUD_IReader* reader1, AUD_IReader* reader2); + + /** + * Destroys the reader. + */ + virtual ~AUD_SuperposeReader(); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_SUPERPOSEREADER diff --git a/intern/audaspace/FX/AUD_VolumeFactory.cpp b/intern/audaspace/FX/AUD_VolumeFactory.cpp new file mode 100644 index 00000000000..1b341a5a51a --- /dev/null +++ b/intern/audaspace/FX/AUD_VolumeFactory.cpp @@ -0,0 +1,46 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_VolumeFactory.h" +#include "AUD_IIRFilterReader.h" + +AUD_VolumeFactory::AUD_VolumeFactory(AUD_IFactory* factory, float volume) : + AUD_EffectFactory(factory), + m_volume(volume) +{ +} + +float AUD_VolumeFactory::getVolume() const +{ + return m_volume; +} + +AUD_IReader* AUD_VolumeFactory::createReader() const +{ + std::vector a, b; + a.push_back(1); + b.push_back(m_volume); + return new AUD_IIRFilterReader(getReader(), b, a); +} diff --git a/intern/audaspace/FX/AUD_VolumeFactory.h b/intern/audaspace/FX/AUD_VolumeFactory.h new file mode 100644 index 00000000000..a086aab4640 --- /dev/null +++ b/intern/audaspace/FX/AUD_VolumeFactory.h @@ -0,0 +1,64 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_VOLUMEFACTORY +#define AUD_VOLUMEFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory changes the volume of another factory. + * The set volume should be a value between 0.0 and 1.0, higher values at your + * own risk! + */ +class AUD_VolumeFactory : public AUD_EffectFactory +{ +private: + /** + * The volume. + */ + const float m_volume; + + // hide copy constructor and operator= + AUD_VolumeFactory(const AUD_VolumeFactory&); + AUD_VolumeFactory& operator=(const AUD_VolumeFactory&); + +public: + /** + * Creates a new volume factory. + * \param factory The input factory. + * \param volume The desired volume. + */ + AUD_VolumeFactory(AUD_IFactory* factory, float volume); + + /** + * Returns the volume. + */ + float getVolume() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_VOLUMEFACTORY diff --git a/intern/audaspace/FX/Makefile b/intern/audaspace/FX/Makefile new file mode 100644 index 00000000000..bda0e2bdab6 --- /dev/null +++ b/intern/audaspace/FX/Makefile @@ -0,0 +1,43 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_fx +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I../ffmpeg +CPPFLAGS += -I../intern +CPPFLAGS += -I../SDL +CPPFLAGS += -I../SRC +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/Makefile b/intern/audaspace/Makefile new file mode 100644 index 00000000000..c1a613af1ae --- /dev/null +++ b/intern/audaspace/Makefile @@ -0,0 +1,119 @@ +# -*- mode: gnumakefile; tab-width: 8; indent-tabs-mode: t; -*- +# vim: tabstop=8 +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): GSR +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +include nan_definitions.mk + +LIBNAME = audaspace +SOURCEDIR = intern/audaspace +DIR = $(OCGDIR)/$(SOURCEDIR) +DIRS = intern +DIRS += FX +DIRS += SDL +DIRS += SRC +DIRS += Python + +ifeq ($(WITH_FFMPEG),true) + DIRS += ffmpeg +endif + +ifeq ($(WITH_OPENAL),true) + DIRS += OpenAL +endif + +ifeq ($(WITH_JACK),true) + DIRS += jack +endif + +ifeq ($(WITH_SNDFILE),true) + DIRS += sndfile +endif + +#ifeq ($(WITH_FFTW3),true) +# DIRS += fftw +#endif + +include nan_subdirs.mk + +install: $(ALL_OR_DEBUG) + @[ -d $(NAN_AUDASPACE) ] || mkdir $(NAN_AUDASPACE) + @[ -d $(NAN_AUDASPACE)/include ] || mkdir $(NAN_AUDASPACE)/include + @[ -d $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) ] || mkdir $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) + @../tools/cpifdiff.sh $(DIR)/$(DEBUG_DIR)libaudaspace.a $(DIR)/$(DEBUG_DIR)libaud_sdl.a $(DIR)/$(DEBUG_DIR)libaud_python.a $(DIR)/$(DEBUG_DIR)libaud_fx.a $(DIR)/$(DEBUG_DIR)libaud_src.a $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) + +ifeq ($(WITH_FFMPEG),true) + @../tools/cpifdiff.sh $(DIR)/$(DEBUG_DIR)libaud_ffmpeg.a $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) +endif + +ifeq ($(WITH_OPENAL),true) + @../tools/cpifdiff.sh $(DIR)/$(DEBUG_DIR)libaud_openal.a $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) +endif + +ifeq ($(WITH_JACK),true) + @../tools/cpifdiff.sh $(DIR)/$(DEBUG_DIR)libaud_jack.a $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) +endif + +ifeq ($(WITH_SNDFILE),true) + @../tools/cpifdiff.sh $(DIR)/$(DEBUG_DIR)libaud_sndfile.a $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) +endif + +#ifeq ($(WITH_FFTW3),true) +# @../tools/cpifdiff.sh $(DIR)/$(DEBUG_DIR)libaud_fftw.a $(NAN_AUDASPACE)/lib/$(DEBUG_DIR) +#endif + +ifeq ($(OS),darwin) + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaudaspace.a + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_src.a + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_fx.a + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_sdl.a + +ifeq ($(WITH_FFMPEG),true) + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_ffmpeg.a +endif + +ifeq ($(WITH_OPENAL),true) + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_openal.a +endif + +ifeq ($(WITH_JACK),true) + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_jack.a +endif + +ifeq ($(WITH_SNDFILE),true) + ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_sndfile.a +endif + +#ifeq ($(WITH_FFTW3),true) +# ranlib $(NAN_AUDASPACE)/lib/$(DEBUG_DIR)libaud_fftw.a +#endif + +endif + @../tools/cpifdiff.sh intern/*.h $(NAN_AUDASPACE)/include/ diff --git a/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp b/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp new file mode 100644 index 00000000000..a87a4ad5eef --- /dev/null +++ b/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp @@ -0,0 +1,1623 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_OpenALDevice.h" +#include "AUD_IFactory.h" +#include "AUD_IReader.h" +#include "AUD_ConverterReader.h" + +#include +#include + +#ifdef WIN32 +#include +#else +#include +#endif + +#define AUD_OPENAL_CYCLE_BUFFERS 3 + +/// Saves the data for playback. +struct AUD_OpenALHandle : AUD_Handle +{ + /// Whether it's a buffered or a streamed source. + bool isBuffered; + + /// The reader source. + AUD_IReader* reader; + + /// Whether to keep the source if end of it is reached. + bool keep; + + /// OpenAL sample format. + ALenum format; + + /// OpenAL source. + ALuint source; + + /// OpenAL buffers. + ALuint buffers[AUD_OPENAL_CYCLE_BUFFERS]; + + /// The first buffer to be read next. + int current; + + /// Whether the stream doesn't return any more data. + bool data_end; + + /// The loop count of the source. + int loopcount; + + /// The stop callback. + stopCallback stop; + + /// Stop callback data. + void* stop_data; +}; + +struct AUD_OpenALBufferedFactory +{ + /// The factory. + AUD_IFactory* factory; + + /// The OpenAL buffer. + ALuint buffer; +}; + +typedef std::list::iterator AUD_HandleIterator; +typedef std::list::iterator AUD_BFIterator; + +/******************************************************************************/ +/**************************** Threading Code **********************************/ +/******************************************************************************/ + +void* AUD_openalRunThread(void* device) +{ + AUD_OpenALDevice* dev = (AUD_OpenALDevice*)device; + dev->updateStreams(); + return NULL; +} + +void AUD_OpenALDevice::start() +{ + lock(); + + if(!m_playing) + { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + pthread_create(&m_thread, &attr, AUD_openalRunThread, this); + + pthread_attr_destroy(&attr); + + m_playing = true; + } + + unlock(); +} + +void AUD_OpenALDevice::updateStreams() +{ + AUD_OpenALHandle* sound; + + int length; + sample_t* buffer; + + ALint info; + AUD_DeviceSpecs specs = m_specs; + + while(1) + { + lock(); + + alcSuspendContext(m_context); + + { + // for all sounds + for(AUD_HandleIterator it = m_playingSounds->begin(); it != m_playingSounds->end(); it++) + { + sound = *it; + + // is it a streamed sound? + if(!sound->isBuffered) + { + // check for buffer refilling + alGetSourcei(sound->source, AL_BUFFERS_PROCESSED, &info); + + if(info) + { + specs.specs = sound->reader->getSpecs(); + + // for all empty buffers + while(info--) + { + // if there's still data to play back + if(!sound->data_end) + { + // read data + length = m_buffersize; + sound->reader->read(length, buffer); + + // looping necessary? + if(length == 0 && sound->loopcount) + { + if(sound->loopcount > 0) + sound->loopcount--; + + sound->reader->seek(0); + + length = m_buffersize; + sound->reader->read(length, buffer); + } + + // read nothing? + if(length == 0) + { + sound->data_end = true; + break; + } + + // unqueue buffer + alSourceUnqueueBuffers(sound->source, 1, + &sound->buffers[sound->current]); + ALenum err; + if((err = alGetError()) != AL_NO_ERROR) + { + sound->data_end = true; + break; + } + + // fill with new data + alBufferData(sound->buffers[sound->current], + sound->format, + buffer, length * + AUD_DEVICE_SAMPLE_SIZE(specs), + specs.rate); + + if((err = alGetError()) != AL_NO_ERROR) + { + sound->data_end = true; + break; + } + + // and queue again + alSourceQueueBuffers(sound->source, 1, + &sound->buffers[sound->current]); + if(alGetError() != AL_NO_ERROR) + { + sound->data_end = true; + break; + } + + sound->current = (sound->current+1) % + AUD_OPENAL_CYCLE_BUFFERS; + } + else + break; + } + } + } + + // check if the sound has been stopped + alGetSourcei(sound->source, AL_SOURCE_STATE, &info); + + if(info != AL_PLAYING) + { + // if it really stopped + if(sound->data_end) + { + if(sound->stop) + sound->stop(sound->stop_data); + + // increment the iterator to the next value, + // because the sound gets deleted in the list here. + ++it; + // pause or + if(sound->keep) + pause(sound); + // stop + else + stop(sound); + // decrement again, so that we get the next sound in the + // next loop run + if(m_playingSounds->empty()) + break; + else + --it; + } + // continue playing + else + alSourcePlay(sound->source); + } + } + } + + alcProcessContext(m_context); + + // stop thread + if(m_playingSounds->empty()) + { + unlock(); + m_playing = false; + pthread_exit(NULL); + } + + unlock(); + +#ifdef WIN32 + Sleep(20); +#else + usleep(20000); +#endif + } +} + +/******************************************************************************/ +/**************************** IDevice Code ************************************/ +/******************************************************************************/ + +bool AUD_OpenALDevice::isValid(AUD_Handle* handle) +{ + for(AUD_HandleIterator i = m_playingSounds->begin(); + i != m_playingSounds->end(); i++) + if(*i == handle) + return true; + for(AUD_HandleIterator i = m_pausedSounds->begin(); + i != m_pausedSounds->end(); i++) + if(*i == handle) + return true; + return false; +} + +static const char* open_error = "AUD_OpenALDevice: Device couldn't be opened."; + +AUD_OpenALDevice::AUD_OpenALDevice(AUD_DeviceSpecs specs, int buffersize) +{ + // cannot determine how many channels or which format OpenAL uses, but + // it at least is able to play 16 bit stereo audio + specs.channels = AUD_CHANNELS_STEREO; + specs.format = AUD_FORMAT_S16; + +#if 0 + if(alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT") == AL_TRUE) + { + ALCchar* devices = const_cast(alcGetString(NULL, ALC_DEVICE_SPECIFIER)); + printf("OpenAL devices (standard is: %s):\n", alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER)); + + while(*devices) + { + printf("%s\n", devices); + devices += strlen(devices) + 1; + } + } +#endif + + m_device = alcOpenDevice(NULL); + + if(!m_device) + AUD_THROW(AUD_ERROR_OPENAL, open_error); + + // at least try to set the frequency + ALCint attribs[] = { ALC_FREQUENCY, specs.rate, 0 }; + ALCint* attributes = attribs; + if(specs.rate == AUD_RATE_INVALID) + attributes = NULL; + + m_context = alcCreateContext(m_device, attributes); + alcMakeContextCurrent(m_context); + + alcGetIntegerv(m_device, ALC_FREQUENCY, 1, (ALCint*)&specs.rate); + + // check for specific formats and channel counts to be played back + if(alIsExtensionPresent("AL_EXT_FLOAT32") == AL_TRUE) + specs.format = AUD_FORMAT_FLOAT32; + + m_useMC = alIsExtensionPresent("AL_EXT_MCFORMATS") == AL_TRUE; + + alGetError(); + + m_specs = specs; + m_buffersize = buffersize; + m_playing = false; + + m_playingSounds = new std::list(); + m_pausedSounds = new std::list(); + m_bufferedFactories = new std::list(); + + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + + pthread_mutex_init(&m_mutex, &attr); + + pthread_mutexattr_destroy(&attr); +} + +AUD_OpenALDevice::~AUD_OpenALDevice() +{ + AUD_OpenALHandle* sound; + + lock(); + alcSuspendContext(m_context); + + // delete all playing sounds + while(!m_playingSounds->empty()) + { + sound = *(m_playingSounds->begin()); + alDeleteSources(1, &sound->source); + if(!sound->isBuffered) + { + delete sound->reader; + alDeleteBuffers(AUD_OPENAL_CYCLE_BUFFERS, sound->buffers); + } + delete sound; + m_playingSounds->erase(m_playingSounds->begin()); + } + + // delete all paused sounds + while(!m_pausedSounds->empty()) + { + sound = *(m_pausedSounds->begin()); + alDeleteSources(1, &sound->source); + if(!sound->isBuffered) + { + delete sound->reader; + alDeleteBuffers(AUD_OPENAL_CYCLE_BUFFERS, sound->buffers); + } + delete sound; + m_pausedSounds->erase(m_pausedSounds->begin()); + } + + // delete all buffered factories + while(!m_bufferedFactories->empty()) + { + alDeleteBuffers(1, &(*(m_bufferedFactories->begin()))->buffer); + delete *m_bufferedFactories->begin(); + m_bufferedFactories->erase(m_bufferedFactories->begin()); + } + + alcProcessContext(m_context); + + // wait for the thread to stop + if(m_playing) + { + unlock(); + pthread_join(m_thread, NULL); + } + else + unlock(); + + delete m_playingSounds; + delete m_pausedSounds; + delete m_bufferedFactories; + + // quit OpenAL + alcMakeContextCurrent(NULL); + alcDestroyContext(m_context); + alcCloseDevice(m_device); + + pthread_mutex_destroy(&m_mutex); +} + +AUD_DeviceSpecs AUD_OpenALDevice::getSpecs() const +{ + return m_specs; +} + +bool AUD_OpenALDevice::getFormat(ALenum &format, AUD_Specs specs) +{ + bool valid = true; + format = 0; + + switch(m_specs.format) + { + case AUD_FORMAT_S16: + switch(specs.channels) + { + case AUD_CHANNELS_MONO: + format = AL_FORMAT_MONO16; + break; + case AUD_CHANNELS_STEREO: + format = AL_FORMAT_STEREO16; + break; + case AUD_CHANNELS_SURROUND4: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_QUAD16"); + break; + } + case AUD_CHANNELS_SURROUND51: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_51CHN16"); + break; + } + case AUD_CHANNELS_SURROUND61: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_61CHN16"); + break; + } + case AUD_CHANNELS_SURROUND71: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_71CHN16"); + break; + } + default: + valid = false; + } + break; + case AUD_FORMAT_FLOAT32: + switch(specs.channels) + { + case AUD_CHANNELS_MONO: + format = alGetEnumValue("AL_FORMAT_MONO_FLOAT32"); + break; + case AUD_CHANNELS_STEREO: + format = alGetEnumValue("AL_FORMAT_STEREO_FLOAT32"); + break; + case AUD_CHANNELS_SURROUND4: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_QUAD32"); + break; + } + case AUD_CHANNELS_SURROUND51: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_51CHN32"); + break; + } + case AUD_CHANNELS_SURROUND61: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_61CHN32"); + break; + } + case AUD_CHANNELS_SURROUND71: + if(m_useMC) + { + format = alGetEnumValue("AL_FORMAT_71CHN32"); + break; + } + default: + valid = false; + } + break; + default: + valid = false; + } + + if(!format) + valid = false; + + return valid; +} + +static const char* genbuffer_error = "AUD_OpenALDevice: Buffer couldn't be " + "generated."; +static const char* gensource_error = "AUD_OpenALDevice: Source couldn't be " + "generated."; +static const char* queue_error = "AUD_OpenALDevice: Buffer couldn't be " + "queued to the source."; +static const char* bufferdata_error = "AUD_OpenALDevice: Buffer couldn't be " + "filled with data."; + +AUD_Handle* AUD_OpenALDevice::play(AUD_IFactory* factory, bool keep) +{ + lock(); + + AUD_OpenALHandle* sound = NULL; + + try + { + // check if it is a buffered factory + for(AUD_BFIterator i = m_bufferedFactories->begin(); + i != m_bufferedFactories->end(); i++) + { + if((*i)->factory == factory) + { + // create the handle + sound = new AUD_OpenALHandle; + sound->keep = keep; + sound->current = -1; + sound->isBuffered = true; + sound->data_end = true; + sound->loopcount = 0; + sound->stop = NULL; + sound->stop_data = NULL; + + alcSuspendContext(m_context); + + // OpenAL playback code + try + { + alGenSources(1, &sound->source); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL, gensource_error); + + try + { + alSourcei(sound->source, AL_BUFFER, (*i)->buffer); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL, queue_error); + } + catch(AUD_Exception&) + { + alDeleteSources(1, &sound->source); + throw; + } + } + catch(AUD_Exception&) + { + delete sound; + alcProcessContext(m_context); + throw; + } + + // play sound + m_playingSounds->push_back(sound); + + alSourcei(sound->source, AL_SOURCE_RELATIVE, 1); + start(); + + alcProcessContext(m_context); + } + } + } + catch(AUD_Exception&) + { + unlock(); + throw; + } + + unlock(); + + if(sound) + return sound; + + AUD_IReader* reader = factory->createReader(); + + AUD_DeviceSpecs specs = m_specs; + specs.specs = reader->getSpecs(); + + // check format + bool valid = specs.channels != AUD_CHANNELS_INVALID; + + if(m_specs.format != AUD_FORMAT_FLOAT32) + reader = new AUD_ConverterReader(reader, m_specs); + + // create the handle + sound = new AUD_OpenALHandle; + sound->keep = keep; + sound->reader = reader; + sound->current = 0; + sound->isBuffered = false; + sound->data_end = false; + sound->loopcount = 0; + sound->stop = NULL; + sound->stop_data = NULL; + + valid &= getFormat(sound->format, specs.specs); + + if(!valid) + { + delete sound; + delete reader; + return NULL; + } + + lock(); + alcSuspendContext(m_context); + + // OpenAL playback code + try + { + alGenBuffers(AUD_OPENAL_CYCLE_BUFFERS, sound->buffers); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL, genbuffer_error); + + try + { + sample_t* buf; + int length; + + for(int i = 0; i < AUD_OPENAL_CYCLE_BUFFERS; i++) + { + length = m_buffersize; + reader->read(length, buf); + alBufferData(sound->buffers[i], sound->format, buf, + length * AUD_DEVICE_SAMPLE_SIZE(specs), + specs.rate); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL, bufferdata_error); + } + + alGenSources(1, &sound->source); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL, gensource_error); + + try + { + alSourceQueueBuffers(sound->source, AUD_OPENAL_CYCLE_BUFFERS, + sound->buffers); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL, queue_error); + } + catch(AUD_Exception&) + { + alDeleteSources(1, &sound->source); + throw; + } + } + catch(AUD_Exception&) + { + alDeleteBuffers(AUD_OPENAL_CYCLE_BUFFERS, sound->buffers); + throw; + } + } + catch(AUD_Exception&) + { + delete sound; + delete reader; + alcProcessContext(m_context); + unlock(); + throw; + } + + // play sound + m_playingSounds->push_back(sound); + alSourcei(sound->source, AL_SOURCE_RELATIVE, 1); + + start(); + + alcProcessContext(m_context); + unlock(); + + return sound; +} + +bool AUD_OpenALDevice::pause(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + // only songs that are played can be paused + for(AUD_HandleIterator i = m_playingSounds->begin(); + i != m_playingSounds->end(); i++) + { + if(*i == handle) + { + m_pausedSounds->push_back(*i); + alSourcePause((*i)->source); + m_playingSounds->erase(i); + result = true; + break; + } + } + + unlock(); + + return result; +} + +bool AUD_OpenALDevice::resume(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + // only songs that are paused can be resumed + for(AUD_HandleIterator i = m_pausedSounds->begin(); + i != m_pausedSounds->end(); i++) + { + if(*i == handle) + { + m_playingSounds->push_back(*i); + start(); + m_pausedSounds->erase(i); + result = true; + break; + } + } + + unlock(); + + return result; +} + +bool AUD_OpenALDevice::stop(AUD_Handle* handle) +{ + AUD_OpenALHandle* sound; + + bool result = false; + + lock(); + + for(AUD_HandleIterator i = m_playingSounds->begin(); + i != m_playingSounds->end(); i++) + { + if(*i == handle) + { + sound = *i; + alDeleteSources(1, &sound->source); + if(!sound->isBuffered) + { + delete sound->reader; + alDeleteBuffers(AUD_OPENAL_CYCLE_BUFFERS, sound->buffers); + } + delete *i; + m_playingSounds->erase(i); + result = true; + break; + } + } + if(!result) + { + for(AUD_HandleIterator i = m_pausedSounds->begin(); + i != m_pausedSounds->end(); i++) + { + if(*i == handle) + { + sound = *i; + alDeleteSources(1, &sound->source); + if(!sound->isBuffered) + { + delete sound->reader; + alDeleteBuffers(AUD_OPENAL_CYCLE_BUFFERS, sound->buffers); + } + delete *i; + m_pausedSounds->erase(i); + result = true; + break; + } + } + } + + unlock(); + + return result; +} + +bool AUD_OpenALDevice::getKeep(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + if(isValid(handle)) + result = ((AUD_OpenALHandle*)handle)->keep; + + unlock(); + + return result; +} + +bool AUD_OpenALDevice::setKeep(AUD_Handle* handle, bool keep) +{ + bool result = false; + + lock(); + + if(isValid(handle)) + { + ((AUD_OpenALHandle*)handle)->keep = keep; + result = true; + } + + unlock(); + + return result; +} + +bool AUD_OpenALDevice::seek(AUD_Handle* handle, float position) +{ + bool result = false; + + lock(); + + if(isValid(handle)) + { + AUD_OpenALHandle* alhandle = (AUD_OpenALHandle*)handle; + if(alhandle->isBuffered) + alSourcef(alhandle->source, AL_SEC_OFFSET, position); + else + { + alhandle->reader->seek((int)(position * + alhandle->reader->getSpecs().rate)); + alhandle->data_end = false; + + ALint info; + + alGetSourcei(alhandle->source, AL_SOURCE_STATE, &info); + + if(info != AL_PLAYING) + { + if(info == AL_PAUSED) + alSourceStop(alhandle->source); + + alSourcei(alhandle->source, AL_BUFFER, 0); + alhandle->current = 0; + + ALenum err; + if((err = alGetError()) == AL_NO_ERROR) + { + sample_t* buf; + int length; + AUD_DeviceSpecs specs = m_specs; + specs.specs = alhandle->reader->getSpecs(); + + for(int i = 0; i < AUD_OPENAL_CYCLE_BUFFERS; i++) + { + length = m_buffersize; + alhandle->reader->read(length, buf); + alBufferData(alhandle->buffers[i], alhandle->format, + buf, + length * AUD_DEVICE_SAMPLE_SIZE(specs), + specs.rate); + + if(alGetError() != AL_NO_ERROR) + break; + } + + alSourceQueueBuffers(alhandle->source, + AUD_OPENAL_CYCLE_BUFFERS, + alhandle->buffers); + } + + alSourceRewind(alhandle->source); + } + } + result = true; + } + + unlock(); + return result; +} + +float AUD_OpenALDevice::getPosition(AUD_Handle* handle) +{ + float position = 0.0f; + + lock(); + + if(isValid(handle)) + { + AUD_OpenALHandle* h = (AUD_OpenALHandle*)handle; + alGetSourcef(h->source, AL_SEC_OFFSET, &position); + if(!h->isBuffered) + { + AUD_Specs specs = h->reader->getSpecs(); + position += (h->reader->getPosition() - m_buffersize * + AUD_OPENAL_CYCLE_BUFFERS) / + (float)specs.rate; + } + } + + unlock(); + return position; +} + +AUD_Status AUD_OpenALDevice::getStatus(AUD_Handle* handle) +{ + AUD_Status status = AUD_STATUS_INVALID; + + lock(); + + for(AUD_HandleIterator i = m_playingSounds->begin(); + i != m_playingSounds->end(); i++) + { + if(*i == handle) + { + status = AUD_STATUS_PLAYING; + break; + } + } + if(status == AUD_STATUS_INVALID) + { + for(AUD_HandleIterator i = m_pausedSounds->begin(); + i != m_pausedSounds->end(); i++) + { + if(*i == handle) + { + status = AUD_STATUS_PAUSED; + break; + } + } + } + + unlock(); + + return status; +} + +void AUD_OpenALDevice::lock() +{ + pthread_mutex_lock(&m_mutex); +} + +void AUD_OpenALDevice::unlock() +{ + pthread_mutex_unlock(&m_mutex); +} + +float AUD_OpenALDevice::getVolume() const +{ + float result; + alGetListenerf(AL_GAIN, &result); + return result; +} + +void AUD_OpenALDevice::setVolume(float volume) +{ + alListenerf(AL_GAIN, volume); +} + +float AUD_OpenALDevice::getVolume(AUD_Handle* handle) +{ + lock(); + float result = std::numeric_limits::quiet_NaN(); + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source,AL_GAIN, &result); + unlock(); + return result; +} + +bool AUD_OpenALDevice::setVolume(AUD_Handle* handle, float volume) +{ + lock(); + bool result = isValid(handle); + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_GAIN, volume); + unlock(); + return result; +} + +float AUD_OpenALDevice::getPitch(AUD_Handle* handle) +{ + lock(); + float result = std::numeric_limits::quiet_NaN(); + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source,AL_PITCH, &result); + unlock(); + return result; +} + +bool AUD_OpenALDevice::setPitch(AUD_Handle* handle, float pitch) +{ + lock(); + bool result = isValid(handle); + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_PITCH, pitch); + unlock(); + return result; +} + +int AUD_OpenALDevice::getLoopCount(AUD_Handle* handle) +{ + lock(); + int result = 0; + if(isValid(handle)) + result = ((AUD_OpenALHandle*)handle)->loopcount; + unlock(); + return result; +} + +bool AUD_OpenALDevice::setLoopCount(AUD_Handle* handle, int count) +{ + lock(); + bool result = isValid(handle); + if(result) + ((AUD_OpenALHandle*)handle)->loopcount = count; + unlock(); + return result; +} + +bool AUD_OpenALDevice::setStopCallback(AUD_Handle* handle, stopCallback callback, void* data) +{ + lock(); + bool result = isValid(handle); + if(result) + { + AUD_OpenALHandle* h = (AUD_OpenALHandle*)handle; + h->stop = callback; + h->stop_data = data; + } + unlock(); + return result; +} + +/* AUD_XXX Temorary disabled + +bool AUD_OpenALDevice::bufferFactory(void *value) +{ + bool result = false; + AUD_IFactory* factory = (AUD_IFactory*) value; + + // load the factory into an OpenAL buffer + if(factory) + { + // check if the factory is already buffered + lock(); + for(AUD_BFIterator i = m_bufferedFactories->begin(); + i != m_bufferedFactories->end(); i++) + { + if((*i)->factory == factory) + { + result = true; + break; + } + } + unlock(); + if(result) + return result; + + AUD_IReader* reader = factory->createReader(); + + if(reader == NULL) + return false; + + AUD_DeviceSpecs specs = m_specs; + specs.specs = reader->getSpecs(); + + if(m_specs.format != AUD_FORMAT_FLOAT32) + reader = new AUD_ConverterReader(reader, m_specs); + + ALenum format; + + if(!getFormat(format, specs.specs)) + { + delete reader; + return false; + } + + // load into a buffer + lock(); + alcSuspendContext(m_context); + + AUD_OpenALBufferedFactory* bf = new AUD_OpenALBufferedFactory; + bf->factory = factory; + + try + { + alGenBuffers(1, &bf->buffer); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL); + + try + { + sample_t* buf; + int length = reader->getLength(); + + reader->read(length, buf); + alBufferData(bf->buffer, format, buf, + length * AUD_DEVICE_SAMPLE_SIZE(specs), + specs.rate); + if(alGetError() != AL_NO_ERROR) + AUD_THROW(AUD_ERROR_OPENAL); + } + catch(AUD_Exception&) + { + alDeleteBuffers(1, &bf->buffer); + throw; + } + } + catch(AUD_Exception&) + { + delete bf; + delete reader; + alcProcessContext(m_context); + unlock(); + return false; + } + + m_bufferedFactories->push_back(bf); + + alcProcessContext(m_context); + unlock(); + } + else + { + // stop all playing and paused buffered sources + lock(); + alcSuspendContext(m_context); + + AUD_OpenALHandle* sound; + AUD_HandleIterator it = m_playingSounds->begin(); + while(it != m_playingSounds->end()) + { + sound = *it; + ++it; + + if(sound->isBuffered) + stop(sound); + } + alcProcessContext(m_context); + + while(!m_bufferedFactories->empty()) + { + alDeleteBuffers(1, + &(*(m_bufferedFactories->begin()))->buffer); + delete *m_bufferedFactories->begin(); + m_bufferedFactories->erase(m_bufferedFactories->begin()); + } + unlock(); + } + + return true; +}*/ + +/******************************************************************************/ +/**************************** 3D Device Code **********************************/ +/******************************************************************************/ + +AUD_Vector3 AUD_OpenALDevice::getListenerLocation() const +{ + ALfloat p[3]; + alGetListenerfv(AL_POSITION, p); + return AUD_Vector3(p[0], p[1], p[2]); +} + +void AUD_OpenALDevice::setListenerLocation(const AUD_Vector3& location) +{ + alListenerfv(AL_POSITION, (ALfloat*)location.get()); +} + +AUD_Vector3 AUD_OpenALDevice::getListenerVelocity() const +{ + ALfloat v[3]; + alGetListenerfv(AL_VELOCITY, v); + return AUD_Vector3(v[0], v[1], v[2]); +} + +void AUD_OpenALDevice::setListenerVelocity(const AUD_Vector3& velocity) +{ + alListenerfv(AL_VELOCITY, (ALfloat*)velocity.get()); +} + +AUD_Quaternion AUD_OpenALDevice::getListenerOrientation() const +{ + // AUD_XXX not implemented yet + return AUD_Quaternion(0, 0, 0, 0); +} + +void AUD_OpenALDevice::setListenerOrientation(const AUD_Quaternion& orientation) +{ + ALfloat direction[6]; + direction[0] = -2 * (orientation.w() * orientation.y() + + orientation.x() * orientation.z()); + direction[1] = 2 * (orientation.x() * orientation.w() - + orientation.z() * orientation.y()); + direction[2] = 2 * (orientation.x() * orientation.x() + + orientation.y() * orientation.y()) - 1; + direction[3] = 2 * (orientation.x() * orientation.y() - + orientation.w() * orientation.z()); + direction[4] = 1 - 2 * (orientation.x() * orientation.x() + + orientation.z() * orientation.z()); + direction[5] = 2 * (orientation.w() * orientation.x() + + orientation.y() * orientation.z()); + alListenerfv(AL_ORIENTATION, direction); +} + +float AUD_OpenALDevice::getSpeedOfSound() const +{ + return alGetFloat(AL_SPEED_OF_SOUND); +} + +void AUD_OpenALDevice::setSpeedOfSound(float speed) +{ + alSpeedOfSound(speed); +} + +float AUD_OpenALDevice::getDopplerFactor() const +{ + return alGetFloat(AL_DOPPLER_FACTOR); +} + +void AUD_OpenALDevice::setDopplerFactor(float factor) +{ + alDopplerFactor(factor); +} + +AUD_DistanceModel AUD_OpenALDevice::getDistanceModel() const +{ + switch(alGetInteger(AL_DISTANCE_MODEL)) + { + case AL_INVERSE_DISTANCE: + return AUD_DISTANCE_MODEL_INVERSE; + case AL_INVERSE_DISTANCE_CLAMPED: + return AUD_DISTANCE_MODEL_INVERSE_CLAMPED; + case AL_LINEAR_DISTANCE: + return AUD_DISTANCE_MODEL_LINEAR; + case AL_LINEAR_DISTANCE_CLAMPED: + return AUD_DISTANCE_MODEL_LINEAR_CLAMPED; + case AL_EXPONENT_DISTANCE: + return AUD_DISTANCE_MODEL_EXPONENT; + case AL_EXPONENT_DISTANCE_CLAMPED: + return AUD_DISTANCE_MODEL_EXPONENT_CLAMPED; + default: + return AUD_DISTANCE_MODEL_INVALID; + } +} + +void AUD_OpenALDevice::setDistanceModel(AUD_DistanceModel model) +{ + switch(model) + { + case AUD_DISTANCE_MODEL_INVERSE: + alDistanceModel(AL_INVERSE_DISTANCE); + break; + case AUD_DISTANCE_MODEL_INVERSE_CLAMPED: + alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); + break; + case AUD_DISTANCE_MODEL_LINEAR: + alDistanceModel(AL_LINEAR_DISTANCE); + break; + case AUD_DISTANCE_MODEL_LINEAR_CLAMPED: + alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); + break; + case AUD_DISTANCE_MODEL_EXPONENT: + alDistanceModel(AL_EXPONENT_DISTANCE); + break; + case AUD_DISTANCE_MODEL_EXPONENT_CLAMPED: + alDistanceModel(AL_EXPONENT_DISTANCE_CLAMPED); + break; + default: + alDistanceModel(AL_NONE); + } +} + +AUD_Vector3 AUD_OpenALDevice::getSourceLocation(AUD_Handle* handle) +{ + AUD_Vector3 result = AUD_Vector3(0, 0, 0); + ALfloat p[3]; + lock(); + + if(isValid(handle)) + { + alGetSourcefv(((AUD_OpenALHandle*)handle)->source, AL_POSITION, p); + result = AUD_Vector3(p[0], p[1], p[2]); + } + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setSourceLocation(AUD_Handle* handle, const AUD_Vector3& location) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcefv(((AUD_OpenALHandle*)handle)->source, AL_POSITION, + (ALfloat*)location.get()); + + unlock(); + return result; +} + +AUD_Vector3 AUD_OpenALDevice::getSourceVelocity(AUD_Handle* handle) +{ + AUD_Vector3 result = AUD_Vector3(0, 0, 0); + ALfloat v[3]; + lock(); + + if(isValid(handle)) + { + alGetSourcefv(((AUD_OpenALHandle*)handle)->source, AL_VELOCITY, v); + result = AUD_Vector3(v[0], v[1], v[2]); + } + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setSourceVelocity(AUD_Handle* handle, const AUD_Vector3& velocity) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcefv(((AUD_OpenALHandle*)handle)->source, AL_VELOCITY, + (ALfloat*)velocity.get()); + + unlock(); + return result; +} + +AUD_Quaternion AUD_OpenALDevice::getSourceOrientation(AUD_Handle* handle) +{ + // AUD_XXX not implemented yet + return AUD_Quaternion(0, 0, 0, 0); +} + +bool AUD_OpenALDevice::setSourceOrientation(AUD_Handle* handle, const AUD_Quaternion& orientation) +{ + lock(); + bool result = isValid(handle); + + if(result) + { + ALfloat direction[3]; + direction[0] = -2 * (orientation.w() * orientation.y() + + orientation.x() * orientation.z()); + direction[1] = 2 * (orientation.x() * orientation.w() - + orientation.z() * orientation.y()); + direction[2] = 2 * (orientation.x() * orientation.x() + + orientation.y() * orientation.y()) - 1; + alSourcefv(((AUD_OpenALHandle*)handle)->source, AL_DIRECTION, + direction); + } + + unlock(); + return result; +} + +bool AUD_OpenALDevice::isRelative(AUD_Handle* handle) +{ + int result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcei(((AUD_OpenALHandle*)handle)->source, AL_SOURCE_RELATIVE, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setRelative(AUD_Handle* handle, bool relative) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcei(((AUD_OpenALHandle*)handle)->source, AL_SOURCE_RELATIVE, + relative); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getVolumeMaximum(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_MAX_GAIN, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setVolumeMaximum(AUD_Handle* handle, float volume) +{ + lock(); + bool result = isValid(handle); + + if(result) + + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_MAX_GAIN, + volume); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getVolumeMinimum(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_MIN_GAIN, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setVolumeMinimum(AUD_Handle* handle, float volume) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_MIN_GAIN, + volume); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getDistanceMaximum(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_MAX_DISTANCE, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setDistanceMaximum(AUD_Handle* handle, float distance) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_MAX_DISTANCE, + distance); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getDistanceReference(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_REFERENCE_DISTANCE, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setDistanceReference(AUD_Handle* handle, float distance) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_REFERENCE_DISTANCE, + distance); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getAttenuation(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_ROLLOFF_FACTOR, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setAttenuation(AUD_Handle* handle, float factor) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_ROLLOFF_FACTOR, + factor); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getConeAngleOuter(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_CONE_OUTER_ANGLE, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setConeAngleOuter(AUD_Handle* handle, float angle) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_CONE_OUTER_ANGLE, + angle); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getConeAngleInner(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_CONE_INNER_ANGLE, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setConeAngleInner(AUD_Handle* handle, float angle) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_CONE_INNER_ANGLE, + angle); + + unlock(); + return result; +} + +float AUD_OpenALDevice::getConeVolumeOuter(AUD_Handle* handle) +{ + float result = std::numeric_limits::quiet_NaN();; + + lock(); + + if(isValid(handle)) + alGetSourcef(((AUD_OpenALHandle*)handle)->source, AL_CONE_OUTER_GAIN, + &result); + + unlock(); + return result; +} + +bool AUD_OpenALDevice::setConeVolumeOuter(AUD_Handle* handle, float volume) +{ + lock(); + bool result = isValid(handle); + + if(result) + alSourcef(((AUD_OpenALHandle*)handle)->source, AL_CONE_OUTER_GAIN, + volume); + + unlock(); + return result; +} diff --git a/intern/audaspace/OpenAL/AUD_OpenALDevice.h b/intern/audaspace/OpenAL/AUD_OpenALDevice.h new file mode 100644 index 00000000000..985954fc20b --- /dev/null +++ b/intern/audaspace/OpenAL/AUD_OpenALDevice.h @@ -0,0 +1,203 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_OPENALDEVICE +#define AUD_OPENALDEVICE + +#include "AUD_IDevice.h" +#include "AUD_I3DDevice.h" +struct AUD_OpenALHandle; +struct AUD_OpenALBufferedFactory; + +#include +#include +#include +#include + +/** + * This device plays through OpenAL. + */ +class AUD_OpenALDevice : public AUD_IDevice, public AUD_I3DDevice +{ +private: + /** + * The OpenAL device handle. + */ + ALCdevice* m_device; + + /** + * The OpenAL context. + */ + ALCcontext* m_context; + + /** + * The specification of the device. + */ + AUD_DeviceSpecs m_specs; + + /** + * Whether the device has the AL_EXT_MCFORMATS extension. + */ + bool m_useMC; + + /** + * The list of sounds that are currently playing. + */ + std::list* m_playingSounds; + + /** + * The list of sounds that are currently paused. + */ + std::list* m_pausedSounds; + + /** + * The list of buffered factories. + */ + std::list* m_bufferedFactories; + + /** + * The mutex for locking. + */ + pthread_mutex_t m_mutex; + + /** + * The streaming thread. + */ + pthread_t m_thread; + + /** + * The condition for streaming thread wakeup. + */ + bool m_playing; + + /** + * Buffer size. + */ + int m_buffersize; + + /** + * Starts the streaming thread. + */ + void start(); + + /** + * Checks if a handle is valid. + * \param handle The handle to check. + * \return Whether the handle is valid. + */ + bool isValid(AUD_Handle* handle); + + /** + * Gets the format according to the specs. + * \param format The variable to put the format into. + * \param specs The specs to read the channel count from. + * \return Whether the format is valid or not. + */ + bool getFormat(ALenum &format, AUD_Specs specs); + + // hide copy constructor and operator= + AUD_OpenALDevice(const AUD_OpenALDevice&); + AUD_OpenALDevice& operator=(const AUD_OpenALDevice&); + +public: + /** + * Opens the OpenAL audio device for playback. + * \param specs The wanted audio specification. + * \param buffersize The size of the internal buffer. + * \note The specification really used for opening the device may differ. + * \note The buffersize will be multiplicated by three for this device. + * \exception AUD_Exception Thrown if the audio device cannot be opened. + */ + AUD_OpenALDevice(AUD_DeviceSpecs specs, + int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Streaming thread main function. + */ + void updateStreams(); + + virtual ~AUD_OpenALDevice(); + + virtual AUD_DeviceSpecs getSpecs() const; + virtual AUD_Handle* play(AUD_IFactory* factory, bool keep = false); + virtual bool pause(AUD_Handle* handle); + virtual bool resume(AUD_Handle* handle); + virtual bool stop(AUD_Handle* handle); + virtual bool getKeep(AUD_Handle* handle); + virtual bool setKeep(AUD_Handle* handle, bool keep); + virtual bool seek(AUD_Handle* handle, float position); + virtual float getPosition(AUD_Handle* handle); + virtual AUD_Status getStatus(AUD_Handle* handle); + virtual void lock(); + virtual void unlock(); + virtual float getVolume() const; + virtual void setVolume(float volume); + virtual float getVolume(AUD_Handle* handle); + virtual bool setVolume(AUD_Handle* handle, float volume); + virtual float getPitch(AUD_Handle* handle); + virtual bool setPitch(AUD_Handle* handle, float pitch); + virtual int getLoopCount(AUD_Handle* handle); + virtual bool setLoopCount(AUD_Handle* handle, int count); + virtual bool setStopCallback(AUD_Handle* handle, stopCallback callback = NULL, void* data = NULL); + + virtual AUD_Vector3 getListenerLocation() const; + virtual void setListenerLocation(const AUD_Vector3& location); + virtual AUD_Vector3 getListenerVelocity() const; + virtual void setListenerVelocity(const AUD_Vector3& velocity); + virtual AUD_Quaternion getListenerOrientation() const; + virtual void setListenerOrientation(const AUD_Quaternion& orientation); + virtual float getSpeedOfSound() const; + virtual void setSpeedOfSound(float speed); + virtual float getDopplerFactor() const; + virtual void setDopplerFactor(float factor); + virtual AUD_DistanceModel getDistanceModel() const; + virtual void setDistanceModel(AUD_DistanceModel model); + virtual AUD_Vector3 getSourceLocation(AUD_Handle* handle); + virtual bool setSourceLocation(AUD_Handle* handle, const AUD_Vector3& location); + virtual AUD_Vector3 getSourceVelocity(AUD_Handle* handle); + virtual bool setSourceVelocity(AUD_Handle* handle, const AUD_Vector3& velocity); + virtual AUD_Quaternion getSourceOrientation(AUD_Handle* handle); + virtual bool setSourceOrientation(AUD_Handle* handle, const AUD_Quaternion& orientation); + virtual bool isRelative(AUD_Handle* handle); + virtual bool setRelative(AUD_Handle* handle, bool relative); + virtual float getVolumeMaximum(AUD_Handle* handle); + virtual bool setVolumeMaximum(AUD_Handle* handle, float volume); + virtual float getVolumeMinimum(AUD_Handle* handle); + virtual bool setVolumeMinimum(AUD_Handle* handle, float volume); + virtual float getDistanceMaximum(AUD_Handle* handle); + virtual bool setDistanceMaximum(AUD_Handle* handle, float distance); + virtual float getDistanceReference(AUD_Handle* handle); + virtual bool setDistanceReference(AUD_Handle* handle, float distance); + virtual float getAttenuation(AUD_Handle* handle); + virtual bool setAttenuation(AUD_Handle* handle, float factor); + virtual float getConeAngleOuter(AUD_Handle* handle); + virtual bool setConeAngleOuter(AUD_Handle* handle, float angle); + virtual float getConeAngleInner(AUD_Handle* handle); + virtual bool setConeAngleInner(AUD_Handle* handle, float angle); + virtual float getConeVolumeOuter(AUD_Handle* handle); + virtual bool setConeVolumeOuter(AUD_Handle* handle, float volume); +}; + +#endif //AUD_OPENALDEVICE diff --git a/intern/audaspace/OpenAL/Makefile b/intern/audaspace/OpenAL/Makefile new file mode 100644 index 00000000000..4cf9f66b06c --- /dev/null +++ b/intern/audaspace/OpenAL/Makefile @@ -0,0 +1,39 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_openal +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I../intern +CPPFLAGS += -I. diff --git a/intern/audaspace/Python/AUD_PyAPI.cpp b/intern/audaspace/Python/AUD_PyAPI.cpp new file mode 100644 index 00000000000..b0c55d5856e --- /dev/null +++ b/intern/audaspace/Python/AUD_PyAPI.cpp @@ -0,0 +1,2973 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_PyAPI.h" +#include "structmember.h" + +#include "AUD_I3DDevice.h" +#include "AUD_NULLDevice.h" +#include "AUD_DelayFactory.h" +#include "AUD_DoubleFactory.h" +#include "AUD_FaderFactory.h" +#include "AUD_HighpassFactory.h" +#include "AUD_LimiterFactory.h" +#include "AUD_LoopFactory.h" +#include "AUD_LowpassFactory.h" +#include "AUD_PingPongFactory.h" +#include "AUD_PitchFactory.h" +#include "AUD_ReverseFactory.h" +#include "AUD_SinusFactory.h" +#include "AUD_FileFactory.h" +#include "AUD_SquareFactory.h" +#include "AUD_StreamBufferFactory.h" +#include "AUD_SuperposeFactory.h" +#include "AUD_VolumeFactory.h" +#include "AUD_IIRFilterFactory.h" + +#ifdef WITH_SDL +#include "AUD_SDLDevice.h" +#endif + +#ifdef WITH_OPENAL +#include "AUD_OpenALDevice.h" +#endif + +#ifdef WITH_JACK +#include "AUD_JackDevice.h" +#endif + +// ==================================================================== + +typedef enum +{ + AUD_DEVICE_NULL = 0, + AUD_DEVICE_OPENAL, + AUD_DEVICE_SDL, + AUD_DEVICE_JACK, + AUD_DEVICE_READ, +} AUD_DeviceTypes; + +// ==================================================================== + +#define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, #name, name) + +// ==================================================================== + +static PyObject* AUDError; + +static const char* device_not_3d_error = "Device is not a 3D device!"; + +// ==================================================================== + +static void +Factory_dealloc(Factory* self) +{ + if(self->factory) + delete self->factory; + Py_XDECREF(self->child_list); + Py_TYPE(self)->tp_free((PyObject*)self); +} + +static PyObject * +Factory_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + Factory *self; + + self = (Factory*)type->tp_alloc(type, 0); + if(self != NULL) + { + static const char *kwlist[] = {"filename", NULL}; + const char* filename = NULL; + + if(!PyArg_ParseTupleAndKeywords(args, kwds, "s:Factory", const_cast(kwlist), &filename)) + { + Py_DECREF(self); + return NULL; + } + + try + { + self->factory = new AUD_FileFactory(filename); + } + catch(AUD_Exception& e) + { + Py_DECREF(self); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)self; +} + +PyDoc_STRVAR(M_aud_Factory_sine_doc, + "sine(frequency, rate=44100)\n\n" + "Creates a sine factory which plays a sine wave.\n\n" + ":arg frequency: The frequency of the sine wave in Hz.\n" + ":type frequency: float\n" + ":arg rate: The sampling rate in Hz. It's recommended to set this " + "value to the playback device's samling rate to avoid resamping.\n" + ":type rate: int\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_sine(PyTypeObject* type, PyObject* args) +{ + float frequency; + int rate = 44100; + + if(!PyArg_ParseTuple(args, "f|i:sine", &frequency, &rate)) + return NULL; + + Factory *self; + + self = (Factory*)type->tp_alloc(type, 0); + if(self != NULL) + { + try + { + self->factory = new AUD_SinusFactory(frequency, (AUD_SampleRate)rate); + } + catch(AUD_Exception& e) + { + Py_DECREF(self); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)self; +} + +PyDoc_STRVAR(M_aud_Factory_file_doc, + "file(filename)\n\n" + "Creates a factory object of a sound file.\n\n" + ":arg filename: Path of the file.\n" + ":type filename: string\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. warning:: If the file doesn't exist or can't be read you will " + "not get an exception immediately, but when you try to start " + "playback of that factory."); + +static PyObject * +Factory_file(PyTypeObject* type, PyObject* args) +{ + const char* filename = NULL; + + if(!PyArg_ParseTuple(args, "s:file", &filename)) + return NULL; + + Factory *self; + + self = (Factory*)type->tp_alloc(type, 0); + if(self != NULL) + { + try + { + self->factory = new AUD_FileFactory(filename); + } + catch(AUD_Exception& e) + { + Py_DECREF(self); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)self; +} + +PyDoc_STRVAR(M_aud_Factory_lowpass_doc, + "lowpass(frequency, Q=0.5)\n\n" + "Creates a second order lowpass filter based on the transfer " + "function H(s) = 1 / (s^2 + s/Q + 1)\n\n" + ":arg frequency: The cut off trequency of the lowpass.\n" + ":type frequency: float\n" + ":arg Q: Q factor of the lowpass.\n" + ":type Q: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_lowpass(Factory* self, PyObject* args) +{ + float frequency; + float Q = 0.5; + + if(!PyArg_ParseTuple(args, "f|f:lowpass", &frequency, &Q)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_LowpassFactory(self->factory, frequency, Q); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_delay_doc, + "delay(time)\n\n" + "Delays by playing adding silence in front of the other factory's " + "data.\n\n" + ":arg time: How many seconds of silence should be added before " + "the factory.\n" + ":type time: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_delay(Factory* self, PyObject* args) +{ + float delay; + + if(!PyArg_ParseTuple(args, "f:delay", &delay)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_DelayFactory(self->factory, delay); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_join_doc, + "join(factory)\n\n" + "Plays two factories in sequence.\n\n" + ":arg factory: The factory to play second.\n" + ":type factory: :class:`Factory`\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: The two factories have to have the same specifications " + "(channels and samplerate)."); + +static PyObject * +Factory_join(Factory* self, PyObject* object) +{ + PyTypeObject* type = ((PyObject*)self)->ob_type; + + if(!PyObject_TypeCheck(object, type)) + { + PyErr_SetString(PyExc_TypeError, "Object has to be of type Factory!"); + return NULL; + } + + Factory *parent; + Factory *child = (Factory*)object; + + parent = (Factory*)type->tp_alloc(type, 0); + if(parent != NULL) + { + parent->child_list = Py_BuildValue("(OO)", self, object); + + try + { + parent->factory = new AUD_DoubleFactory(self->factory, child->factory); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_highpass_doc, + "highpass(frequency, Q=0.5)\n\n" + "Creates a second order highpass filter based on the transfer " + "function H(s) = s^2 / (s^2 + s/Q + 1)\n\n" + ":arg frequency: The cut off trequency of the highpass.\n" + ":type frequency: float\n" + ":arg Q: Q factor of the lowpass.\n" + ":type Q: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_highpass(Factory* self, PyObject* args) +{ + float frequency; + float Q = 0.5; + + if(!PyArg_ParseTuple(args, "f|f:highpass", &frequency, &Q)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_HighpassFactory(self->factory, frequency, Q); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_limit_doc, + "limit(start, end)\n\n" + "Limits a factory within a specific start and end time.\n\n" + ":arg start: Start time in seconds.\n" + ":type start: float\n" + ":arg end: End time in seconds.\n" + ":type end: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_limit(Factory* self, PyObject* args) +{ + float start, end; + + if(!PyArg_ParseTuple(args, "ff:limit", &start, &end)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_LimiterFactory(self->factory, start, end); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_pitch_doc, + "pitch(factor)\n\n" + "Changes the pitch of a factory with a specific factor.\n\n" + ":arg factor: The factor to change the pitch with.\n" + ":type factor: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: This is done by changing the sample rate of the " + "underlying factory, which has to be an integer, so the factor " + "value rounded and the factor may not be 100 % accurate.\n\n" + ".. note:: This is a filter function, you might consider using " + ":attr:`Handle.pitch` instead."); + +static PyObject * +Factory_pitch(Factory* self, PyObject* args) +{ + float factor; + + if(!PyArg_ParseTuple(args, "f:pitch", &factor)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_PitchFactory(self->factory, factor); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_volume_doc, + "volume(volume)\n\n" + "Changes the volume of a factory.\n\n" + ":arg volume: The new volume..\n" + ":type volume: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: Should be in the range [0, 1] to avoid clipping.\n\n" + ".. note:: This is a filter function, you might consider using " + ":attr:`Handle.volume` instead."); + +static PyObject * +Factory_volume(Factory* self, PyObject* args) +{ + float volume; + + if(!PyArg_ParseTuple(args, "f:volume", &volume)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_VolumeFactory(self->factory, volume); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_fadein_doc, + "fadein(start, length)\n\n" + "Fades a factory in by raising the volume linearly in the given " + "time interval.\n\n" + ":arg start: Time in seconds when the fading should start.\n" + ":type start: float\n" + ":arg length: Time in seconds how long the fading should last.\n" + ":type length: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: Before the fade starts it plays silence."); + +static PyObject * +Factory_fadein(Factory* self, PyObject* args) +{ + float start, length; + + if(!PyArg_ParseTuple(args, "ff:fadein", &start, &length)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_FaderFactory(self->factory, AUD_FADE_IN, start, length); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_fadeout_doc, + "fadeout(start, length)\n\n" + "Fades a factory in by lowering the volume linearly in the given " + "time interval.\n\n" + ":arg start: Time in seconds when the fading should start.\n" + ":type start: float\n" + ":arg length: Time in seconds how long the fading should last.\n" + ":type length: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: After the fade this factory plays silence, so that " + "the length of the factory is not altered."); + +static PyObject * +Factory_fadeout(Factory* self, PyObject* args) +{ + float start, length; + + if(!PyArg_ParseTuple(args, "ff:fadeout", &start, &length)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_FaderFactory(self->factory, AUD_FADE_OUT, start, length); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_loop_doc, + "loop(count)\n\n" + "Loops a factory.\n\n" + ":arg count: How often the factory should be looped. " + "Negative values mean endlessly.\n" + ":type count: integer\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: This is a filter function, you might consider using " + ":attr:`Handle.loop_count` instead."); + +static PyObject * +Factory_loop(Factory* self, PyObject* args) +{ + int loop; + + if(!PyArg_ParseTuple(args, "i:loop", &loop)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_LoopFactory(self->factory, loop); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_mix_doc, + "mix(factory)\n\n" + "Mixes two factories.\n\n" + ":arg factory: The factory to mix over the other.\n" + ":type factory: :class:`Factory`\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: The two factories have to have the same specifications " + "(channels and samplerate)."); + +static PyObject * +Factory_mix(Factory* self, PyObject* object) +{ + PyTypeObject* type = ((PyObject*)self)->ob_type; + + if(!PyObject_TypeCheck(object, type)) + { + PyErr_SetString(PyExc_TypeError, "Object is not of type Factory!"); + return NULL; + } + + Factory *parent = (Factory*)type->tp_alloc(type, 0); + Factory *child = (Factory*)object; + + if(parent != NULL) + { + parent->child_list = Py_BuildValue("(OO)", self, object); + + try + { + parent->factory = new AUD_SuperposeFactory(self->factory, child->factory); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_pingpong_doc, + "pingpong()\n\n" + "Plays a factory forward and then backward.\n" + "This is like joining a factory with its reverse.\n\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_pingpong(Factory* self) +{ + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_PingPongFactory(self->factory); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_reverse_doc, + "reverse()\n\n" + "Plays a factory reversed.\n\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: The factory has to have a finite length and has to be " + "seekable. It's recommended to use this only with factories with " + "fast and accurate seeking, which is not true for encoded audio " + "files, such ones should be buffered using :meth:`buffer` before " + "being played reversed.\n\n" + ".. warning:: If seeking is not accurate in the underlying factory " + "you'll likely hear skips/jumps/cracks."); + +static PyObject * +Factory_reverse(Factory* self) +{ + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_ReverseFactory(self->factory); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_buffer_doc, + "buffer()\n\n" + "Buffers a factory into RAM.\n" + "This saves CPU usage needed for decoding and file access if the " + "underlying factory reads from a file on the harddisk, but it " + "consumes a lot of memory.\n\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`\n\n" + ".. note:: Only known-length factories can be buffered.\n\n" + ".. warning:: Raw PCM data needs a lot of space, only buffer " + "short factories."); + +static PyObject * +Factory_buffer(Factory* self) +{ + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + try + { + parent->factory = new AUD_StreamBufferFactory(self->factory); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_square_doc, + "square(threshold = 0)\n\n" + "Makes a square wave out of an audio wave by setting all samples " + "with a amplitude >= threshold to 1, all <= -threshold to -1 and " + "all between to 0.\n\n" + ":arg threshold: Threshold value over which an amplitude counts " + "non-zero.\n" + ":type threshold: float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_square(Factory* self, PyObject* args) +{ + float threshold = 0; + + if(!PyArg_ParseTuple(args, "|f:square", &threshold)) + return NULL; + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_SquareFactory(self->factory, threshold); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +PyDoc_STRVAR(M_aud_Factory_filter_doc, + "filter(b, a = (1))\n\n" + "Filters a factory with the supplied IIR filter coefficients.\n" + "Without the second parameter you'll get a FIR filter.\n" + "If the first value of the a sequence is 0 it will be set to 1 " + "automatically.\n" + "If the first value of the a sequence is neither 0 nor 1, all " + "filter coefficients will be scaled by this value so that it is 1 " + "in the end, you don't have to scale yourself.\n\n" + ":arg b: The nominator filter coefficients.\n" + ":type b: sequence of float\n" + ":arg a: The denominator filter coefficients.\n" + ":type a: sequence of float\n" + ":return: The created :class:`Factory` object.\n" + ":rtype: :class:`Factory`"); + +static PyObject * +Factory_filter(Factory* self, PyObject* args) +{ + PyObject* py_b; + PyObject* py_a = NULL; + + if(!PyArg_ParseTuple(args, "O|O:filter", &py_b, &py_a)) + return NULL; + + if(!PySequence_Check(py_b) || (py_a != NULL && !PySequence_Check(py_a))) + { + PyErr_SetString(PyExc_TypeError, "Parameter is not a sequence!"); + return NULL; + } + + if(!PySequence_Length(py_b) || (py_a != NULL && !PySequence_Length(py_a))) + { + PyErr_SetString(PyExc_ValueError, "The sequence has to contain at least one value!"); + return NULL; + } + + std::vector a, b; + PyObject* py_value; + float value; + int result; + + for(int i = 0; i < PySequence_Length(py_b); i++) + { + py_value = PySequence_GetItem(py_b, i); + result = PyArg_Parse(py_value, "f:filter", &value); + Py_DECREF(py_value); + + if(!result) + return NULL; + + b.push_back(value); + } + + if(py_a) + { + for(int i = 0; i < PySequence_Length(py_a); i++) + { + py_value = PySequence_GetItem(py_a, i); + result = PyArg_Parse(py_value, "f:filter", &value); + Py_DECREF(py_value); + + if(!result) + return NULL; + + a.push_back(value); + } + + if(a[0] == 0) + a[0] = 1; + } + else + a.push_back(1); + + PyTypeObject* type = ((PyObject*)self)->ob_type; + Factory *parent = (Factory*)type->tp_alloc(type, 0); + + if(parent != NULL) + { + Py_INCREF(self); + parent->child_list = (PyObject*)self; + + try + { + parent->factory = new AUD_IIRFilterFactory(self->factory, b, a); + } + catch(AUD_Exception& e) + { + Py_DECREF(parent); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)parent; +} + +static PyMethodDef Factory_methods[] = { + {"sine", (PyCFunction)Factory_sine, METH_VARARGS | METH_CLASS, + M_aud_Factory_sine_doc + }, + {"file", (PyCFunction)Factory_file, METH_VARARGS | METH_CLASS, + M_aud_Factory_file_doc + }, + {"lowpass", (PyCFunction)Factory_lowpass, METH_VARARGS, + M_aud_Factory_lowpass_doc + }, + {"delay", (PyCFunction)Factory_delay, METH_VARARGS, + M_aud_Factory_delay_doc + }, + {"join", (PyCFunction)Factory_join, METH_O, + M_aud_Factory_join_doc + }, + {"highpass", (PyCFunction)Factory_highpass, METH_VARARGS, + M_aud_Factory_highpass_doc + }, + {"limit", (PyCFunction)Factory_limit, METH_VARARGS, + M_aud_Factory_limit_doc + }, + {"pitch", (PyCFunction)Factory_pitch, METH_VARARGS, + M_aud_Factory_pitch_doc + }, + {"volume", (PyCFunction)Factory_volume, METH_VARARGS, + M_aud_Factory_volume_doc + }, + {"fadein", (PyCFunction)Factory_fadein, METH_VARARGS, + M_aud_Factory_fadein_doc + }, + {"fadeout", (PyCFunction)Factory_fadeout, METH_VARARGS, + M_aud_Factory_fadeout_doc + }, + {"loop", (PyCFunction)Factory_loop, METH_VARARGS, + M_aud_Factory_loop_doc + }, + {"mix", (PyCFunction)Factory_mix, METH_O, + M_aud_Factory_mix_doc + }, + {"pingpong", (PyCFunction)Factory_pingpong, METH_NOARGS, + M_aud_Factory_pingpong_doc + }, + {"reverse", (PyCFunction)Factory_reverse, METH_NOARGS, + M_aud_Factory_reverse_doc + }, + {"buffer", (PyCFunction)Factory_buffer, METH_NOARGS, + M_aud_Factory_buffer_doc + }, + {"square", (PyCFunction)Factory_square, METH_VARARGS, + M_aud_Factory_square_doc + }, + {"filter", (PyCFunction)Factory_filter, METH_VARARGS, + M_aud_Factory_filter_doc + }, + {NULL} /* Sentinel */ +}; + +PyDoc_STRVAR(M_aud_Factory_doc, + "Factory objects are immutable and represent a sound that can be " + "played simultaneously multiple times. They are called factories " + "because they create reader objects internally that are used for " + "playback."); + +static PyTypeObject FactoryType = { + PyVarObject_HEAD_INIT(NULL, 0) + "aud.Factory", /* tp_name */ + sizeof(Factory), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)Factory_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + M_aud_Factory_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Factory_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + Factory_new, /* tp_new */ +}; + +// ========== Handle ================================================== + +static void +Handle_dealloc(Handle* self) +{ + Py_XDECREF(self->device); + Py_TYPE(self)->tp_free((PyObject*)self); +} + +PyDoc_STRVAR(M_aud_Handle_pause_doc, + "pause()\n\n" + "Pauses playback.\n\n" + ":return: Whether the action succeeded.\n" + ":rtype: bool"); + +static PyObject * +Handle_pause(Handle *self) +{ + Device* device = (Device*)self->device; + + try + { + if(device->device->pause(self->handle)) + { + Py_RETURN_TRUE; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } + + Py_RETURN_FALSE; +} + +PyDoc_STRVAR(M_aud_Handle_resume_doc, + "resume()\n\n" + "Resumes playback.\n\n" + ":return: Whether the action succeeded.\n" + ":rtype: bool"); + +static PyObject * +Handle_resume(Handle *self) +{ + Device* device = (Device*)self->device; + + try + { + if(device->device->resume(self->handle)) + { + Py_RETURN_TRUE; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } + + Py_RETURN_FALSE; +} + +PyDoc_STRVAR(M_aud_Handle_stop_doc, + "stop()\n\n" + "Stops playback.\n\n" + ":return: Whether the action succeeded.\n" + ":rtype: bool\n\n" + ".. note:: This makes the handle invalid."); + +static PyObject * +Handle_stop(Handle *self) +{ + Device* device = (Device*)self->device; + + try + { + if(device->device->stop(self->handle)) + { + Py_RETURN_TRUE; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } + + Py_RETURN_FALSE; +} + +static PyMethodDef Handle_methods[] = { + {"pause", (PyCFunction)Handle_pause, METH_NOARGS, + M_aud_Handle_pause_doc + }, + {"resume", (PyCFunction)Handle_resume, METH_NOARGS, + M_aud_Handle_resume_doc + }, + {"stop", (PyCFunction)Handle_stop, METH_NOARGS, + M_aud_Handle_stop_doc + }, + {NULL} /* Sentinel */ +}; + +PyDoc_STRVAR(M_aud_Handle_position_doc, + "The playback position of the sound in seconds."); + +static PyObject * +Handle_get_position(Handle *self, void* nothing) +{ + Device* device = (Device*)self->device; + + try + { + return Py_BuildValue("f", device->device->getPosition(self->handle)); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_position(Handle *self, PyObject* args, void* nothing) +{ + float position; + + if(!PyArg_Parse(args, "f:position", &position)) + return -1; + + Device* device = (Device*)self->device; + + try + { + if(device->device->seek(self->handle, position)) + return 0; + PyErr_SetString(AUDError, "Couldn't seek the sound!"); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_keep_doc, + "Whether the sound should be kept paused in the device when its " + "end is reached.\n" + "This can be used to seek the sound to some position and start " + "playback again.\n\n" + ".. warning:: If this is set to true and you forget stopping this " + "equals a memory leak as the handle exists until the device is " + "destroyed."); + +static PyObject * +Handle_get_keep(Handle *self, void* nothing) +{ + Device* device = (Device*)self->device; + + try + { + if(device->device->getKeep(self->handle)) + { + Py_RETURN_TRUE; + } + else + { + Py_RETURN_FALSE; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_keep(Handle *self, PyObject* args, void* nothing) +{ + if(!PyBool_Check(args)) + { + PyErr_SetString(PyExc_TypeError, "keep is not a boolean!"); + return -1; + } + + bool keep = args == Py_True; + Device* device = (Device*)self->device; + + try + { + if(device->device->setKeep(self->handle, keep)) + return 0; + PyErr_SetString(AUDError, "Couldn't set keep of the sound!"); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_status_doc, + "Whether the sound is playing, paused or stopped (=invalid)."); + +static PyObject * +Handle_get_status(Handle *self, void* nothing) +{ + Device* device = (Device*)self->device; + + try + { + return Py_BuildValue("i", device->device->getStatus(self->handle)); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +PyDoc_STRVAR(M_aud_Handle_volume_doc, + "The volume of the sound."); + +static PyObject * +Handle_get_volume(Handle *self, void* nothing) +{ + Device* device = (Device*)self->device; + + try + { + return Py_BuildValue("f", device->device->getVolume(self->handle)); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_volume(Handle *self, PyObject* args, void* nothing) +{ + float volume; + + if(!PyArg_Parse(args, "f:volume", &volume)) + return -1; + + Device* device = (Device*)self->device; + + try + { + if(device->device->setVolume(self->handle, volume)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the sound volume!"); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_pitch_doc, + "The pitch of the sound."); + +static PyObject * +Handle_get_pitch(Handle *self, void* nothing) +{ + Device* device = (Device*)self->device; + + try + { + return Py_BuildValue("f", device->device->getPitch(self->handle)); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_pitch(Handle *self, PyObject* args, void* nothing) +{ + float pitch; + + if(!PyArg_Parse(args, "f:pitch", &pitch)) + return -1; + + Device* device = (Device*)self->device; + + try + { + if(device->device->setPitch(self->handle, pitch)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the sound pitch!"); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_loop_count_doc, + "The (remaining) loop count of the sound. A negative value indicates infinity."); + +static PyObject * +Handle_get_loop_count(Handle *self, void* nothing) +{ + Device* device = (Device*)self->device; + + try + { + return Py_BuildValue("i", device->device->getLoopCount(self->handle)); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_loop_count(Handle *self, PyObject* args, void* nothing) +{ + int loops; + + if(!PyArg_Parse(args, "i:loop_count", &loops)) + return -1; + + Device* device = (Device*)self->device; + + try + { + if(device->device->setLoopCount(self->handle, loops)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the loop count!"); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_location_doc, + "The source's location in 3D space, a 3D tuple of floats."); + +static PyObject * +Handle_get_location(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + AUD_Vector3 v = device->getSourceLocation(self->handle); + return Py_BuildValue("(fff)", v.x(), v.y(), v.z()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Handle_set_location(Handle *self, PyObject* args, void* nothing) +{ + float x, y, z; + + if(!PyArg_Parse(args, "(fff):location", &x, &y, &z)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + AUD_Vector3 location(x, y, z); + if(device->setSourceLocation(self->handle, location)) + return 0; + PyErr_SetString(AUDError, "Location couldn't be set!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_velocity_doc, + "The source's velocity in 3D space, a 3D tuple of floats."); + +static PyObject * +Handle_get_velocity(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + AUD_Vector3 v = device->getSourceVelocity(self->handle); + return Py_BuildValue("(fff)", v.x(), v.y(), v.z()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Handle_set_velocity(Handle *self, PyObject* args, void* nothing) +{ + float x, y, z; + + if(!PyArg_Parse(args, "(fff):velocity", &x, &y, &z)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + AUD_Vector3 velocity(x, y, z); + if(device->setSourceVelocity(self->handle, velocity)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the velocity!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_orientation_doc, + "The source's orientation in 3D space as quaternion, a 4 float tuple."); + +static PyObject * +Handle_get_orientation(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + AUD_Quaternion o = device->getSourceOrientation(self->handle); + return Py_BuildValue("(ffff)", o.w(), o.x(), o.y(), o.z()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Handle_set_orientation(Handle *self, PyObject* args, void* nothing) +{ + float w, x, y, z; + + if(!PyArg_Parse(args, "(ffff):orientation", &w, &x, &y, &z)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + AUD_Quaternion orientation(w, x, y, z); + if(device->setSourceOrientation(self->handle, orientation)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the orientation!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_relative_doc, + "Whether the source's location, velocity and orientation is relative or absolute to the listener."); + +static PyObject * +Handle_get_relative(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->isRelative(self->handle)) + { + Py_RETURN_TRUE; + } + else + { + Py_RETURN_FALSE; + } + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Handle_set_relative(Handle *self, PyObject* args, void* nothing) +{ + if(!PyBool_Check(args)) + { + PyErr_SetString(PyExc_TypeError, "Value is not a boolean!"); + return -1; + } + + bool relative = (args == Py_True); + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setRelative(self->handle, relative)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the relativeness!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_volume_minimum_doc, + "The minimum volume of the source.\n\n" + ".. seealso:: :attr:`Device.distance_model`"); + +static PyObject * +Handle_get_volume_minimum(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getVolumeMinimum(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_volume_minimum(Handle *self, PyObject* args, void* nothing) +{ + float volume; + + if(!PyArg_Parse(args, "f:volume_minimum", &volume)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setVolumeMinimum(self->handle, volume)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the minimum volume!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_volume_maximum_doc, + "The maximum volume of the source.\n\n" + ".. seealso:: :attr:`Device.distance_model`"); + +static PyObject * +Handle_get_volume_maximum(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getVolumeMaximum(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_volume_maximum(Handle *self, PyObject* args, void* nothing) +{ + float volume; + + if(!PyArg_Parse(args, "f:volume_maximum", &volume)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setVolumeMaximum(self->handle, volume)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the maximum volume!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_distance_reference_doc, + "The reference distance of the source.\n" + "At this distance the volume will be exactly :attr:`volume`.\n\n" + ".. seealso:: :attr:`Device.distance_model`"); + +static PyObject * +Handle_get_distance_reference(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getDistanceReference(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_distance_reference(Handle *self, PyObject* args, void* nothing) +{ + float distance; + + if(!PyArg_Parse(args, "f:distance_reference", &distance)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setDistanceReference(self->handle, distance)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the reference distance!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_distance_maximum_doc, + "The maximum distance of the source.\n" + "If the listener is further away the source volume will be 0.\n\n" + ".. seealso:: :attr:`Device.distance_model`"); + +static PyObject * +Handle_get_distance_maximum(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getDistanceMaximum(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_distance_maximum(Handle *self, PyObject* args, void* nothing) +{ + float distance; + + if(!PyArg_Parse(args, "f:distance_maximum", &distance)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setDistanceMaximum(self->handle, distance)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the maximum distance!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_attenuation_doc, + "This factor is used for distance based attenuation of the " + "source.\n\n" + ".. seealso:: :attr:`Device.distance_model`"); + +static PyObject * +Handle_get_attenuation(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getAttenuation(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_attenuation(Handle *self, PyObject* args, void* nothing) +{ + float factor; + + if(!PyArg_Parse(args, "f:attenuation", &factor)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setAttenuation(self->handle, factor)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the attenuation!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_cone_angle_inner_doc, + "The opening angle of the inner cone of the source. If the cone " + "values of a source are set there are two (audible) cones with " + "the apex at the :attr:`location` of the source and with infinite " + "height, heading in the direction of the source's " + ":attr:`orientation`.\n" + "In the inner cone the volume is normal. Outside the outer cone " + "the volume will be :attr:`cone_volume_outer` and in the area " + "between the volume will be interpolated linearly."); + +static PyObject * +Handle_get_cone_angle_inner(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getConeAngleInner(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_cone_angle_inner(Handle *self, PyObject* args, void* nothing) +{ + float angle; + + if(!PyArg_Parse(args, "f:cone_angle_inner", &angle)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setConeAngleInner(self->handle, angle)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the cone inner angle!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_cone_angle_outer_doc, + "The opening angle of the outer cone of the source.\n\n" + ".. seealso:: :attr:`cone_angle_inner`"); + +static PyObject * +Handle_get_cone_angle_outer(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getConeAngleOuter(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_cone_angle_outer(Handle *self, PyObject* args, void* nothing) +{ + float angle; + + if(!PyArg_Parse(args, "f:cone_angle_outer", &angle)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setConeAngleOuter(self->handle, angle)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the cone outer angle!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Handle_cone_volume_outer_doc, + "The volume outside the outer cone of the source.\n\n" + ".. seealso:: :attr:`cone_angle_inner`"); + +static PyObject * +Handle_get_cone_volume_outer(Handle *self, void* nothing) +{ + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + return Py_BuildValue("f", device->getConeVolumeOuter(self->handle)); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Handle_set_cone_volume_outer(Handle *self, PyObject* args, void* nothing) +{ + float volume; + + if(!PyArg_Parse(args, "f:cone_volume_outer", &volume)) + return -1; + + Device* dev = (Device*)self->device; + + try + { + AUD_I3DDevice* device = dynamic_cast(dev->device); + if(device) + { + if(device->setConeVolumeOuter(self->handle, volume)) + return 0; + PyErr_SetString(AUDError, "Couldn't set the cone outer volume!"); + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +static PyGetSetDef Handle_properties[] = { + {(char*)"position", (getter)Handle_get_position, (setter)Handle_set_position, + M_aud_Handle_position_doc, NULL }, + {(char*)"keep", (getter)Handle_get_keep, (setter)Handle_set_keep, + M_aud_Handle_keep_doc, NULL }, + {(char*)"status", (getter)Handle_get_status, NULL, + M_aud_Handle_status_doc, NULL }, + {(char*)"volume", (getter)Handle_get_volume, (setter)Handle_set_volume, + M_aud_Handle_volume_doc, NULL }, + {(char*)"pitch", (getter)Handle_get_pitch, (setter)Handle_set_pitch, + M_aud_Handle_pitch_doc, NULL }, + {(char*)"loop_count", (getter)Handle_get_loop_count, (setter)Handle_set_loop_count, + M_aud_Handle_loop_count_doc, NULL }, + {(char*)"location", (getter)Handle_get_location, (setter)Handle_set_location, + M_aud_Handle_location_doc, NULL }, + {(char*)"velocity", (getter)Handle_get_velocity, (setter)Handle_set_velocity, + M_aud_Handle_velocity_doc, NULL }, + {(char*)"orientation", (getter)Handle_get_orientation, (setter)Handle_set_orientation, + M_aud_Handle_orientation_doc, NULL }, + {(char*)"relative", (getter)Handle_get_relative, (setter)Handle_set_relative, + M_aud_Handle_relative_doc, NULL }, + {(char*)"volume_minimum", (getter)Handle_get_volume_minimum, (setter)Handle_set_volume_minimum, + M_aud_Handle_volume_minimum_doc, NULL }, + {(char*)"volume_maximum", (getter)Handle_get_volume_maximum, (setter)Handle_set_volume_maximum, + M_aud_Handle_volume_maximum_doc, NULL }, + {(char*)"distance_reference", (getter)Handle_get_distance_reference, (setter)Handle_set_distance_reference, + M_aud_Handle_distance_reference_doc, NULL }, + {(char*)"distance_maximum", (getter)Handle_get_distance_maximum, (setter)Handle_set_distance_maximum, + M_aud_Handle_distance_maximum_doc, NULL }, + {(char*)"attenuation", (getter)Handle_get_attenuation, (setter)Handle_set_attenuation, + M_aud_Handle_attenuation_doc, NULL }, + {(char*)"cone_angle_inner", (getter)Handle_get_cone_angle_inner, (setter)Handle_set_cone_angle_inner, + M_aud_Handle_cone_angle_inner_doc, NULL }, + {(char*)"cone_angle_outer", (getter)Handle_get_cone_angle_outer, (setter)Handle_set_cone_angle_outer, + M_aud_Handle_cone_angle_outer_doc, NULL }, + {(char*)"cone_volume_outer", (getter)Handle_get_cone_volume_outer, (setter)Handle_set_cone_volume_outer, + M_aud_Handle_cone_volume_outer_doc, NULL }, + {NULL} /* Sentinel */ +}; + +PyDoc_STRVAR(M_aud_Handle_doc, + "Handle objects are playback handles that can be used to control " + "playback of a sound. If a sound is played back multiple times " + "then there are as many handles."); + +static PyTypeObject HandleType = { + PyVarObject_HEAD_INIT(NULL, 0) + "aud.Handle", /* tp_name */ + sizeof(Handle), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)Handle_dealloc,/* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + M_aud_Handle_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Handle_methods, /* tp_methods */ + 0, /* tp_members */ + Handle_properties, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ +}; + +// ========== Device ================================================== + +static void +Device_dealloc(Device* self) +{ + if(self->device) + delete self->device; + Py_TYPE(self)->tp_free((PyObject*)self); +} + +static PyObject * +Device_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + Device *self; + + static const char *kwlist[] = {"type", "rate", "channels", "format", "buffer_size", "name", NULL}; + int device; + int rate = AUD_RATE_44100; + int channels = AUD_CHANNELS_STEREO; + int format = AUD_FORMAT_FLOAT32; + int buffersize = AUD_DEFAULT_BUFFER_SIZE; + const char* name = "Audaspace"; + + if(!PyArg_ParseTupleAndKeywords(args, kwds, "i|iiiis:Device", const_cast(kwlist), + &device, &rate, &channels, &format, &buffersize, &name)) + return NULL; + + if(buffersize < 128) + { + PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than 127!"); + return NULL; + } + + self = (Device*)type->tp_alloc(type, 0); + if(self != NULL) + { + AUD_DeviceSpecs specs; + specs.channels = (AUD_Channels)channels; + specs.format = (AUD_SampleFormat)format; + specs.rate = (AUD_SampleRate)rate; + + self->device = NULL; + + try + { + switch(device) + { + case AUD_DEVICE_NULL: + self->device = new AUD_NULLDevice(); + break; + case AUD_DEVICE_OPENAL: +#ifdef WITH_OPENAL + self->device = new AUD_OpenALDevice(specs, buffersize); +#endif + break; + case AUD_DEVICE_SDL: +#ifdef WITH_SDL + self->device = new AUD_SDLDevice(specs, buffersize); +#endif + break; + case AUD_DEVICE_JACK: +#ifdef WITH_JACK + self->device = new AUD_JackDevice(name, specs, buffersize); +#endif + break; + case AUD_DEVICE_READ: + break; + } + + } + catch(AUD_Exception& e) + { + Py_DECREF(self); + PyErr_SetString(AUDError, e.str); + return NULL; + } + + if(!self->device) + { + Py_DECREF(self); + PyErr_SetString(AUDError, "Unsupported device type!"); + return NULL; + } + } + + return (PyObject *)self; +} + +PyDoc_STRVAR(M_aud_Device_play_doc, + "play(factory, keep=False)\n\n" + "Plays a factory.\n\n" + ":arg factory: The factory to play.\n" + ":type factory: :class:`Factory`\n" + ":arg keep: See :attr:`Handle.keep`.\n" + ":type keep: bool\n" + ":return: The playback handle with which playback can be " + "controlled with.\n" + ":rtype: :class:`Handle`"); + +static PyObject * +Device_play(Device *self, PyObject *args, PyObject *kwds) +{ + PyObject* object; + PyObject* keepo = NULL; + + bool keep = false; + + static const char *kwlist[] = {"factory", "keep", NULL}; + + if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:play", const_cast(kwlist), &object, &keepo)) + return NULL; + + if(!PyObject_TypeCheck(object, &FactoryType)) + { + PyErr_SetString(PyExc_TypeError, "Object is not of type Factory!"); + return NULL; + } + + if(keepo != NULL) + { + if(!PyBool_Check(keepo)) + { + PyErr_SetString(PyExc_TypeError, "keep is not a boolean!"); + return NULL; + } + + keep = keepo == Py_True; + } + + Factory* sound = (Factory*)object; + Handle *handle; + + handle = (Handle*)HandleType.tp_alloc(&HandleType, 0); + if(handle != NULL) + { + handle->device = (PyObject*)self; + Py_INCREF(self); + + try + { + handle->handle = self->device->play(sound->factory, keep); + } + catch(AUD_Exception& e) + { + Py_DECREF(handle); + PyErr_SetString(AUDError, e.str); + return NULL; + } + } + + return (PyObject *)handle; +} + +PyDoc_STRVAR(M_aud_Device_lock_doc, + "lock()\n\n" + "Locks the device so that it's guaranteed, that no samples are " + "read from the streams until :meth:`unlock` is called.\n" + "This is useful if you want to do start/stop/pause/resume some " + "sounds at the same time.\n\n" + ".. note:: The device has to be unlocked as often as locked to be " + "able to continue playback.\n\n" + ".. warning:: Make sure the time between locking and unlocking is " + "as short as possible to avoid clicks."); + +static PyObject * +Device_lock(Device *self) +{ + try + { + self->device->lock(); + Py_RETURN_NONE; + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +PyDoc_STRVAR(M_aud_Device_unlock_doc, + "unlock()\n\n" + "Unlocks the device after a lock call, see :meth:`lock` for " + "details."); + +static PyObject * +Device_unlock(Device *self) +{ + try + { + self->device->unlock(); + Py_RETURN_NONE; + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static PyMethodDef Device_methods[] = { + {"play", (PyCFunction)Device_play, METH_VARARGS | METH_KEYWORDS, + M_aud_Device_play_doc + }, + {"lock", (PyCFunction)Device_lock, METH_NOARGS, + M_aud_Device_lock_doc + }, + {"unlock", (PyCFunction)Device_unlock, METH_NOARGS, + M_aud_Device_unlock_doc + }, + {NULL} /* Sentinel */ +}; + +PyDoc_STRVAR(M_aud_Device_rate_doc, + "The sampling rate of the device in Hz."); + +static PyObject * +Device_get_rate(Device *self, void* nothing) +{ + try + { + AUD_DeviceSpecs specs = self->device->getSpecs(); + return Py_BuildValue("i", specs.rate); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +PyDoc_STRVAR(M_aud_Device_format_doc, + "The native sample format of the device."); + +static PyObject * +Device_get_format(Device *self, void* nothing) +{ + try + { + AUD_DeviceSpecs specs = self->device->getSpecs(); + return Py_BuildValue("i", specs.format); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +PyDoc_STRVAR(M_aud_Device_channels_doc, + "The channel count of the device."); + +static PyObject * +Device_get_channels(Device *self, void* nothing) +{ + try + { + AUD_DeviceSpecs specs = self->device->getSpecs(); + return Py_BuildValue("i", specs.channels); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +PyDoc_STRVAR(M_aud_Device_volume_doc, + "The overall volume of the device."); + +static PyObject * +Device_get_volume(Device *self, void* nothing) +{ + try + { + return Py_BuildValue("f", self->device->getVolume()); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Device_set_volume(Device *self, PyObject* args, void* nothing) +{ + float volume; + + if(!PyArg_Parse(args, "f:volume", &volume)) + return -1; + + try + { + self->device->setVolume(volume); + return 0; + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return -1; + } +} + +PyDoc_STRVAR(M_aud_Device_listener_location_doc, + "The listeners's location in 3D space, a 3D tuple of floats."); + +static PyObject * +Device_get_listener_location(Device *self, void* nothing) +{ + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + AUD_Vector3 v = device->getListenerLocation(); + return Py_BuildValue("(fff)", v.x(), v.y(), v.z()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Device_set_listener_location(Device *self, PyObject* args, void* nothing) +{ + float x, y, z; + + if(!PyArg_Parse(args, "(fff):listener_location", &x, &y, &z)) + return -1; + + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + AUD_Vector3 location(x, y, z); + device->setListenerLocation(location); + return 0; + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Device_listener_velocity_doc, + "The listener's velocity in 3D space, a 3D tuple of floats."); + +static PyObject * +Device_get_listener_velocity(Device *self, void* nothing) +{ + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + AUD_Vector3 v = device->getListenerVelocity(); + return Py_BuildValue("(fff)", v.x(), v.y(), v.z()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Device_set_listener_velocity(Device *self, PyObject* args, void* nothing) +{ + float x, y, z; + + if(!PyArg_Parse(args, "(fff):listener_velocity", &x, &y, &z)) + return -1; + + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + AUD_Vector3 velocity(x, y, z); + device->setListenerVelocity(velocity); + return 0; + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Device_listener_orientation_doc, + "The listener's orientation in 3D space as quaternion, a 4 float tuple."); + +static PyObject * +Device_get_listener_orientation(Device *self, void* nothing) +{ + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + AUD_Quaternion o = device->getListenerOrientation(); + return Py_BuildValue("(ffff)", o.w(), o.x(), o.y(), o.z()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return NULL; +} + +static int +Device_set_listener_orientation(Device *self, PyObject* args, void* nothing) +{ + float w, x, y, z; + + if(!PyArg_Parse(args, "(ffff):listener_orientation", &w, &x, &y, &z)) + return -1; + + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + AUD_Quaternion orientation(w, x, y, z); + device->setListenerOrientation(orientation); + return 0; + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Device_speed_of_sound_doc, + "The speed of sound of the device.\n" + "The speed of sound in air is typically 343 m/s."); + +static PyObject * +Device_get_speed_of_sound(Device *self, void* nothing) +{ + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + return Py_BuildValue("f", device->getSpeedOfSound()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Device_set_speed_of_sound(Device *self, PyObject* args, void* nothing) +{ + float speed; + + if(!PyArg_Parse(args, "f:speed_of_sound", &speed)) + return -1; + + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + device->setSpeedOfSound(speed); + return 0; + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Device_doppler_factor_doc, + "The doppler factor of the device.\n" + "This factor is a scaling factor for the velocity vectors in " + "doppler calculation. So a value bigger than 1 will exaggerate " + "the effect as it raises the velocity."); + +static PyObject * +Device_get_doppler_factor(Device *self, void* nothing) +{ + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + return Py_BuildValue("f", device->getDopplerFactor()); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Device_set_doppler_factor(Device *self, PyObject* args, void* nothing) +{ + float factor; + + if(!PyArg_Parse(args, "f:doppler_factor", &factor)) + return -1; + + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + device->setDopplerFactor(factor); + return 0; + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +PyDoc_STRVAR(M_aud_Device_distance_model_doc, + "The distance model of the device.\n\n" + ".. seealso:: http://connect.creativelabs.com/openal/Documentation/OpenAL%201.1%20Specification.htm#_Toc199835864"); + +static PyObject * +Device_get_distance_model(Device *self, void* nothing) +{ + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + return Py_BuildValue("i", int(device->getDistanceModel())); + } + else + { + PyErr_SetString(AUDError, device_not_3d_error); + return NULL; + } + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + return NULL; + } +} + +static int +Device_set_distance_model(Device *self, PyObject* args, void* nothing) +{ + int model; + + if(!PyArg_Parse(args, "i:distance_model", &model)) + return -1; + + try + { + AUD_I3DDevice* device = dynamic_cast(self->device); + if(device) + { + device->setDistanceModel(AUD_DistanceModel(model)); + return 0; + } + else + PyErr_SetString(AUDError, device_not_3d_error); + } + catch(AUD_Exception& e) + { + PyErr_SetString(AUDError, e.str); + } + + return -1; +} + +static PyGetSetDef Device_properties[] = { + {(char*)"rate", (getter)Device_get_rate, NULL, + M_aud_Device_rate_doc, NULL }, + {(char*)"format", (getter)Device_get_format, NULL, + M_aud_Device_format_doc, NULL }, + {(char*)"channels", (getter)Device_get_channels, NULL, + M_aud_Device_channels_doc, NULL }, + {(char*)"volume", (getter)Device_get_volume, (setter)Device_set_volume, + M_aud_Device_volume_doc, NULL }, + {(char*)"listener_location", (getter)Device_get_listener_location, (setter)Device_set_listener_location, + M_aud_Device_listener_location_doc, NULL }, + {(char*)"listener_velocity", (getter)Device_get_listener_velocity, (setter)Device_set_listener_velocity, + M_aud_Device_listener_velocity_doc, NULL }, + {(char*)"listener_orientation", (getter)Device_get_listener_orientation, (setter)Device_set_listener_orientation, + M_aud_Device_listener_orientation_doc, NULL }, + {(char*)"speed_of_sound", (getter)Device_get_speed_of_sound, (setter)Device_set_speed_of_sound, + M_aud_Device_speed_of_sound_doc, NULL }, + {(char*)"doppler_factor", (getter)Device_get_doppler_factor, (setter)Device_set_doppler_factor, + M_aud_Device_doppler_factor_doc, NULL }, + {(char*)"distance_model", (getter)Device_get_distance_model, (setter)Device_set_distance_model, + M_aud_Device_distance_model_doc, NULL }, + {NULL} /* Sentinel */ +}; + +PyDoc_STRVAR(M_aud_Device_doc, + "Device objects represent an audio output backend like OpenAL or " + "SDL, but might also represent a file output or RAM buffer " + "output."); + +static PyTypeObject DeviceType = { + PyVarObject_HEAD_INIT(NULL, 0) + "aud.Device", /* tp_name */ + sizeof(Device), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)Device_dealloc,/* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + M_aud_Device_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Device_methods, /* tp_methods */ + 0, /* tp_members */ + Device_properties, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + Device_new, /* tp_new */ +}; + +PyObject * +Device_empty() +{ + return DeviceType.tp_alloc(&DeviceType, 0); +} + +// ==================================================================== + +PyDoc_STRVAR(M_aud_doc, + "This module provides access to the audaspace audio library."); + +static struct PyModuleDef audmodule = { + PyModuleDef_HEAD_INIT, + "aud", /* name of module */ + M_aud_doc, /* module documentation */ + -1, /* size of per-interpreter state of the module, + or -1 if the module keeps state in global variables. */ + NULL, NULL, NULL, NULL, NULL +}; + +PyMODINIT_FUNC +PyInit_aud(void) +{ + PyObject* m; + + if(PyType_Ready(&FactoryType) < 0) + return NULL; + + if(PyType_Ready(&DeviceType) < 0) + return NULL; + + if(PyType_Ready(&HandleType) < 0) + return NULL; + + m = PyModule_Create(&audmodule); + if(m == NULL) + return NULL; + + Py_INCREF(&FactoryType); + PyModule_AddObject(m, "Factory", (PyObject*)&FactoryType); + + Py_INCREF(&DeviceType); + PyModule_AddObject(m, "Device", (PyObject*)&DeviceType); + + Py_INCREF(&HandleType); + PyModule_AddObject(m, "Handle", (PyObject*)&HandleType); + + AUDError = PyErr_NewException("aud.error", NULL, NULL); + Py_INCREF(AUDError); + PyModule_AddObject(m, "error", AUDError); + + // device constants + PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_NULL); + PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_OPENAL); + PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_SDL); + PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_JACK); + //PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_READ); + // format constants + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_FLOAT32); + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_FLOAT64); + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_INVALID); + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_S16); + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_S24); + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_S32); + PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_U8); + // status constants + PY_MODULE_ADD_CONSTANT(m, AUD_STATUS_INVALID); + PY_MODULE_ADD_CONSTANT(m, AUD_STATUS_PAUSED); + PY_MODULE_ADD_CONSTANT(m, AUD_STATUS_PLAYING); + // distance model constants + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_EXPONENT); + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_EXPONENT_CLAMPED); + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_INVERSE); + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_INVERSE_CLAMPED); + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_LINEAR); + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_LINEAR_CLAMPED); + PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_INVALID); + + return m; +} diff --git a/intern/audaspace/Python/AUD_PyAPI.h b/intern/audaspace/Python/AUD_PyAPI.h new file mode 100644 index 00000000000..aeeaf94af9d --- /dev/null +++ b/intern/audaspace/Python/AUD_PyAPI.h @@ -0,0 +1,67 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_PYAPI +#define AUD_PYAPI + +#include "Python.h" + +#ifdef __cplusplus +extern "C" { +#include "AUD_IDevice.h" +#else +typedef void AUD_IFactory; +typedef void AUD_IDevice; +typedef void AUD_Handle; +#endif + +typedef struct { + PyObject_HEAD + PyObject* child_list; + AUD_IFactory* factory; +} Factory; + +typedef struct { + PyObject_HEAD + AUD_Handle* handle; + PyObject* device; +} Handle; + +typedef struct { + PyObject_HEAD + AUD_IDevice* device; +} Device; + +PyMODINIT_FUNC +PyInit_aud(void); + +extern PyObject * +Device_empty(); + +#ifdef __cplusplus +} +#endif + +#endif //AUD_PYAPI diff --git a/intern/audaspace/Python/Makefile b/intern/audaspace/Python/Makefile new file mode 100644 index 00000000000..a66f86549f5 --- /dev/null +++ b/intern/audaspace/Python/Makefile @@ -0,0 +1,76 @@ +# -*- mode: gnumakefile; tab-width: 8; indent-tabs-mode: t; -*- +# vim: tabstop=8 +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# The Original Code is Copyright (C) 2010 by Blender Foundation +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): Ton +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_python +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_PYTHON)/include/python$(NAN_PYTHON_VERSION) + +ifeq ($(WITH_SDL),true) + CPPFLAGS += -DWITH_SDL + CPPFLAGS += $(NAN_SDLCFLAGS) +endif + +ifeq ($(WITH_OPENAL),true) + CPPFLAGS += -DWITH_OPENAL + CPPFLAGS += -I../OpenAL +endif + +ifeq ($(WITH_JACK),true) + CPPFLAGS += -DWITH_JACK + CPPFLAGS += $(NAN_JACKCFLAGS) + CPPFLAGS += -I../jack +endif + +ifeq ($(WITH_FFMPEG),true) + CPPFLAGS += -DWITH_FFMPEG + CPPFLAGS += $(NAN_FFMPEGCFLAGS) +endif + +ifeq ($(WITH_SNDFILE),true) + CPPFLAGS += -DWITH_SNDFILE + CPPFLAGS += -I../sndfile + CPPFLAGS += -I$(NAN_SNDFILE)/include +endif + +CPPFLAGS += -I$(NAN_SAMPLERATE)/include/ +CPPFLAGS += -I../ffmpeg +CPPFLAGS += -I../FX +CPPFLAGS += -I../SDL +CPPFLAGS += -I../SRC +CPPFLAGS += -I../intern +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/SConscript b/intern/audaspace/SConscript new file mode 100644 index 00000000000..7230bffebdc --- /dev/null +++ b/intern/audaspace/SConscript @@ -0,0 +1,48 @@ +#!/usr/bin/python + +Import ('env') + +sources = env.Glob('intern/*.cpp') + env.Glob('FX/*.cpp') + env.Glob('SRC/*.cpp') +incs = '. intern FX SRC ' + env['BF_PTHREADS_INC'] + ' ' + env['BF_LIBSAMPLERATE_INC'] +defs = [] + +if env['WITH_BF_FFMPEG']: + sources += env.Glob('ffmpeg/*.cpp') + incs += ' ffmpeg ' + env['BF_FFMPEG_INC'] + defs.append('WITH_FFMPEG') + +if env['WITH_BF_SDL']: + sources += env.Glob('SDL/*.cpp') + incs += ' SDL ' + env['BF_SDL_INC'] + defs.append('WITH_SDL') + +if env['WITH_BF_OPENAL']: + sources += env.Glob('OpenAL/*.cpp') + incs += ' OpenAL ' + env['BF_OPENAL_INC'] + defs.append('WITH_OPENAL') + +if env['WITH_BF_JACK']: + sources += env.Glob('jack/*.cpp') + incs += ' jack ' + env['BF_JACK_INC'] + defs.append('WITH_JACK') + +if env['WITH_BF_SNDFILE']: + sources += env.Glob('sndfile/*.cpp') + incs += ' sndfile ' + env['BF_SNDFILE_INC'] + defs.append('WITH_SNDFILE') + +#if env['WITH_BF_FFTW3']: +# sources += env.Glob('fftw/*.cpp') +# incs += ' fftw ' + env['BF_FFTW3_INC'] +# defs.append('WITH_FFTW3') + +if env['WITH_BF_PYTHON']: + sources += env.Glob('Python/*.cpp') + incs += ' Python ' + env['BF_PYTHON_INC'] +else: + defs.append('DISABLE_PYTHON') + +if env['OURPLATFORM'] in ('win32-vc', 'win32-mingw', 'linuxcross', 'win64-vc'): + incs += ' ' + env['BF_PTHREADS_INC'] + +env.BlenderLib ('bf_intern_audaspace', sources, Split(incs), defs, libtype=['intern','player'], priority = [25,215] ) diff --git a/intern/audaspace/SDL/AUD_SDLDevice.cpp b/intern/audaspace/SDL/AUD_SDLDevice.cpp new file mode 100644 index 00000000000..c1eb7bdf61e --- /dev/null +++ b/intern/audaspace/SDL/AUD_SDLDevice.cpp @@ -0,0 +1,93 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SDLDevice.h" +#include "AUD_IReader.h" + +void AUD_SDLDevice::SDL_mix(void *data, Uint8* buffer, int length) +{ + AUD_SDLDevice* device = (AUD_SDLDevice*)data; + + device->mix((data_t*)buffer,length/AUD_DEVICE_SAMPLE_SIZE(device->m_specs)); +} + +static const char* open_error = "AUD_SDLDevice: Device couldn't be opened."; +static const char* format_error = "AUD_SDLDevice: Obtained unsupported sample " + "format."; + +AUD_SDLDevice::AUD_SDLDevice(AUD_DeviceSpecs specs, int buffersize) +{ + if(specs.channels == AUD_CHANNELS_INVALID) + specs.channels = AUD_CHANNELS_STEREO; + if(specs.format == AUD_FORMAT_INVALID) + specs.format = AUD_FORMAT_S16; + if(specs.rate == AUD_RATE_INVALID) + specs.rate = AUD_RATE_44100; + + m_specs = specs; + + SDL_AudioSpec format, obtained; + + format.freq = m_specs.rate; + if(m_specs.format == AUD_FORMAT_U8) + format.format = AUDIO_U8; + else + format.format = AUDIO_S16SYS; + format.channels = m_specs.channels; + format.samples = buffersize; + format.callback = AUD_SDLDevice::SDL_mix; + format.userdata = this; + + if(SDL_OpenAudio(&format, &obtained) != 0) + AUD_THROW(AUD_ERROR_SDL, open_error); + + m_specs.rate = (AUD_SampleRate)obtained.freq; + m_specs.channels = (AUD_Channels)obtained.channels; + if(obtained.format == AUDIO_U8) + m_specs.format = AUD_FORMAT_U8; + else if(obtained.format == AUDIO_S16LSB || obtained.format == AUDIO_S16MSB) + m_specs.format = AUD_FORMAT_S16; + else + { + SDL_CloseAudio(); + AUD_THROW(AUD_ERROR_SDL, format_error); + } + + create(); +} + +AUD_SDLDevice::~AUD_SDLDevice() +{ + lock(); + SDL_CloseAudio(); + unlock(); + + destroy(); +} + +void AUD_SDLDevice::playing(bool playing) +{ + SDL_PauseAudio(playing ? 0 : 1); +} diff --git a/intern/audaspace/SDL/AUD_SDLDevice.h b/intern/audaspace/SDL/AUD_SDLDevice.h new file mode 100644 index 00000000000..af713b27480 --- /dev/null +++ b/intern/audaspace/SDL/AUD_SDLDevice.h @@ -0,0 +1,71 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SDLDEVICE +#define AUD_SDLDEVICE + +#include "AUD_SoftwareDevice.h" + +#include + +/** + * This device plays back through SDL, the simple direct media layer. + */ +class AUD_SDLDevice : public AUD_SoftwareDevice +{ +private: + /** + * Mixes the next bytes into the buffer. + * \param data The SDL device. + * \param buffer The target buffer. + * \param length The length in bytes to be filled. + */ + static void SDL_mix(void *data, Uint8* buffer, int length); + + // hide copy constructor and operator= + AUD_SDLDevice(const AUD_SDLDevice&); + AUD_SDLDevice& operator=(const AUD_SDLDevice&); + +protected: + virtual void playing(bool playing); + +public: + /** + * Opens the SDL audio device for playback. + * \param specs The wanted audio specification. + * \param buffersize The size of the internal buffer. + * \note The specification really used for opening the device may differ. + * \exception AUD_Exception Thrown if the audio device cannot be opened. + */ + AUD_SDLDevice(AUD_DeviceSpecs specs, + int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Closes the SDL audio device. + */ + virtual ~AUD_SDLDevice(); +}; + +#endif //AUD_SDLDEVICE diff --git a/intern/audaspace/SDL/Makefile b/intern/audaspace/SDL/Makefile new file mode 100644 index 00000000000..02a4068f3dc --- /dev/null +++ b/intern/audaspace/SDL/Makefile @@ -0,0 +1,41 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_sdl +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += $(NAN_SDLCFLAGS) +CPPFLAGS += -I../intern +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/SRC/AUD_SRCResampleFactory.cpp b/intern/audaspace/SRC/AUD_SRCResampleFactory.cpp new file mode 100644 index 00000000000..b421bb777e1 --- /dev/null +++ b/intern/audaspace/SRC/AUD_SRCResampleFactory.cpp @@ -0,0 +1,43 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SRCResampleFactory.h" +#include "AUD_SRCResampleReader.h" + +AUD_SRCResampleFactory::AUD_SRCResampleFactory(AUD_IFactory* factory, + AUD_DeviceSpecs specs) : + AUD_ResampleFactory(factory, specs) +{ +} + +AUD_IReader* AUD_SRCResampleFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + if(reader->getSpecs().rate != m_specs.rate) + reader = new AUD_SRCResampleReader(reader, m_specs.specs); + + return reader; +} diff --git a/intern/audaspace/SRC/AUD_SRCResampleFactory.h b/intern/audaspace/SRC/AUD_SRCResampleFactory.h new file mode 100644 index 00000000000..4edb0e76181 --- /dev/null +++ b/intern/audaspace/SRC/AUD_SRCResampleFactory.h @@ -0,0 +1,48 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SRCRESAMPLEFACTORY +#define AUD_SRCRESAMPLEFACTORY + +#include "AUD_ResampleFactory.h" + +/** + * This factory creates a resampling reader that uses libsamplerate for + * resampling. + */ +class AUD_SRCResampleFactory : public AUD_ResampleFactory +{ +private: + // hide copy constructor and operator= + AUD_SRCResampleFactory(const AUD_SRCResampleFactory&); + AUD_SRCResampleFactory& operator=(const AUD_SRCResampleFactory&); + +public: + AUD_SRCResampleFactory(AUD_IFactory* factory, AUD_DeviceSpecs specs); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SRCRESAMPLEFACTORY diff --git a/intern/audaspace/SRC/AUD_SRCResampleReader.cpp b/intern/audaspace/SRC/AUD_SRCResampleReader.cpp new file mode 100644 index 00000000000..91bf7002a49 --- /dev/null +++ b/intern/audaspace/SRC/AUD_SRCResampleReader.cpp @@ -0,0 +1,114 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SRCResampleReader.h" + +#include +#include +#include + +static long src_callback(void *cb_data, float **data) +{ + return ((AUD_SRCResampleReader*)cb_data)->doCallback(data); +} + +static const char* state_error = "AUD_SRCResampleReader: SRC State couldn't be " + "created."; + +AUD_SRCResampleReader::AUD_SRCResampleReader(AUD_IReader* reader, + AUD_Specs specs) : + AUD_EffectReader(reader), + m_sspecs(reader->getSpecs()), + m_factor(double(specs.rate) / double(m_sspecs.rate)), + m_tspecs(specs), + m_position(0) +{ + m_tspecs.channels = m_sspecs.channels; + + int error; + m_src = src_callback_new(src_callback, + SRC_SINC_MEDIUM_QUALITY, + m_sspecs.channels, + &error, + this); + + if(!m_src) + { + // XXX printf("%s\n", src_strerror(error)); + AUD_THROW(AUD_ERROR_SRC, state_error); + } +} + +AUD_SRCResampleReader::~AUD_SRCResampleReader() +{ + src_delete(m_src); +} + +long AUD_SRCResampleReader::doCallback(float** data) +{ + int length = m_buffer.getSize() / AUD_SAMPLE_SIZE(m_tspecs); + sample_t* buffer; + + m_reader->read(length, buffer); + + *data = buffer; + return length; +} + +void AUD_SRCResampleReader::seek(int position) +{ + m_reader->seek(position / m_factor); + src_reset(m_src); + m_position = position; +} + +int AUD_SRCResampleReader::getLength() const +{ + return m_reader->getLength() * m_factor; +} + +int AUD_SRCResampleReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_SRCResampleReader::getSpecs() const +{ + return m_tspecs; +} + +void AUD_SRCResampleReader::read(int & length, sample_t* & buffer) +{ + int size = length * AUD_SAMPLE_SIZE(m_tspecs); + + if(m_buffer.getSize() < size) + m_buffer.resize(size); + + buffer = m_buffer.getBuffer(); + + length = src_callback_read(m_src, m_factor, length, buffer); + + m_position += length; +} diff --git a/intern/audaspace/SRC/AUD_SRCResampleReader.h b/intern/audaspace/SRC/AUD_SRCResampleReader.h new file mode 100644 index 00000000000..5e3dafb359b --- /dev/null +++ b/intern/audaspace/SRC/AUD_SRCResampleReader.h @@ -0,0 +1,104 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SRCRESAMPLEREADER +#define AUD_SRCRESAMPLEREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +#include + +/** + * This resampling reader uses libsamplerate for resampling. + */ +class AUD_SRCResampleReader : public AUD_EffectReader +{ +private: + /** + * The sample specification of the source. + */ + const AUD_Specs m_sspecs; + + /** + * The resampling factor. + */ + const double m_factor; + + /** + * The sound output buffer. + */ + AUD_Buffer m_buffer; + + /** + * The target specification. + */ + AUD_Specs m_tspecs; + + /** + * The src state structure. + */ + SRC_STATE* m_src; + + /** + * The current playback position; + */ + int m_position; + + // hide copy constructor and operator= + AUD_SRCResampleReader(const AUD_SRCResampleReader&); + AUD_SRCResampleReader& operator=(const AUD_SRCResampleReader&); + +public: + /** + * Creates a resampling reader. + * \param reader The reader to mix. + * \param specs The target specification. + * \exception AUD_Exception Thrown if the source specification cannot be + * resampled to the target specification. + */ + AUD_SRCResampleReader(AUD_IReader* reader, AUD_Specs specs); + + /** + * Destroys the reader. + */ + ~AUD_SRCResampleReader(); + + /** + * The callback function for SRC. + * \warning Do not call! + * \param data The pointer to the float data. + * \return The count of samples in the float data. + */ + long doCallback(float** data); + + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_SRCRESAMPLEREADER diff --git a/intern/audaspace/SRC/Makefile b/intern/audaspace/SRC/Makefile new file mode 100644 index 00000000000..0959ebb4bfa --- /dev/null +++ b/intern/audaspace/SRC/Makefile @@ -0,0 +1,44 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_src +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_SAMPLERATE)/include +CPPFLAGS += -I../ffmpeg +CPPFLAGS += -I../FX +CPPFLAGS += -I../SDL +CPPFLAGS += -I../intern +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp new file mode 100644 index 00000000000..cad64d70790 --- /dev/null +++ b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +// needed for INT64_C +#ifndef __STDC_CONSTANT_MACROS +#define __STDC_CONSTANT_MACROS +#endif + +#include "AUD_FFMPEGFactory.h" +#include "AUD_FFMPEGReader.h" +#include "AUD_Buffer.h" + +AUD_FFMPEGFactory::AUD_FFMPEGFactory(std::string filename) : + m_filename(filename) +{ +} + +AUD_FFMPEGFactory::AUD_FFMPEGFactory(const data_t* buffer, int size) : + m_buffer(new AUD_Buffer(size)) +{ + memcpy(m_buffer.get()->getBuffer(), buffer, size); +} + +AUD_IReader* AUD_FFMPEGFactory::createReader() const +{ + if(m_buffer.get()) + return new AUD_FFMPEGReader(m_buffer); + else + return new AUD_FFMPEGReader(m_filename); +} diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h new file mode 100644 index 00000000000..43a6ce68ca7 --- /dev/null +++ b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h @@ -0,0 +1,74 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_FFMPEGFACTORY +#define AUD_FFMPEGFACTORY + +#include "AUD_IFactory.h" +#include "AUD_Reference.h" +class AUD_Buffer; + +#include + +/** + * This factory reads a sound file via ffmpeg. + * \warning Notice that the needed formats and codecs have to be registered + * for ffmpeg before this class can be used. + */ +class AUD_FFMPEGFactory : public AUD_IFactory +{ +private: + /** + * The filename of the sound source file. + */ + const std::string m_filename; + + /** + * The buffer to read from. + */ + AUD_Reference m_buffer; + + // hide copy constructor and operator= + AUD_FFMPEGFactory(const AUD_FFMPEGFactory&); + AUD_FFMPEGFactory& operator=(const AUD_FFMPEGFactory&); + +public: + /** + * Creates a new factory. + * \param filename The sound file path. + */ + AUD_FFMPEGFactory(std::string filename); + + /** + * Creates a new factory. + * \param buffer The buffer to read from. + * \param size The size of the buffer. + */ + AUD_FFMPEGFactory(const data_t* buffer, int size); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_FFMPEGFACTORY diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp new file mode 100644 index 00000000000..623e99d5edc --- /dev/null +++ b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp @@ -0,0 +1,382 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +// needed for INT64_C +#ifndef __STDC_CONSTANT_MACROS +#define __STDC_CONSTANT_MACROS +#endif + +#include "AUD_FFMPEGReader.h" + +extern "C" { +#include +#include +} + +int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer) +{ + // save packet parameters + uint8_t *audio_pkg_data = packet->data; + int audio_pkg_size = packet->size; + + int buf_size = buffer.getSize(); + int buf_pos = 0; + + int read_length, data_size; + + // as long as there is still data in the package + while(audio_pkg_size > 0) + { + // resize buffer if needed + if(buf_size - buf_pos < AVCODEC_MAX_AUDIO_FRAME_SIZE) + { + buffer.resize(buf_size + AVCODEC_MAX_AUDIO_FRAME_SIZE, true); + buf_size += AVCODEC_MAX_AUDIO_FRAME_SIZE; + } + + // read samples from the packet + data_size = buf_size - buf_pos; + /*read_length = avcodec_decode_audio3(m_codecCtx, + (int16_t*)(((data_t*)buffer.getBuffer())+buf_pos), + &data_size, + packet);*/ + read_length = avcodec_decode_audio2(m_codecCtx, + (int16_t*)(((data_t*)buffer.getBuffer()) + buf_pos), + &data_size, + audio_pkg_data, + audio_pkg_size); + + // read error, next packet! + if(read_length < 0) + break; + + buf_pos += data_size; + + // move packet parameters + audio_pkg_data += read_length; + audio_pkg_size -= read_length; + } + + return buf_pos; +} + +static const char* streaminfo_error = "AUD_FFMPEGReader: Stream info couldn't " + "be found."; +static const char* noaudio_error = "AUD_FFMPEGReader: File doesn't include an " + "audio stream."; +static const char* nodecoder_error = "AUD_FFMPEGReader: No decoder found for " + "the audio stream."; +static const char* codecopen_error = "AUD_FFMPEGReader: Codec couldn't be " + "opened."; +static const char* format_error = "AUD_FFMPEGReader: Unsupported sample " + "format."; + +void AUD_FFMPEGReader::init() +{ + m_position = 0; + m_pkgbuf_left = 0; + + if(av_find_stream_info(m_formatCtx)<0) + AUD_THROW(AUD_ERROR_FFMPEG, streaminfo_error); + + // find audio stream and codec + m_stream = -1; + + for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++) + { + if((m_formatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) + && (m_stream < 0)) + { + m_stream=i; + break; + } + } + + if(m_stream == -1) + AUD_THROW(AUD_ERROR_FFMPEG, noaudio_error); + + m_codecCtx = m_formatCtx->streams[m_stream]->codec; + + // get a decoder and open it + AVCodec *aCodec = avcodec_find_decoder(m_codecCtx->codec_id); + if(!aCodec) + AUD_THROW(AUD_ERROR_FFMPEG, nodecoder_error); + + if(avcodec_open(m_codecCtx, aCodec)<0) + AUD_THROW(AUD_ERROR_FFMPEG, codecopen_error); + + // XXX this prints file information to stdout: + //dump_format(m_formatCtx, 0, NULL, 0); + + m_specs.channels = (AUD_Channels) m_codecCtx->channels; + + switch(m_codecCtx->sample_fmt) + { + case SAMPLE_FMT_U8: + m_convert = AUD_convert_u8_float; + m_specs.format = AUD_FORMAT_U8; + break; + case SAMPLE_FMT_S16: + m_convert = AUD_convert_s16_float; + m_specs.format = AUD_FORMAT_S16; + break; + case SAMPLE_FMT_S32: + m_convert = AUD_convert_s32_float; + m_specs.format = AUD_FORMAT_S32; + break; + case SAMPLE_FMT_FLT: + m_convert = AUD_convert_copy; + m_specs.format = AUD_FORMAT_FLOAT32; + break; + case SAMPLE_FMT_DBL: + m_convert = AUD_convert_double_float; + m_specs.format = AUD_FORMAT_FLOAT64; + break; + default: + AUD_THROW(AUD_ERROR_FFMPEG, format_error); + } + + m_specs.rate = (AUD_SampleRate) m_codecCtx->sample_rate; +} + +static const char* fileopen_error = "AUD_FFMPEGReader: File couldn't be " + "opened."; + +AUD_FFMPEGReader::AUD_FFMPEGReader(std::string filename) : + m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1), + m_byteiocontext(NULL) +{ + // open file + if(av_open_input_file(&m_formatCtx, filename.c_str(), NULL, 0, NULL)!=0) + AUD_THROW(AUD_ERROR_FILE, fileopen_error); + + try + { + init(); + } + catch(AUD_Exception&) + { + av_close_input_file(m_formatCtx); + throw; + } +} + +static const char* streamopen_error = "AUD_FFMPEGReader: Stream couldn't be " + "opened."; + +AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference buffer) : + m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1), + m_membuffer(buffer) +{ + m_byteiocontext = (ByteIOContext*)av_mallocz(sizeof(ByteIOContext)); + + if(init_put_byte(m_byteiocontext, (data_t*)buffer.get()->getBuffer(), + buffer.get()->getSize(), 0, NULL, NULL, NULL, NULL) != 0) + { + av_free(m_byteiocontext); + AUD_THROW(AUD_ERROR_FILE, fileopen_error); + } + + AVProbeData probe_data; + probe_data.filename = ""; + probe_data.buf = (data_t*)buffer.get()->getBuffer(); + probe_data.buf_size = buffer.get()->getSize(); + AVInputFormat* fmt = av_probe_input_format(&probe_data, 1); + + // open stream + if(av_open_input_stream(&m_formatCtx, m_byteiocontext, "", fmt, NULL)!=0) + { + av_free(m_byteiocontext); + AUD_THROW(AUD_ERROR_FILE, streamopen_error); + } + + try + { + init(); + } + catch(AUD_Exception&) + { + av_close_input_stream(m_formatCtx); + av_free(m_byteiocontext); + throw; + } +} + +AUD_FFMPEGReader::~AUD_FFMPEGReader() +{ + avcodec_close(m_codecCtx); + + if(m_byteiocontext) + { + av_close_input_stream(m_formatCtx); + av_free(m_byteiocontext); + } + else + av_close_input_file(m_formatCtx); +} + +bool AUD_FFMPEGReader::isSeekable() const +{ + return true; +} + +void AUD_FFMPEGReader::seek(int position) +{ + if(position >= 0) + { + // a value < 0 tells us that seeking failed + if(av_seek_frame(m_formatCtx, + -1, + (uint64_t)(((uint64_t)position * + (uint64_t)AV_TIME_BASE) / + (uint64_t)m_specs.rate), + AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0) + { + avcodec_flush_buffers(m_codecCtx); + m_position = position; + + AVPacket packet; + bool search = true; + + while(search && av_read_frame(m_formatCtx, &packet) >= 0) + { + // is it a frame from the audio stream? + if(packet.stream_index == m_stream) + { + // decode the package + m_pkgbuf_left = decode(&packet, m_pkgbuf); + search = false; + + // check position + if(packet.pts != AV_NOPTS_VALUE) + { + // calculate real position, and read to frame! + m_position = packet.pts * + av_q2d(m_formatCtx->streams[m_stream]->time_base) * + m_specs.rate; + + if(m_position < position) + { + // read until we're at the right position + int length = AUD_DEFAULT_BUFFER_SIZE; + sample_t* buffer; + for(int len = position - m_position; + length == AUD_DEFAULT_BUFFER_SIZE; + len -= AUD_DEFAULT_BUFFER_SIZE) + { + if(len < AUD_DEFAULT_BUFFER_SIZE) + length = len; + read(length, buffer); + } + } + } + } + av_free_packet(&packet); + } + } + else + { + // Seeking failed, do nothing. + } + } +} + +int AUD_FFMPEGReader::getLength() const +{ + // return approximated remaning size + return (int)((m_formatCtx->duration * m_codecCtx->sample_rate) + / AV_TIME_BASE)-m_position; +} + +int AUD_FFMPEGReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_FFMPEGReader::getSpecs() const +{ + return m_specs.specs; +} + +void AUD_FFMPEGReader::read(int & length, sample_t* & buffer) +{ + // read packages and decode them + AVPacket packet; + int data_size = 0; + int pkgbuf_pos; + int left = length; + int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs); + + // resize output buffer if necessary + if(m_buffer.getSize() < length * AUD_SAMPLE_SIZE(m_specs)) + m_buffer.resize(length * AUD_SAMPLE_SIZE(m_specs)); + + buffer = m_buffer.getBuffer(); + pkgbuf_pos = m_pkgbuf_left; + m_pkgbuf_left = 0; + + // there may still be data in the buffer from the last call + if(pkgbuf_pos > 0) + { + data_size = AUD_MIN(pkgbuf_pos, left * sample_size); + m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(), + data_size / AUD_FORMAT_SIZE(m_specs.format)); + buffer += data_size / AUD_FORMAT_SIZE(m_specs.format); + left -= data_size/sample_size; + } + + // for each frame read as long as there isn't enough data already + while((left > 0) && (av_read_frame(m_formatCtx, &packet) >= 0)) + { + // is it a frame from the audio stream? + if(packet.stream_index == m_stream) + { + // decode the package + pkgbuf_pos = decode(&packet, m_pkgbuf); + + // copy to output buffer + data_size = AUD_MIN(pkgbuf_pos, left * sample_size); + m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(), + data_size / AUD_FORMAT_SIZE(m_specs.format)); + buffer += data_size / AUD_FORMAT_SIZE(m_specs.format); + left -= data_size/sample_size; + } + av_free_packet(&packet); + } + // read more data than necessary? + if(pkgbuf_pos > data_size) + { + m_pkgbuf_left = pkgbuf_pos-data_size; + memmove(m_pkgbuf.getBuffer(), + ((data_t*)m_pkgbuf.getBuffer())+data_size, + pkgbuf_pos-data_size); + } + + buffer = m_buffer.getBuffer(); + + if(left > 0) + length -= left; + m_position += length; +} diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h new file mode 100644 index 00000000000..8ebf5b45cdc --- /dev/null +++ b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h @@ -0,0 +1,154 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_FFMPEGREADER +#define AUD_FFMPEGREADER + +#include "AUD_ConverterFunctions.h" +#include "AUD_IReader.h" +#include "AUD_Reference.h" +#include "AUD_Buffer.h" + +#include + +struct AVCodecContext; +extern "C" { +#include +} + +/** + * This class reads a sound file via ffmpeg. + * \warning Seeking may not be accurate! Moreover the position is updated after + * a buffer reading call. So calling getPosition right after seek + * normally results in a wrong value. + * \warning Playback of an ogg with some outdated ffmpeg versions results in a + * segfault on windows. + */ +class AUD_FFMPEGReader : public AUD_IReader +{ +private: + /** + * The current position in samples. + */ + int m_position; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * The specification of the audio data. + */ + AUD_DeviceSpecs m_specs; + + /** + * The buffer for package reading. + */ + AUD_Buffer m_pkgbuf; + + /** + * The count of samples still available from the last read package. + */ + int m_pkgbuf_left; + + /** + * The AVFormatContext structure for using ffmpeg. + */ + AVFormatContext* m_formatCtx; + + /** + * The AVCodecContext structure for using ffmpeg. + */ + AVCodecContext* m_codecCtx; + + /** + * The ByteIOContext to read the data from. + */ + ByteIOContext* m_byteiocontext; + + /** + * The stream ID in the file. + */ + int m_stream; + + /** + * Converter function. + */ + AUD_convert_f m_convert; + + /** + * The memory file to read from, only saved to keep the buffer alive. + */ + AUD_Reference m_membuffer; + + /** + * Decodes a packet into the given buffer. + * \param packet The AVPacket to decode. + * \param buffer The target buffer. + * \return The count of read bytes. + */ + int decode(AVPacket* packet, AUD_Buffer& buffer); + + /** + * Initializes the object. + */ + void init(); + + // hide copy constructor and operator= + AUD_FFMPEGReader(const AUD_FFMPEGReader&); + AUD_FFMPEGReader& operator=(const AUD_FFMPEGReader&); + +public: + /** + * Creates a new reader. + * \param filename The path to the file to be read. + * \exception AUD_Exception Thrown if the file specified does not exist or + * cannot be read with ffmpeg. + */ + AUD_FFMPEGReader(std::string filename); + + /** + * Creates a new reader. + * \param buffer The buffer to read from. + * \exception AUD_Exception Thrown if the buffer specified cannot be read + * with ffmpeg. + */ + AUD_FFMPEGReader(AUD_Reference buffer); + + /** + * Destroys the reader and closes the file. + */ + virtual ~AUD_FFMPEGReader(); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_FFMPEGREADER diff --git a/intern/audaspace/ffmpeg/Makefile b/intern/audaspace/ffmpeg/Makefile new file mode 100644 index 00000000000..492ac83f532 --- /dev/null +++ b/intern/audaspace/ffmpeg/Makefile @@ -0,0 +1,41 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_ffmpeg +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += $(NAN_FFMPEGCFLAGS) +CPPFLAGS += -I../intern +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/fftw/AUD_BandPassFactory.cpp b/intern/audaspace/fftw/AUD_BandPassFactory.cpp new file mode 100644 index 00000000000..2950cdf8bad --- /dev/null +++ b/intern/audaspace/fftw/AUD_BandPassFactory.cpp @@ -0,0 +1,71 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_BandPassFactory.h" +#include "AUD_BandPassReader.h" + +AUD_BandPassFactory::AUD_BandPassFactory(AUD_IFactory* factory, float low, + float high) : + AUD_EffectFactory(factory), + m_low(low), + m_high(high) {} + +AUD_BandPassFactory::AUD_BandPassFactory(float low, float high) : + AUD_EffectFactory(0), + m_low(low), + m_high(high) {} + +float AUD_BandPassFactory::getLow() +{ + return m_low; +} + +float AUD_BandPassFactory::getHigh() +{ + return m_high; +} + +void AUD_BandPassFactory::setLow(float low) +{ + m_low = low; +} + +void AUD_BandPassFactory::setHigh(float high) +{ + m_high = high; +} + +AUD_IReader* AUD_BandPassFactory::createReader() +{ + AUD_IReader* reader = getReader(); + + if(reader != 0) + { + reader = new AUD_BandPassReader(reader, m_low, m_high); + AUD_NEW("reader") + } + + return reader; +} diff --git a/intern/audaspace/fftw/AUD_BandPassFactory.h b/intern/audaspace/fftw/AUD_BandPassFactory.h new file mode 100644 index 00000000000..6e1e686bc3c --- /dev/null +++ b/intern/audaspace/fftw/AUD_BandPassFactory.h @@ -0,0 +1,88 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_BANDPASSFACTORY +#define AUD_BANDPASSFACTORY + +#include "AUD_EffectFactory.h" + +/** + * This factory creates a band pass filter for a sound wave. + */ +class AUD_BandPassFactory : public AUD_EffectFactory +{ +private: + /** + * The lowest frequency to be passed. + */ + float m_low; + + /** + * The highest frequency to be passed. + */ + float m_high; + +public: + /** + * Creates a new band pass factory. + * \param factory The input factory. + * \param low The lowest passed frequency. + * \param high The highest passed frequency. + */ + AUD_BandPassFactory(AUD_IFactory* factory, float low, float high); + + /** + * Creates a new band pass factory. + * \param low The lowest passed frequency. + * \param high The highest passed frequency. + */ + AUD_BandPassFactory(float low, float high); + + /** + * Returns the lowest passed frequency. + */ + float getLow(); + + /** + * Returns the highest passed frequency. + */ + float getHigh(); + + /** + * Sets the lowest passed frequency. + * \param low The lowest passed frequency. + */ + void setLow(float low); + + /** + * Sets the highest passed frequency. + * \param high The highest passed frequency. + */ + void setHigh(float hight); + + virtual AUD_IReader* createReader(); +}; + +#endif //AUD_BANDPASSFACTORY diff --git a/intern/audaspace/fftw/AUD_BandPassReader.cpp b/intern/audaspace/fftw/AUD_BandPassReader.cpp new file mode 100644 index 00000000000..67729914c6d --- /dev/null +++ b/intern/audaspace/fftw/AUD_BandPassReader.cpp @@ -0,0 +1,124 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_BandPassReader.h" +#include "AUD_Buffer.h" + +#include +#include + +AUD_BandPassReader::AUD_BandPassReader(AUD_IReader* reader, float low, + float high) : + AUD_EffectReader(reader), m_low(low), m_high(high) +{ + m_buffer = new AUD_Buffer(); AUD_NEW("buffer") + m_in = new AUD_Buffer(); AUD_NEW("buffer") + m_out = new AUD_Buffer(); AUD_NEW("buffer") + m_length = 0; +} + +AUD_BandPassReader::~AUD_BandPassReader() +{ + if(m_length != 0) + { + fftw_destroy_plan(m_forward); + fftw_destroy_plan(m_backward); + } + + delete m_buffer; AUD_DELETE("buffer") + delete m_in; AUD_DELETE("buffer") + delete m_out; AUD_DELETE("buffer") +} + +AUD_ReaderType AUD_BandPassReader::getType() +{ + return m_reader->getType(); +} + +void AUD_BandPassReader::read(int & length, sample_t* & buffer) +{ + AUD_Specs specs = m_reader->getSpecs(); + + m_reader->read(length, buffer); + + if(length > 0) + { + if(length * AUD_SAMPLE_SIZE(specs) > m_buffer->getSize()) + m_buffer->resize(length * AUD_SAMPLE_SIZE(specs)); + + if(length != m_length) + { + if(m_length != 0) + { + fftw_destroy_plan(m_forward); + fftw_destroy_plan(m_backward); + } + + m_length = length; + + if(m_length * sizeof(double) > m_in->getSize()) + { + m_in->resize(m_length * sizeof(double)); + m_out->resize((m_length / 2 + 1) * sizeof(fftw_complex)); + } + + m_forward = fftw_plan_dft_r2c_1d(m_length, + (double*)m_in->getBuffer(), + (fftw_complex*)m_out->getBuffer(), + FFTW_ESTIMATE); + m_backward = fftw_plan_dft_c2r_1d(m_length, + (fftw_complex*)m_out->getBuffer(), + (double*)m_in->getBuffer(), + FFTW_ESTIMATE); + } + + double* target = (double*) m_in->getBuffer(); + sample_t* target2 = m_buffer->getBuffer(); + fftw_complex* complex = (fftw_complex*) m_out->getBuffer(); + float frequency; + + for(int channel = 0; channel < specs.channels; channel++) + { + for(int i = 0; i < m_length; i++) + target[i] = buffer[i * specs.channels + channel]; + + fftw_execute(m_forward); + + for(int i = 0; i < m_length / 2 + 1; i++) + { + frequency = i * specs.rate / (m_length / 2.0f + 1.0f); + if((frequency < m_low) || (frequency > m_high)) + complex[i][0] = complex[i][1] = 0.0; + } + + fftw_execute(m_backward); + + for(int i = 0; i < m_length; i++) + target2[i * specs.channels + channel] = target[i] / m_length; + } + } + + buffer = m_buffer->getBuffer(); +} diff --git a/intern/audaspace/fftw/AUD_BandPassReader.h b/intern/audaspace/fftw/AUD_BandPassReader.h new file mode 100644 index 00000000000..bb63a3ec818 --- /dev/null +++ b/intern/audaspace/fftw/AUD_BandPassReader.h @@ -0,0 +1,98 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_BANDPASSREADER +#define AUD_BANDPASSREADER + +#include + +#include "AUD_EffectReader.h" +class AUD_Buffer; + +/** + * This class only passes a specific frequency band of another reader. + */ +class AUD_BandPassReader : public AUD_EffectReader +{ +private: + /** + * The playback buffer. + */ + AUD_Buffer *m_buffer; + + /** + * The input buffer for fourier transformations. + */ + AUD_Buffer *m_in; + + /** + * The output buffer for fourier transformations. + */ + AUD_Buffer *m_out; + + /** + * The lowest passed frequency. + */ + float m_low; + + /** + * The highest passed frequency. + */ + float m_high; + + /** + * The fftw plan for forward transformation. + */ + fftw_plan m_forward; + + /** + * The fftw plan for backward transformation. + */ + fftw_plan m_backward; + + /** + * The length of the plans. + */ + int m_length; + +public: + /** + * Creates a new band pass reader. + * \param reader The reader to read from. + * \param low The lowest passed frequency. + * \param high The highest passed frequency. + */ + AUD_BandPassReader(AUD_IReader* reader, float low, float high); + + /** + * Destroys the reader. + */ + virtual ~AUD_BandPassReader(); + + virtual AUD_ReaderType getType(); + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_BANDPASSREADER diff --git a/intern/audaspace/fftw/Makefile b/intern/audaspace/fftw/Makefile new file mode 100644 index 00000000000..c2d069d068c --- /dev/null +++ b/intern/audaspace/fftw/Makefile @@ -0,0 +1,42 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_fftw +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I../intern +CPPFLAGS += -I../FX +CPPFLAGS += -I.. +CPPFLAGS += -I. +CPPFLAGS += -I$(BF_FFTW3)/include diff --git a/intern/audaspace/intern/AUD_3DMath.h b/intern/audaspace/intern/AUD_3DMath.h new file mode 100644 index 00000000000..390bb1b8467 --- /dev/null +++ b/intern/audaspace/intern/AUD_3DMath.h @@ -0,0 +1,189 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_3DMATH +#define AUD_3DMATH + +class AUD_Quaternion +{ +private: + union + { + float m_v[4]; + struct + { + float m_w; + float m_x; + float m_y; + float m_z; + }; + }; + +public: + /** + * Creates a new quaternion. + * \param w The w component. + * \param x The x component. + * \param y The y component. + * \param z The z component. + */ + inline AUD_Quaternion(float w, float x, float y, float z) : + m_w(w), m_x(x), m_y(y), m_z(z) + { + } + + /** + * Retrieves the w component of the quarternion. + * \return The w component. + */ + inline const float& w() const + { + return m_w; + } + + /** + * Retrieves the x component of the quarternion. + * \return The x component. + */ + inline const float& x() const + { + return m_x; + } + + /** + * Retrieves the y component of the quarternion. + * \return The y component. + */ + inline const float& y() const + { + return m_y; + } + + /** + * Retrieves the z component of the quarternion. + * \return The z component. + */ + inline const float& z() const + { + return m_z; + } + + /** + * Retrieves the components of the vector. + * \param destination Where the 4 float values should be saved to. + */ + inline void get(float* destination) const + { + destination[0] = m_w; + destination[1] = m_x; + destination[2] = m_y; + destination[3] = m_z; + } + + /** + * Retrieves the components of the vector. + * \return The components as float[4]. + */ + inline const float* get() const + { + return m_v; + } +}; + +class AUD_Vector3 +{ +private: + union + { + float m_v[3]; + struct + { + float m_x; + float m_y; + float m_z; + }; + }; + +public: + /** + * Creates a new 3 dimensional vector. + * \param x The x component. + * \param y The y component. + * \param z The z component. + */ + inline AUD_Vector3(float x, float y, float z) : + m_x(x), m_y(y), m_z(z) + { + } + + /** + * Retrieves the x component of the vector. + * \return The x component. + */ + inline const float& x() const + { + return m_x; + } + + /** + * Retrieves the y component of the vector. + * \return The y component. + */ + inline const float& y() const + { + return m_y; + } + + /** + * Retrieves the z component of the vector. + * \return The z component. + */ + inline const float& z() const + { + return m_z; + } + + /** + * Retrieves the components of the vector. + * \param destination Where the 3 float values should be saved to. + */ + inline void get(float* destination) const + { + destination[0] = m_x; + destination[1] = m_y; + destination[2] = m_z; + } + + /** + * Retrieves the components of the vector. + * \return The components as float[3]. + */ + inline const float* get() const + { + return m_v; + } +}; + +#endif //AUD_3DMATH diff --git a/intern/audaspace/intern/AUD_Buffer.cpp b/intern/audaspace/intern/AUD_Buffer.cpp new file mode 100644 index 00000000000..11eed399ca5 --- /dev/null +++ b/intern/audaspace/intern/AUD_Buffer.cpp @@ -0,0 +1,70 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_Buffer.h" +#include "AUD_Space.h" + +#include +#include + +#define AUD_ALIGN(a) (a + 16 - ((long)a & 15)) + +AUD_Buffer::AUD_Buffer(int size) +{ + m_size = size; + m_buffer = (data_t*) malloc(size+16); +} + +AUD_Buffer::~AUD_Buffer() +{ + free(m_buffer); +} + +sample_t* AUD_Buffer::getBuffer() const +{ + return (sample_t*) AUD_ALIGN(m_buffer); +} + +int AUD_Buffer::getSize() const +{ + return m_size; +} + +void AUD_Buffer::resize(int size, bool keep) +{ + if(keep) + { + data_t* buffer = (data_t*) malloc(size + 16); + + memcpy(AUD_ALIGN(buffer), AUD_ALIGN(m_buffer), AUD_MIN(size, m_size)); + + free(m_buffer); + m_buffer = buffer; + } + else + m_buffer = (data_t*) realloc(m_buffer, size + 16); + + m_size = size; +} diff --git a/intern/audaspace/intern/AUD_Buffer.h b/intern/audaspace/intern/AUD_Buffer.h new file mode 100644 index 00000000000..b3889b35ffe --- /dev/null +++ b/intern/audaspace/intern/AUD_Buffer.h @@ -0,0 +1,79 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_BUFFER +#define AUD_BUFFER + +#include "AUD_Space.h" + +/** + * This class is a simple buffer in RAM which is 16 Byte aligned and provides + * resize functionality. + */ +class AUD_Buffer +{ +private: + /// The size of the buffer in bytes. + int m_size; + + /// The pointer to the buffer memory. + data_t* m_buffer; + + // hide copy constructor and operator= + AUD_Buffer(const AUD_Buffer&); + AUD_Buffer& operator=(const AUD_Buffer&); + +public: + /** + * Creates a new buffer. + * \param size The size of the buffer in bytes. + */ + AUD_Buffer(int size = 0); + + /** + * Destroys the buffer. + */ + ~AUD_Buffer(); + + /** + * Returns the pointer to the buffer in memory. + */ + sample_t* getBuffer() const; + + /** + * Returns the size of the buffer in bytes. + */ + int getSize() const; + + /** + * Resizes the buffer. + * \param size The new size of the buffer, measured in bytes. + * \param keep Whether to keep the old data. If the new buffer is smaller, + * the data at the end will be lost. + */ + void resize(int size, bool keep = false); +}; + +#endif //AUD_BUFFER diff --git a/intern/audaspace/intern/AUD_BufferReader.cpp b/intern/audaspace/intern/AUD_BufferReader.cpp new file mode 100644 index 00000000000..d3af549a868 --- /dev/null +++ b/intern/audaspace/intern/AUD_BufferReader.cpp @@ -0,0 +1,74 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_BufferReader.h" +#include "AUD_Buffer.h" +#include "AUD_Space.h" + +AUD_BufferReader::AUD_BufferReader(AUD_Reference buffer, + AUD_Specs specs) : + m_position(0), m_buffer(buffer), m_specs(specs) +{ +} + +bool AUD_BufferReader::isSeekable() const +{ + return true; +} + +void AUD_BufferReader::seek(int position) +{ + m_position = position; +} + +int AUD_BufferReader::getLength() const +{ + return m_buffer.get()->getSize() / AUD_SAMPLE_SIZE(m_specs); +} + +int AUD_BufferReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_BufferReader::getSpecs() const +{ + return m_specs; +} + +void AUD_BufferReader::read(int & length, sample_t* & buffer) +{ + int sample_size = AUD_SAMPLE_SIZE(m_specs); + + buffer = m_buffer.get()->getBuffer() + m_position * m_specs.channels; + + // in case the end of the buffer is reached + if(m_buffer.get()->getSize() < (m_position + length) * sample_size) + length = m_buffer.get()->getSize() / sample_size - m_position; + + if(length < 0) + length = 0; + m_position += length; +} diff --git a/intern/audaspace/intern/AUD_BufferReader.h b/intern/audaspace/intern/AUD_BufferReader.h new file mode 100644 index 00000000000..a8bd89060b0 --- /dev/null +++ b/intern/audaspace/intern/AUD_BufferReader.h @@ -0,0 +1,76 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_BUFFERREADER +#define AUD_BUFFERREADER + +#include "AUD_IReader.h" +#include "AUD_Reference.h" +class AUD_Buffer; + +/** + * This class represents a simple reader from a buffer that exists in memory. + * \warning Notice that the buffer used for creating the reader must exist as + * long as the reader exists. + */ +class AUD_BufferReader : public AUD_IReader +{ +private: + /** + * The current position in the buffer. + */ + int m_position; + + /** + * The buffer that is read. + */ + AUD_Reference m_buffer; + + /** + * The specification of the sample data in the buffer. + */ + AUD_Specs m_specs; + + // hide copy constructor and operator= + AUD_BufferReader(const AUD_BufferReader&); + AUD_BufferReader& operator=(const AUD_BufferReader&); + +public: + /** + * Creates a new buffer reader. + * \param buffer The buffer to read from. + * \param specs The specification of the sample data in the buffer. + */ + AUD_BufferReader(AUD_Reference buffer, AUD_Specs specs); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_BUFFERREADER diff --git a/intern/audaspace/intern/AUD_C-API.cpp b/intern/audaspace/intern/AUD_C-API.cpp new file mode 100644 index 00000000000..d22d9e6c434 --- /dev/null +++ b/intern/audaspace/intern/AUD_C-API.cpp @@ -0,0 +1,996 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +// needed for INT64_C +#ifndef __STDC_CONSTANT_MACROS +#define __STDC_CONSTANT_MACROS +#endif + +#ifndef DISABLE_PYTHON +#include "AUD_PyInit.h" +#include "AUD_PyAPI.h" + +Device* g_device; +bool g_pyinitialized = false; +#endif + +#include +#include +#include + +#include "AUD_NULLDevice.h" +#include "AUD_I3DDevice.h" +#include "AUD_FileFactory.h" +#include "AUD_StreamBufferFactory.h" +#include "AUD_DelayFactory.h" +#include "AUD_LimiterFactory.h" +#include "AUD_PingPongFactory.h" +#include "AUD_LoopFactory.h" +#include "AUD_RectifyFactory.h" +#include "AUD_EnvelopeFactory.h" +#include "AUD_LinearResampleFactory.h" +#include "AUD_LowpassFactory.h" +#include "AUD_HighpassFactory.h" +#include "AUD_AccumulatorFactory.h" +#include "AUD_SumFactory.h" +#include "AUD_SquareFactory.h" +#include "AUD_ChannelMapperFactory.h" +#include "AUD_Buffer.h" +#include "AUD_ReadDevice.h" +#include "AUD_IReader.h" +#include "AUD_SequencerFactory.h" +#include "AUD_SilenceFactory.h" + +#ifdef WITH_SDL +#include "AUD_SDLDevice.h" +#endif + +#ifdef WITH_OPENAL +#include "AUD_OpenALDevice.h" +#endif + +#ifdef WITH_JACK +#include "AUD_JackDevice.h" +#endif + + +#ifdef WITH_FFMPEG +extern "C" { +#include +} +#endif + +#include + +typedef AUD_IFactory AUD_Sound; +typedef AUD_ReadDevice AUD_Device; +typedef AUD_Handle AUD_Channel; + +#define AUD_CAPI_IMPLEMENTATION +#include "AUD_C-API.h" + +#ifndef NULL +#define NULL 0 +#endif + +static AUD_IDevice* AUD_device = NULL; +static AUD_I3DDevice* AUD_3ddevice = NULL; + +void AUD_initOnce() +{ +#ifdef WITH_FFMPEG + av_register_all(); +#endif +} + +int AUD_init(AUD_DeviceType device, AUD_DeviceSpecs specs, int buffersize) +{ + AUD_IDevice* dev = NULL; + + if(AUD_device) + AUD_exit(); + + try + { + switch(device) + { + case AUD_NULL_DEVICE: + dev = new AUD_NULLDevice(); + break; +#ifdef WITH_SDL + case AUD_SDL_DEVICE: + dev = new AUD_SDLDevice(specs, buffersize); + break; +#endif +#ifdef WITH_OPENAL + case AUD_OPENAL_DEVICE: + dev = new AUD_OpenALDevice(specs, buffersize); + break; +#endif +#ifdef WITH_JACK + case AUD_JACK_DEVICE: + dev = new AUD_JackDevice("Blender", specs, buffersize); + break; +#endif + default: + return false; + } + + AUD_device = dev; + AUD_3ddevice = dynamic_cast(AUD_device); + +#ifndef DISABLE_PYTHON + if(g_pyinitialized) + { + g_device = (Device*)Device_empty(); + if(g_device != NULL) + { + g_device->device = dev; + } + } +#endif + + return true; + } + catch(AUD_Exception&) + { + return false; + } +} + +void AUD_exit() +{ +#ifndef DISABLE_PYTHON + if(g_device) + { + Py_XDECREF(g_device); + g_device = NULL; + } + else +#endif + if(AUD_device) + delete AUD_device; + AUD_device = NULL; + AUD_3ddevice = NULL; +} + +#ifndef DISABLE_PYTHON +static PyObject* AUD_getCDevice(PyObject* self) +{ + if(g_device) + { + Py_INCREF(g_device); + return (PyObject*)g_device; + } + Py_RETURN_NONE; +} + +static PyMethodDef meth_getcdevice[] = {{ "device", (PyCFunction)AUD_getCDevice, METH_NOARGS, + "device()\n\n" + "Returns the application's :class:`Device`.\n\n" + ":return: The application's :class:`Device`.\n" + ":rtype: :class:`Device`"}}; + +PyObject* AUD_initPython() +{ + PyObject* module = PyInit_aud(); + PyModule_AddObject(module, "device", (PyObject *)PyCFunction_New(meth_getcdevice, NULL)); + PyDict_SetItemString(PySys_GetObject("modules"), "aud", module); + if(AUD_device) + { + g_device = (Device*)Device_empty(); + if(g_device != NULL) + { + g_device->device = AUD_device; + } + } + g_pyinitialized = true; + + return module; +} +#endif + +void AUD_lock() +{ + assert(AUD_device); + AUD_device->lock(); +} + +void AUD_unlock() +{ + assert(AUD_device); + AUD_device->unlock(); +} + +AUD_SoundInfo AUD_getInfo(AUD_Sound* sound) +{ + assert(sound); + + AUD_SoundInfo info; + info.specs.channels = AUD_CHANNELS_INVALID; + info.specs.rate = AUD_RATE_INVALID; + info.length = 0.0f; + + try + { + AUD_IReader* reader = sound->createReader(); + + if(reader) + { + info.specs = reader->getSpecs(); + info.length = reader->getLength() / (float) info.specs.rate; + } + } + catch(AUD_Exception&) + { + } + + return info; +} + +AUD_Sound* AUD_load(const char* filename) +{ + assert(filename); + return new AUD_FileFactory(filename); +} + +AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size) +{ + assert(buffer); + return new AUD_FileFactory(buffer, size); +} + +AUD_Sound* AUD_bufferSound(AUD_Sound* sound) +{ + assert(sound); + + try + { + return new AUD_StreamBufferFactory(sound); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay) +{ + assert(sound); + + try + { + return new AUD_DelayFactory(sound, delay); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end) +{ + assert(sound); + + try + { + return new AUD_LimiterFactory(sound, start, end); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +AUD_Sound* AUD_pingpongSound(AUD_Sound* sound) +{ + assert(sound); + + try + { + return new AUD_PingPongFactory(sound); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +AUD_Sound* AUD_loopSound(AUD_Sound* sound) +{ + assert(sound); + + try + { + return new AUD_LoopFactory(sound); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +int AUD_setLoop(AUD_Channel* handle, int loops) +{ + if(handle) + { + try + { + return AUD_device->setLoopCount(handle, loops); + } + catch(AUD_Exception&) + { + } + } + return false; +} + +AUD_Sound* AUD_rectifySound(AUD_Sound* sound) +{ + assert(sound); + + try + { + return new AUD_RectifyFactory(sound); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +void AUD_unload(AUD_Sound* sound) +{ + assert(sound); + delete sound; +} + +AUD_Channel* AUD_play(AUD_Sound* sound, int keep) +{ + assert(AUD_device); + assert(sound); + try + { + return AUD_device->play(sound, keep); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +int AUD_pause(AUD_Channel* handle) +{ + assert(AUD_device); + return AUD_device->pause(handle); +} + +int AUD_resume(AUD_Channel* handle) +{ + assert(AUD_device); + return AUD_device->resume(handle); +} + +int AUD_stop(AUD_Channel* handle) +{ + if(AUD_device) + return AUD_device->stop(handle); + return false; +} + +int AUD_setKeep(AUD_Channel* handle, int keep) +{ + assert(AUD_device); + return AUD_device->setKeep(handle, keep); +} + +int AUD_seek(AUD_Channel* handle, float seekTo) +{ + assert(AUD_device); + return AUD_device->seek(handle, seekTo); +} + +float AUD_getPosition(AUD_Channel* handle) +{ + assert(AUD_device); + return AUD_device->getPosition(handle); +} + +AUD_Status AUD_getStatus(AUD_Channel* handle) +{ + assert(AUD_device); + return AUD_device->getStatus(handle); +} + +int AUD_setListenerLocation(const float* location) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_Vector3 v(location[0], location[1], location[2]); + AUD_3ddevice->setListenerLocation(v); + return true; + } + + return false; +} + +int AUD_setListenerVelocity(const float* velocity) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_Vector3 v(velocity[0], velocity[1], velocity[2]); + AUD_3ddevice->setListenerVelocity(v); + return true; + } + + return false; +} + +int AUD_setListenerOrientation(const float* orientation) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_Quaternion q(orientation[3], orientation[0], orientation[1], orientation[2]); + AUD_3ddevice->setListenerOrientation(q); + return true; + } + + return false; +} + +int AUD_setSpeedOfSound(float speed) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_3ddevice->setSpeedOfSound(speed); + return true; + } + + return false; +} + +int AUD_setDopplerFactor(float factor) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_3ddevice->setDopplerFactor(factor); + return true; + } + + return false; +} + +int AUD_setDistanceModel(AUD_DistanceModel model) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_3ddevice->setDistanceModel(model); + return true; + } + + return false; +} + +int AUD_setSourceLocation(AUD_Channel* handle, const float* location) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_Vector3 v(location[0], location[1], location[2]); + return AUD_3ddevice->setSourceLocation(handle, v); + } + + return false; +} + +int AUD_setSourceVelocity(AUD_Channel* handle, const float* velocity) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_Vector3 v(velocity[0], velocity[1], velocity[2]); + return AUD_3ddevice->setSourceVelocity(handle, v); + } + + return false; +} + +int AUD_setSourceOrientation(AUD_Channel* handle, const float* orientation) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + AUD_Quaternion q(orientation[3], orientation[0], orientation[1], orientation[2]); + return AUD_3ddevice->setSourceOrientation(handle, q); + } + + return false; +} + +int AUD_setRelative(AUD_Channel* handle, int relative) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setRelative(handle, relative); + } + + return false; +} + +int AUD_setVolumeMaximum(AUD_Channel* handle, float volume) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setVolumeMaximum(handle, volume); + } + + return false; +} + +int AUD_setVolumeMinimum(AUD_Channel* handle, float volume) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setVolumeMinimum(handle, volume); + } + + return false; +} + +int AUD_setDistanceMaximum(AUD_Channel* handle, float distance) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setDistanceMaximum(handle, distance); + } + + return false; +} + +int AUD_setDistanceReference(AUD_Channel* handle, float distance) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setDistanceReference(handle, distance); + } + + return false; +} + +int AUD_setAttenuation(AUD_Channel* handle, float factor) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setAttenuation(handle, factor); + } + + return false; +} + +int AUD_setConeAngleOuter(AUD_Channel* handle, float angle) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setConeAngleOuter(handle, angle); + } + + return false; +} + +int AUD_setConeAngleInner(AUD_Channel* handle, float angle) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setConeAngleInner(handle, angle); + } + + return false; +} + +int AUD_setConeVolumeOuter(AUD_Channel* handle, float volume) +{ + assert(AUD_device); + + if(AUD_3ddevice) + { + return AUD_3ddevice->setConeVolumeOuter(handle, volume); + } + + return false; +} + +int AUD_setSoundVolume(AUD_Channel* handle, float volume) +{ + if(handle) + { + assert(AUD_device); + + try + { + return AUD_device->setVolume(handle, volume); + } + catch(AUD_Exception&) {} + } + return false; +} + +int AUD_setSoundPitch(AUD_Channel* handle, float pitch) +{ + if(handle) + { + assert(AUD_device); + + try + { + return AUD_device->setPitch(handle, pitch); + } + catch(AUD_Exception&) {} + } + return false; +} + +AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs) +{ + try + { + return new AUD_ReadDevice(specs); + } + catch(AUD_Exception&) + { + return NULL; + } +} + +AUD_Channel* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek) +{ + assert(device); + assert(sound); + + try + { + AUD_Channel* handle = device->play(sound); + device->seek(handle, seek); + return handle; + } + catch(AUD_Exception&) + { + return NULL; + } +} + +int AUD_setDeviceVolume(AUD_Device* device, float volume) +{ + assert(device); + + try + { + device->setVolume(volume); + return true; + } + catch(AUD_Exception&) {} + + return false; +} + +int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Channel* handle, + float volume) +{ + if(handle) + { + assert(device); + + try + { + return device->setVolume(handle, volume); + } + catch(AUD_Exception&) {} + } + return false; +} + +int AUD_readDevice(AUD_Device* device, data_t* buffer, int length) +{ + assert(device); + assert(buffer); + + try + { + return device->read(buffer, length); + } + catch(AUD_Exception&) + { + return false; + } +} + +void AUD_closeReadDevice(AUD_Device* device) +{ + assert(device); + + try + { + delete device; + } + catch(AUD_Exception&) + { + } +} + +float* AUD_readSoundBuffer(const char* filename, float low, float high, + float attack, float release, float threshold, + int accumulate, int additive, int square, + float sthreshold, int samplerate, int* length) +{ + AUD_Buffer buffer; + AUD_DeviceSpecs specs; + specs.channels = AUD_CHANNELS_MONO; + specs.rate = (AUD_SampleRate)samplerate; + AUD_Sound* sound; + + AUD_FileFactory file(filename); + AUD_ChannelMapperFactory mapper(&file, specs); + AUD_LowpassFactory lowpass(&mapper, high); + AUD_HighpassFactory highpass(&lowpass, low); + AUD_EnvelopeFactory envelope(&highpass, attack, release, threshold, 0.1f); + AUD_LinearResampleFactory resampler(&envelope, specs); + sound = &resampler; + AUD_SquareFactory squaref(sound, sthreshold); + if(square) + sound = &squaref; + AUD_AccumulatorFactory accumulator(sound, additive); + AUD_SumFactory sum(sound); + if(accumulate) + sound = &accumulator; + else if(additive) + sound = ∑ + + AUD_IReader* reader = sound->createReader(); + + if(reader == NULL) + return NULL; + + int len; + int position = 0; + sample_t* readbuffer; + do + { + len = samplerate; + buffer.resize((position + len) * sizeof(float), true); + reader->read(len, readbuffer); + memcpy(buffer.getBuffer() + position, readbuffer, len * sizeof(float)); + position += len; + } while(len != 0); + delete reader; + + float* result = (float*)malloc(position * sizeof(float)); + memcpy(result, buffer.getBuffer(), position * sizeof(float)); + *length = position; + return result; +} + +static void pauseSound(AUD_Channel* handle) +{ + assert(AUD_device); + + AUD_device->pause(handle); +} + +AUD_Channel* AUD_pauseAfter(AUD_Channel* handle, float seconds) +{ + assert(AUD_device); + + AUD_SilenceFactory silence; + AUD_LimiterFactory limiter(&silence, 0, seconds); + + try + { + AUD_Channel* channel = AUD_device->play(&limiter); + AUD_device->setStopCallback(channel, (stopCallback)pauseSound, handle); + return channel; + } + catch(AUD_Exception&) + { + return NULL; + } +} + +AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume) +{ +/* AUD_XXX should be this: but AUD_createSequencer is called before the device + * is initialized. + + return new AUD_SequencerFactory(AUD_device->getSpecs().specs, data, volume); +*/ + AUD_Specs specs; + specs.channels = AUD_CHANNELS_STEREO; + specs.rate = AUD_RATE_44100; + return new AUD_SequencerFactory(specs, data, volume); +} + +void AUD_destroySequencer(AUD_Sound* sequencer) +{ + delete ((AUD_SequencerFactory*)sequencer); +} + +AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound, + float begin, float end, float skip, void* data) +{ + return ((AUD_SequencerFactory*)sequencer)->add((AUD_IFactory**) sound, begin, end, skip, data); +} + +void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry) +{ + ((AUD_SequencerFactory*)sequencer)->remove(entry); +} + +void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, + float begin, float end, float skip) +{ + ((AUD_SequencerFactory*)sequencer)->move(entry, begin, end, skip); +} + +void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, char mute) +{ + ((AUD_SequencerFactory*)sequencer)->mute(entry, mute); +} + +int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length) +{ + AUD_DeviceSpecs specs; + sample_t* buf; + + specs.rate = AUD_RATE_INVALID; + specs.channels = AUD_CHANNELS_MONO; + specs.format = AUD_FORMAT_INVALID; + + AUD_ChannelMapperFactory mapper(sound, specs); + + AUD_IReader* reader = mapper.createReader(); + + int len = reader->getLength(); + float samplejump = (float)len / (float)length; + float min, max; + + for(int i = 0; i < length; i++) + { + len = floor(samplejump * (i+1)) - floor(samplejump * i); + reader->read(len, buf); + + if(len < 1) + { + length = i; + break; + } + + max = min = *buf; + for(int j = 1; j < len; j++) + { + if(buf[j] < min) + min = buf[j]; + if(buf[j] > max) + max = buf[j]; + buffer[i * 2] = min; + buffer[i * 2 + 1] = max; + } + } + + delete reader; + + return length; +} + +void AUD_startPlayback() +{ +#ifdef WITH_JACK + AUD_JackDevice* device = dynamic_cast(AUD_device); + if(device) + device->startPlayback(); +#endif +} + +void AUD_stopPlayback() +{ +#ifdef WITH_JACK + AUD_JackDevice* device = dynamic_cast(AUD_device); + if(device) + device->stopPlayback(); +#endif +} + +void AUD_seekSequencer(AUD_Channel* handle, float time) +{ +#ifdef WITH_JACK + AUD_JackDevice* device = dynamic_cast(AUD_device); + if(device) + device->seekPlayback(time); + else +#endif + { + AUD_device->seek(handle, time); + } +} + +float AUD_getSequencerPosition(AUD_Channel* handle) +{ +#ifdef WITH_JACK + AUD_JackDevice* device = dynamic_cast(AUD_device); + if(device) + return device->getPlaybackPosition(); + else +#endif + { + return AUD_device->getPosition(handle); + } +} + +#ifdef WITH_JACK +void AUD_setSyncCallback(AUD_syncFunction function, void* data) +{ + AUD_JackDevice* device = dynamic_cast(AUD_device); + if(device) + device->setSyncCallback(function, data); +} +#endif + +int AUD_doesPlayback() +{ +#ifdef WITH_JACK + AUD_JackDevice* device = dynamic_cast(AUD_device); + if(device) + return device->doesPlayback(); +#endif + return -1; +} diff --git a/intern/audaspace/intern/AUD_C-API.h b/intern/audaspace/intern/AUD_C-API.h new file mode 100644 index 00000000000..08f2845de4f --- /dev/null +++ b/intern/audaspace/intern/AUD_C-API.h @@ -0,0 +1,492 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CAPI +#define AUD_CAPI + +#ifdef __cplusplus +extern "C" { +#endif + +#include "AUD_Space.h" + +typedef enum +{ + AUD_NULL_DEVICE = 0, + AUD_SDL_DEVICE, + AUD_OPENAL_DEVICE, + AUD_JACK_DEVICE +} AUD_DeviceType; + +typedef struct +{ + AUD_Specs specs; + float length; +} AUD_SoundInfo; + +#ifndef AUD_CAPI_IMPLEMENTATION + typedef void AUD_Sound; + typedef void AUD_Channel; + typedef void AUD_Device; + typedef void AUD_SequencerEntry; + typedef float (*AUD_volumeFunction)(void*, void*, float); + typedef void (*AUD_syncFunction)(void*, int, float); +#endif + +/** + * Initializes FFMPEG if it is enabled. + */ +extern void AUD_initOnce(); + +/** + * Initializes an audio device. + * \param device The device type that should be used. + * \param specs The audio specification to be used. + * \param buffersize The buffersize for the device. + * \return Whether the device has been initialized. + */ +extern int AUD_init(AUD_DeviceType device, AUD_DeviceSpecs specs, int buffersize); + +/** + * Unitinitializes an audio device. + */ +extern void AUD_exit(); + +/** + * Locks the playback device. + */ +extern void AUD_lock(); + +/** + * Unlocks the device. + */ +extern void AUD_unlock(); + +/** + * Returns information about a sound. + * \param sound The sound to get the info about. + * \return The AUD_SoundInfo structure with filled in data. + */ +extern AUD_SoundInfo AUD_getInfo(AUD_Sound* sound); + +/** + * Loads a sound file. + * \param filename The filename of the sound file. + * \return A handle of the sound file. + */ +extern AUD_Sound* AUD_load(const char* filename); + +/** + * Loads a sound file. + * \param buffer The buffer which contains the sound file. + * \param size The size of the buffer. + * \return A handle of the sound file. + */ +extern AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size); + +/** + * Buffers a sound. + * \param sound The sound to buffer. + * \return A handle of the sound buffer. + */ +extern AUD_Sound* AUD_bufferSound(AUD_Sound* sound); + +/** + * Delays a sound. + * \param sound The sound to dealy. + * \param delay The delay in seconds. + * \return A handle of the delayed sound. + */ +extern AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay); + +/** + * Limits a sound. + * \param sound The sound to limit. + * \param start The start time in seconds. + * \param end The stop time in seconds. + * \return A handle of the limited sound. + */ +extern AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end); + +/** + * Ping pongs a sound. + * \param sound The sound to ping pong. + * \return A handle of the ping pong sound. + */ +extern AUD_Sound* AUD_pingpongSound(AUD_Sound* sound); + +/** + * Loops a sound. + * \param sound The sound to loop. + * \return A handle of the looped sound. + */ +extern AUD_Sound* AUD_loopSound(AUD_Sound* sound); + +/** + * Sets a remaining loop count of a looping sound that currently plays. + * \param handle The playback handle. + * \param loops The count of remaining loops, -1 for infinity. + * \return Whether the handle is valid. + */ +extern int AUD_setLoop(AUD_Channel* handle, int loops); + +/** + * Rectifies a sound. + * \param sound The sound to rectify. + * \return A handle of the rectified sound. + */ +extern AUD_Sound* AUD_rectifySound(AUD_Sound* sound); + +/** + * Unloads a sound of any type. + * \param sound The handle of the sound. + */ +extern void AUD_unload(AUD_Sound* sound); + +/** + * Plays back a sound file. + * \param sound The handle of the sound file. + * \param keep When keep is true the sound source will not be deleted but set to + * paused when its end has been reached. + * \return A handle to the played back sound. + */ +extern AUD_Channel* AUD_play(AUD_Sound* sound, int keep); + +/** + * Pauses a played back sound. + * \param handle The handle to the sound. + * \return Whether the handle has been playing or not. + */ +extern int AUD_pause(AUD_Channel* handle); + +/** + * Resumes a paused sound. + * \param handle The handle to the sound. + * \return Whether the handle has been paused or not. + */ +extern int AUD_resume(AUD_Channel* handle); + +/** + * Stops a playing or paused sound. + * \param handle The handle to the sound. + * \return Whether the handle has been valid or not. + */ +extern int AUD_stop(AUD_Channel* handle); + +/** + * Sets the end behaviour of a playing or paused sound. + * \param handle The handle to the sound. + * \param keep When keep is true the sound source will not be deleted but set to + * paused when its end has been reached. + * \return Whether the handle has been valid or not. + */ +extern int AUD_setKeep(AUD_Channel* handle, int keep); + +/** + * Seeks a playing or paused sound. + * \param handle The handle to the sound. + * \param seekTo From where the sound file should be played back in seconds. + * \return Whether the handle has been valid or not. + */ +extern int AUD_seek(AUD_Channel* handle, float seekTo); + +/** + * Retrieves the playback position of a handle. + * \param handle The handle to the sound. + * \return The current playback position in seconds or 0.0 if the handle is + * invalid. + */ +extern float AUD_getPosition(AUD_Channel* handle); + +/** + * Returns the status of a playing, paused or stopped sound. + * \param handle The handle to the sound. + * \return The status of the sound behind the handle. + */ +extern AUD_Status AUD_getStatus(AUD_Channel* handle); + +/** + * Sets the listener location. + * \param location The new location. + */ +extern int AUD_setListenerLocation(const float* location); + +/** + * Sets the listener velocity. + * \param velocity The new velocity. + */ +extern int AUD_setListenerVelocity(const float* velocity); + +/** + * Sets the listener orientation. + * \param orientation The new orientation as quaternion. + */ +extern int AUD_setListenerOrientation(const float* orientation); + +/** + * Sets the speed of sound. + * This value is needed for doppler effect calculation. + * \param speed The new speed of sound. + */ +extern int AUD_setSpeedOfSound(float speed); + +/** + * Sets the doppler factor. + * This value is a scaling factor for the velocity vectors of sources and + * listener which is used while calculating the doppler effect. + * \param factor The new doppler factor. + */ +extern int AUD_setDopplerFactor(float factor); + +/** + * Sets the distance model. + * \param model distance model. + */ +extern int AUD_setDistanceModel(AUD_DistanceModel model); + +/** + * Sets the location of a source. + * \param handle The handle of the source. + * \param location The new location. + * \return Whether the action succeeded. + */ +extern int AUD_setSourceLocation(AUD_Channel* handle, const float* location); + +/** + * Sets the velocity of a source. + * \param handle The handle of the source. + * \param velocity The new velocity. + * \return Whether the action succeeded. + */ +extern int AUD_setSourceVelocity(AUD_Channel* handle, const float* velocity); + +/** + * Sets the orientation of a source. + * \param handle The handle of the source. + * \param orientation The new orientation as quaternion. + * \return Whether the action succeeded. + */ +extern int AUD_setSourceOrientation(AUD_Channel* handle, const float* orientation); + +/** + * Sets whether the source location, velocity and orientation are relative + * to the listener. + * \param handle The handle of the source. + * \param relative Whether the source is relative. + * \return Whether the action succeeded. + */ +extern int AUD_setRelative(AUD_Channel* handle, int relative); + +/** + * Sets the maximum volume of a source. + * \param handle The handle of the source. + * \param volume The new maximum volume. + * \return Whether the action succeeded. + */ +extern int AUD_setVolumeMaximum(AUD_Channel* handle, float volume); + +/** + * Sets the minimum volume of a source. + * \param handle The handle of the source. + * \param volume The new minimum volume. + * \return Whether the action succeeded. + */ +extern int AUD_setVolumeMinimum(AUD_Channel* handle, float volume); + +/** + * Sets the maximum distance of a source. + * If a source is further away from the reader than this distance, the + * volume will automatically be set to 0. + * \param handle The handle of the source. + * \param distance The new maximum distance. + * \return Whether the action succeeded. + */ +extern int AUD_setDistanceMaximum(AUD_Channel* handle, float distance); + +/** + * Sets the reference distance of a source. + * \param handle The handle of the source. + * \param distance The new reference distance. + * \return Whether the action succeeded. + */ +extern int AUD_setDistanceReference(AUD_Channel* handle, float distance); + +/** + * Sets the attenuation of a source. + * This value is used for distance calculation. + * \param handle The handle of the source. + * \param factor The new attenuation. + * \return Whether the action succeeded. + */ +extern int AUD_setAttenuation(AUD_Channel* handle, float factor); + +/** + * Sets the outer angle of the cone of a source. + * \param handle The handle of the source. + * \param angle The new outer angle of the cone. + * \return Whether the action succeeded. + */ +extern int AUD_setConeAngleOuter(AUD_Channel* handle, float angle); + +/** + * Sets the inner angle of the cone of a source. + * \param handle The handle of the source. + * \param angle The new inner angle of the cone. + * \return Whether the action succeeded. + */ +extern int AUD_setConeAngleInner(AUD_Channel* handle, float angle); + +/** + * Sets the outer volume of the cone of a source. + * The volume between inner and outer angle is interpolated between inner + * volume and this value. + * \param handle The handle of the source. + * \param volume The new outer volume of the cone. + * \return Whether the action succeeded. + */ +extern int AUD_setConeVolumeOuter(AUD_Channel* handle, float volume); + +/** + * Sets the volume of a played back sound. + * \param handle The handle to the sound. + * \param volume The new volume, must be between 0.0 and 1.0. + * \return Whether the action succeeded. + */ +extern int AUD_setSoundVolume(AUD_Channel* handle, float volume); + +/** + * Sets the pitch of a played back sound. + * \param handle The handle to the sound. + * \param pitch The new pitch. + * \return Whether the action succeeded. + */ +extern int AUD_setSoundPitch(AUD_Channel* handle, float pitch); + +/** + * Opens a read device, with which audio data can be read. + * \param specs The specification of the audio data. + * \return A device handle. + */ +extern AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs); + +/** + * Sets the main volume of a device. + * \param device The device. + * \param volume The new volume, must be between 0.0 and 1.0. + * \return Whether the action succeeded. + */ +extern int AUD_setDeviceVolume(AUD_Device* device, float volume); + +/** + * Plays back a sound file through a read device. + * \param device The read device. + * \param sound The handle of the sound file. + * \param seek The position where the sound should be seeked to. + * \return A handle to the played back sound. + */ +extern AUD_Channel* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek); + +/** + * Sets the volume of a played back sound of a read device. + * \param device The read device. + * \param handle The handle to the sound. + * \param volume The new volume, must be between 0.0 and 1.0. + * \return Whether the action succeeded. + */ +extern int AUD_setDeviceSoundVolume(AUD_Device* device, + AUD_Channel* handle, + float volume); + +/** + * Reads the next samples into the supplied buffer. + * \param device The read device. + * \param buffer The target buffer. + * \param length The length in samples to be filled. + * \return True if the reading succeeded, false if there are no sounds + * played back currently, in that case the buffer is filled with + * silence. + */ +extern int AUD_readDevice(AUD_Device* device, data_t* buffer, int length); + +/** + * Closes a read device. + * \param device The read device. + */ +extern void AUD_closeReadDevice(AUD_Device* device); + +/** + * Reads a sound file into a newly created float buffer. + * The sound is therefore bandpassed, rectified and resampled. + */ +extern float* AUD_readSoundBuffer(const char* filename, float low, float high, + float attack, float release, float threshold, + int accumulate, int additive, int square, + float sthreshold, int samplerate, + int* length); + +/** + * Pauses a playing sound after a specific amount of time. + * \param handle The handle to the sound. + * \param time The time in seconds. + * \return The silence handle. + */ +extern AUD_Channel* AUD_pauseAfter(AUD_Channel* handle, float seconds); + +extern AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume); + +extern void AUD_destroySequencer(AUD_Sound* sequencer); + +extern AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound, + float begin, float end, float skip, void* data); + +extern void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry); + +extern void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, + float begin, float end, float skip); + +extern void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, + char mute); + +extern int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length); + +extern void AUD_startPlayback(); + +extern void AUD_stopPlayback(); + +extern void AUD_seekSequencer(AUD_Channel* handle, float time); + +extern float AUD_getSequencerPosition(AUD_Channel* handle); + +#ifdef WITH_JACK +extern void AUD_setSyncCallback(AUD_syncFunction function, void* data); +#endif + +extern int AUD_doesPlayback(); + +#ifdef __cplusplus +} +#endif + +#endif //AUD_CAPI diff --git a/intern/audaspace/intern/AUD_ChannelMapperFactory.cpp b/intern/audaspace/intern/AUD_ChannelMapperFactory.cpp new file mode 100644 index 00000000000..b3d5434b1e3 --- /dev/null +++ b/intern/audaspace/intern/AUD_ChannelMapperFactory.cpp @@ -0,0 +1,106 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ChannelMapperFactory.h" +#include "AUD_ChannelMapperReader.h" + +#include + +AUD_ChannelMapperFactory::AUD_ChannelMapperFactory(AUD_IFactory* factory, + AUD_DeviceSpecs specs) : + AUD_MixerFactory(factory, specs) +{ + memset(m_mapping, 0, sizeof(m_mapping)); +} + +AUD_ChannelMapperFactory::~AUD_ChannelMapperFactory() +{ + for(int i = 1; i < 10; i++) + deleteMapping(i); +} + +float** AUD_ChannelMapperFactory::getMapping(int ic) +{ + ic--; + if(ic > 8 || ic < 0) + return 0; + + if(m_mapping[ic]) + { + int channels = -1; + while(m_mapping[ic][++channels] != 0); + if(channels != m_specs.channels) + deleteMapping(ic+1); + } + + if(!m_mapping[ic]) + { + int channels = m_specs.channels; + + m_mapping[ic] = new float*[channels+1]; + m_mapping[ic][channels] = 0; + + for(int i = 0; i < channels; i++) + { + m_mapping[ic][i] = new float[ic+1]; + for(int j = 0; j <= ic; j++) + m_mapping[ic][i][j] = ((i == j) || (channels == 1) || + (ic == 0)) ? 1.0f : 0.0f; + } + } + + return m_mapping[ic]; +} + +void AUD_ChannelMapperFactory::deleteMapping(int ic) +{ + ic--; + if(ic > 8 || ic < 0) + return; + + if(m_mapping[ic]) + { + for(int i = 0; 1; i++) + { + if(m_mapping[ic][i] != 0) + { + delete[] m_mapping[ic][i]; + } + else + break; + } + delete[] m_mapping[ic]; + m_mapping[ic] = 0; + } +} + +AUD_IReader* AUD_ChannelMapperFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + int ic = reader->getSpecs().channels; + + return new AUD_ChannelMapperReader(reader, + const_cast(this)->getMapping(ic)); +} diff --git a/intern/audaspace/intern/AUD_ChannelMapperFactory.h b/intern/audaspace/intern/AUD_ChannelMapperFactory.h new file mode 100644 index 00000000000..2f315874421 --- /dev/null +++ b/intern/audaspace/intern/AUD_ChannelMapperFactory.h @@ -0,0 +1,67 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CHANNELMAPPERFACTORY +#define AUD_CHANNELMAPPERFACTORY + +#include "AUD_MixerFactory.h" + +/** + * This factory creates a reader that maps a sound source's channels to a + * specific output channel count. + */ +class AUD_ChannelMapperFactory : public AUD_MixerFactory +{ +private: + /** + * The mapping specification. + */ + float **m_mapping[9]; + + // hide copy constructor and operator= + AUD_ChannelMapperFactory(const AUD_ChannelMapperFactory&); + AUD_ChannelMapperFactory& operator=(const AUD_ChannelMapperFactory&); + +public: + AUD_ChannelMapperFactory(AUD_IFactory* factory, AUD_DeviceSpecs specs); + + virtual ~AUD_ChannelMapperFactory(); + + /** + * Returns the mapping array for editing. + * \param ic The count of input channels the array should have. + * \note The count of output channels is read of the desired output specs. + */ + float** getMapping(int ic); + + /** + * Deletes the current channel mapping. + */ + void deleteMapping(int ic); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_CHANNELMAPPERFACTORY diff --git a/intern/audaspace/intern/AUD_ChannelMapperReader.cpp b/intern/audaspace/intern/AUD_ChannelMapperReader.cpp new file mode 100644 index 00000000000..71b9f35b3a4 --- /dev/null +++ b/intern/audaspace/intern/AUD_ChannelMapperReader.cpp @@ -0,0 +1,95 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ChannelMapperReader.h" + +AUD_ChannelMapperReader::AUD_ChannelMapperReader(AUD_IReader* reader, + float **mapping) : + AUD_EffectReader(reader) +{ + m_specs = reader->getSpecs(); + + int channels = -1; + m_rch = m_specs.channels; + while(mapping[++channels] != 0); + + m_mapping = new float*[channels]; + m_specs.channels = (AUD_Channels)channels; + + float sum; + int i; + + while(channels--) + { + m_mapping[channels] = new float[m_rch]; + sum = 0.0f; + for(i=0; i < m_rch; i++) + sum += mapping[channels][i]; + for(i=0; i < m_rch; i++) + m_mapping[channels][i] = sum > 0.0f ? + mapping[channels][i]/sum : 0.0f; + } +} + +AUD_ChannelMapperReader::~AUD_ChannelMapperReader() +{ + int channels = m_specs.channels; + + while(channels--) + { + delete[] m_mapping[channels]; + } + + delete[] m_mapping; +} + +AUD_Specs AUD_ChannelMapperReader::getSpecs() const +{ + return m_specs; +} + +void AUD_ChannelMapperReader::read(int & length, sample_t* & buffer) +{ + sample_t* in = buffer; + + m_reader->read(length, in); + + if(m_buffer.getSize() < length * AUD_SAMPLE_SIZE(m_specs)) + m_buffer.resize(length * AUD_SAMPLE_SIZE(m_specs)); + + buffer = m_buffer.getBuffer(); + sample_t sum; + + for(int i = 0; i < length; i++) + { + for(int j = 0; j < m_specs.channels; j++) + { + sum = 0; + for(int k = 0; k < m_rch; k++) + sum += m_mapping[j][k] * in[i * m_rch + k]; + buffer[i * m_specs.channels + j] = sum; + } + } +} diff --git a/intern/audaspace/intern/AUD_ChannelMapperReader.h b/intern/audaspace/intern/AUD_ChannelMapperReader.h new file mode 100644 index 00000000000..398e14da55d --- /dev/null +++ b/intern/audaspace/intern/AUD_ChannelMapperReader.h @@ -0,0 +1,80 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CHANNELMAPPERREADER +#define AUD_CHANNELMAPPERREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This class maps a sound source's channels to a specific output channel count. + * \note The input sample format must be float. + */ +class AUD_ChannelMapperReader : public AUD_EffectReader +{ +private: + /** + * The sound output buffer. + */ + AUD_Buffer m_buffer; + + /** + * The output specification. + */ + AUD_Specs m_specs; + + /** + * The channel count of the reader. + */ + int m_rch; + + /** + * The mapping specification. + */ + float **m_mapping; + + // hide copy constructor and operator= + AUD_ChannelMapperReader(const AUD_ChannelMapperReader&); + AUD_ChannelMapperReader& operator=(const AUD_ChannelMapperReader&); + +public: + /** + * Creates a channel mapper reader. + * \param reader The reader to map. + * \param mapping The mapping specification as two dimensional float array. + */ + AUD_ChannelMapperReader(AUD_IReader* reader, float **mapping); + + /** + * Destroys the reader. + */ + ~AUD_ChannelMapperReader(); + + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_CHANNELMAPPERREADER diff --git a/intern/audaspace/intern/AUD_ConverterFactory.cpp b/intern/audaspace/intern/AUD_ConverterFactory.cpp new file mode 100644 index 00000000000..057d97fe794 --- /dev/null +++ b/intern/audaspace/intern/AUD_ConverterFactory.cpp @@ -0,0 +1,43 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ConverterFactory.h" +#include "AUD_ConverterReader.h" + +AUD_ConverterFactory::AUD_ConverterFactory(AUD_IFactory* factory, + AUD_DeviceSpecs specs) : + AUD_MixerFactory(factory, specs) +{ +} + +AUD_IReader* AUD_ConverterFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + if(m_specs.format != AUD_FORMAT_FLOAT32) + reader = new AUD_ConverterReader(reader, m_specs); + + return reader; +} diff --git a/intern/audaspace/intern/AUD_ConverterFactory.h b/intern/audaspace/intern/AUD_ConverterFactory.h new file mode 100644 index 00000000000..3535616a4a6 --- /dev/null +++ b/intern/audaspace/intern/AUD_ConverterFactory.h @@ -0,0 +1,48 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CONVERTERFACTORY +#define AUD_CONVERTERFACTORY + +#include "AUD_MixerFactory.h" + +/** + * This factory creates a converter reader that is able to convert from one + * audio format to another. + */ +class AUD_ConverterFactory : public AUD_MixerFactory +{ +private: + // hide copy constructor and operator= + AUD_ConverterFactory(const AUD_ConverterFactory&); + AUD_ConverterFactory& operator=(const AUD_ConverterFactory&); + +public: + AUD_ConverterFactory(AUD_IFactory* factory, AUD_DeviceSpecs specs); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_CONVERTERFACTORY diff --git a/intern/audaspace/intern/AUD_ConverterFunctions.cpp b/intern/audaspace/intern/AUD_ConverterFunctions.cpp new file mode 100644 index 00000000000..0d48df04a64 --- /dev/null +++ b/intern/audaspace/intern/AUD_ConverterFunctions.cpp @@ -0,0 +1,460 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ConverterFunctions.h" +#include "AUD_Buffer.h" + +#define AUD_U8_0 0x80 +#define AUD_S16_MAX 0x7FFF +#define AUD_S16_MIN 0x8000 +#define AUD_S16_FLT 32768.0f +#define AUD_S32_MAX 0x7FFFFFFF +#define AUD_S32_MIN 0x80000000 +#define AUD_S32_FLT 2147483648.0f +#define AUD_FLT_MAX 1.0f +#define AUD_FLT_MIN -1.0f + +void AUD_convert_u8_s16(data_t* target, data_t* source, int length) +{ + int16_t* t = (int16_t*) target; + for(int i = 0; i < length; i++) + t[i] = (((int16_t)source[i]) - AUD_U8_0) << 8; +} + +void AUD_convert_u8_s24_be(data_t* target, data_t* source, int length) +{ + for(int i = 0; i < length; i++) + { + target[i*3] = source[i] - AUD_U8_0; + target[i*3+1] = 0; + target[i*3+2] = 0; + } +} + +void AUD_convert_u8_s24_le(data_t* target, data_t* source, int length) +{ + for(int i = 0; i < length; i++) + { + target[i*3+2] = source[i] - AUD_U8_0; + target[i*3+1] = 0; + target[i*3] = 0; + } +} + +void AUD_convert_u8_s32(data_t* target, data_t* source, int length) +{ + int32_t* t = (int32_t*) target; + for(int i = 0; i < length; i++) + t[i] = (((int32_t)source[i]) - AUD_U8_0) << 24; +} + +void AUD_convert_u8_float(data_t* target, data_t* source, int length) +{ + float* t = (float*) target; + for(int i = 0; i < length; i++) + t[i] = (((int32_t)source[i]) - AUD_U8_0) / ((float)AUD_U8_0); +} + +void AUD_convert_u8_double(data_t* target, data_t* source, int length) +{ + double* t = (double*) target; + for(int i = 0; i < length; i++) + t[i] = (((int32_t)source[i]) - AUD_U8_0) / ((double)AUD_U8_0); +} + +void AUD_convert_s16_u8(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + for(int i = 0; i < length; i++) + target[i] = (unsigned char)((s[i] >> 8) + AUD_U8_0); +} + +void AUD_convert_s16_s24_be(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + for(int i = 0; i < length; i++) + { + target[i*3] = s[i] >> 8 & 0xFF; + target[i*3+1] = s[i] & 0xFF; + target[i*3+2] = 0; + } +} + +void AUD_convert_s16_s24_le(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + for(int i = 0; i < length; i++) + { + target[i*3+2] = s[i] >> 8 & 0xFF; + target[i*3+1] = s[i] & 0xFF; + target[i*3] = 0; + } +} + +void AUD_convert_s16_s32(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + int32_t* t = (int32_t*) target; + for(int i = 0; i < length; i++) + t[i] = ((int32_t)s[i]) << 16; +} + +void AUD_convert_s16_float(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + float* t = (float*) target; + for(int i = 0; i < length; i++) + t[i] = s[i] / AUD_S16_FLT; +} + +void AUD_convert_s16_double(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + double* t = (double*) target; + for(int i = 0; i < length; i++) + t[i] = s[i] / AUD_S16_FLT; +} + +void AUD_convert_s24_u8_be(data_t* target, data_t* source, int length) +{ + for(int i = 0; i < length; i++) + target[i] = source[i*3] ^ AUD_U8_0; +} + +void AUD_convert_s24_u8_le(data_t* target, data_t* source, int length) +{ + for(int i = 0; i < length; i++) + target[i] = source[i*3+2] ^ AUD_U8_0; +} + +void AUD_convert_s24_s16_be(data_t* target, data_t* source, int length) +{ + int16_t* t = (int16_t*) target; + for(int i = 0; i < length; i++) + t[i] = source[i*3] << 8 | source[i*3+1]; +} + +void AUD_convert_s24_s16_le(data_t* target, data_t* source, int length) +{ + int16_t* t = (int16_t*) target; + for(int i = 0; i < length; i++) + t[i] = source[i*3+2] << 8 | source[i*3+1]; +} + +void AUD_convert_s24_s24(data_t* target, data_t* source, int length) +{ + memcpy(target, source, length * 3); +} + +void AUD_convert_s24_s32_be(data_t* target, data_t* source, int length) +{ + int32_t* t = (int32_t*) target; + for(int i = 0; i < length; i++) + t[i] = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8; +} + +void AUD_convert_s24_s32_le(data_t* target, data_t* source, int length) +{ + int32_t* t = (int32_t*) target; + for(int i = 0; i < length; i++) + t[i] = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8; +} + +void AUD_convert_s24_float_be(data_t* target, data_t* source, int length) +{ + float* t = (float*) target; + int32_t s; + for(int i = 0; i < length; i++) + { + s = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8; + t[i] = s / AUD_S32_FLT; + } +} + +void AUD_convert_s24_float_le(data_t* target, data_t* source, int length) +{ + float* t = (float*) target; + int32_t s; + for(int i = 0; i < length; i++) + { + s = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8; + t[i] = s / AUD_S32_FLT; + } +} + +void AUD_convert_s24_double_be(data_t* target, data_t* source, int length) +{ + double* t = (double*) target; + int32_t s; + for(int i = 0; i < length; i++) + { + s = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8; + t[i] = s / AUD_S32_FLT; + } +} + +void AUD_convert_s24_double_le(data_t* target, data_t* source, int length) +{ + double* t = (double*) target; + int32_t s; + for(int i = 0; i < length; i++) + { + s = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8; + t[i] = s / AUD_S32_FLT; + } +} + +void AUD_convert_s32_u8(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + for(int i = 0; i < length; i++) + target[i] = (unsigned char)((s[i] >> 24) + AUD_U8_0); +} + +void AUD_convert_s32_s16(data_t* target, data_t* source, int length) +{ + int16_t* t = (int16_t*) target; + int32_t* s = (int32_t*) source; + for(int i = 0; i < length; i++) + t[i] = s[i] >> 16; +} + +void AUD_convert_s32_s24_be(data_t* target, data_t* source, int length) +{ + int32_t* s = (int32_t*) source; + for(int i = 0; i < length; i++) + { + target[i*3] = s[i] >> 24 & 0xFF; + target[i*3+1] = s[i] >> 16 & 0xFF; + target[i*3+2] = s[i] >> 8 & 0xFF; + } +} + +void AUD_convert_s32_s24_le(data_t* target, data_t* source, int length) +{ + int16_t* s = (int16_t*) source; + for(int i = 0; i < length; i++) + { + target[i*3+2] = s[i] >> 24 & 0xFF; + target[i*3+1] = s[i] >> 16 & 0xFF; + target[i*3] = s[i] >> 8 & 0xFF; + } +} + +void AUD_convert_s32_float(data_t* target, data_t* source, int length) +{ + int32_t* s = (int32_t*) source; + float* t = (float*) target; + for(int i = 0; i < length; i++) + t[i] = s[i] / AUD_S32_FLT; +} + +void AUD_convert_s32_double(data_t* target, data_t* source, int length) +{ + int32_t* s = (int32_t*) source; + double* t = (double*) target; + for(int i = 0; i < length; i++) + t[i] = s[i] / AUD_S32_FLT; +} + +void AUD_convert_float_u8(data_t* target, data_t* source, int length) +{ + float* s = (float*) source; + float t; + for(int i = 0; i < length; i++) + { + t = s[i] + AUD_FLT_MAX; + if(t <= 0.0f) + target[i] = 0; + else if(t >= 2.0f) + target[i] = 255; + else + target[i] = (unsigned char)(t*127); + } +} + +void AUD_convert_float_s16(data_t* target, data_t* source, int length) +{ + int16_t* t = (int16_t*) target; + float* s = (float*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t[i] = AUD_S16_MIN; + else if(s[i] >= AUD_FLT_MAX) + t[i] = AUD_S16_MAX; + else + t[i] = (int16_t)(s[i] * AUD_S16_MAX); + } +} + +void AUD_convert_float_s24_be(data_t* target, data_t* source, int length) +{ + int32_t t; + float* s = (float*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t = AUD_S32_MIN; + else if(s[i] >= AUD_FLT_MAX) + t = AUD_S32_MAX; + else + t = (int32_t)(s[i]*AUD_S32_MAX); + target[i*3] = t >> 24 & 0xFF; + target[i*3+1] = t >> 16 & 0xFF; + target[i*3+2] = t >> 8 & 0xFF; + } +} + +void AUD_convert_float_s24_le(data_t* target, data_t* source, int length) +{ + int32_t t; + float* s = (float*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t = AUD_S32_MIN; + else if(s[i] >= AUD_FLT_MAX) + t = AUD_S32_MAX; + else + t = (int32_t)(s[i]*AUD_S32_MAX); + target[i*3+2] = t >> 24 & 0xFF; + target[i*3+1] = t >> 16 & 0xFF; + target[i*3] = t >> 8 & 0xFF; + } +} + +void AUD_convert_float_s32(data_t* target, data_t* source, int length) +{ + int32_t* t = (int32_t*) target; + float* s = (float*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t[i] = AUD_S32_MIN; + else if(s[i] >= AUD_FLT_MAX) + t[i] = AUD_S32_MAX; + else + t[i] = (int32_t)(s[i]*AUD_S32_MAX); + } +} + +void AUD_convert_float_double(data_t* target, data_t* source, int length) +{ + float* s = (float*) source; + double* t = (double*) target; + for(int i = 0; i < length; i++) + t[i] = s[i]; +} + +void AUD_convert_double_u8(data_t* target, data_t* source, int length) +{ + double* s = (double*) source; + double t; + for(int i = 0; i < length; i++) + { + t = s[i] + AUD_FLT_MAX; + if(t <= 0.0) + target[i] = 0; + else if(t >= 2.0) + target[i] = 255; + else + target[i] = (unsigned char)(t*127); + } +} + +void AUD_convert_double_s16(data_t* target, data_t* source, int length) +{ + int16_t* t = (int16_t*) target; + double* s = (double*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t[i] = AUD_S16_MIN; + else if(s[i] >= AUD_FLT_MAX) + t[i] = AUD_S16_MAX; + else + t[i] = (int16_t)(s[i]*AUD_S16_MAX); + } +} + +void AUD_convert_double_s24_be(data_t* target, data_t* source, int length) +{ + int32_t t; + double* s = (double*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t = AUD_S32_MIN; + else if(s[i] >= AUD_FLT_MAX) + t = AUD_S32_MAX; + else + t = (int32_t)(s[i]*AUD_S32_MAX); + target[i*3] = t >> 24 & 0xFF; + target[i*3+1] = t >> 16 & 0xFF; + target[i*3+2] = t >> 8 & 0xFF; + } +} + +void AUD_convert_double_s24_le(data_t* target, data_t* source, int length) +{ + int32_t t; + double* s = (double*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t = AUD_S32_MIN; + else if(s[i] >= AUD_FLT_MAX) + t = AUD_S32_MAX; + else + t = (int32_t)(s[i]*AUD_S32_MAX); + target[i*3+2] = t >> 24 & 0xFF; + target[i*3+1] = t >> 16 & 0xFF; + target[i*3] = t >> 8 & 0xFF; + } +} + +void AUD_convert_double_s32(data_t* target, data_t* source, int length) +{ + int32_t* t = (int32_t*) target; + double* s = (double*) source; + for(int i = 0; i < length; i++) + { + if(s[i] <= AUD_FLT_MIN) + t[i] = AUD_S32_MIN; + else if(s[i] >= AUD_FLT_MAX) + t[i] = AUD_S32_MAX; + else + t[i] = (int32_t)(s[i]*AUD_S32_MAX); + } +} + +void AUD_convert_double_float(data_t* target, data_t* source, int length) +{ + double* s = (double*) source; + float* t = (float*) target; + for(int i = 0; i < length; i++) + t[i] = s[i]; +} diff --git a/intern/audaspace/intern/AUD_ConverterFunctions.h b/intern/audaspace/intern/AUD_ConverterFunctions.h new file mode 100644 index 00000000000..a925f138aa2 --- /dev/null +++ b/intern/audaspace/intern/AUD_ConverterFunctions.h @@ -0,0 +1,134 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CONVERTERFUNCTIONS +#define AUD_CONVERTERFUNCTIONS + +#include "AUD_Space.h" + +#include +#ifdef _MSC_VER +#if (_MSC_VER < 1300) + typedef short int16_t; + typedef int int32_t; +#else + typedef __int16 int16_t; + typedef __int32 int32_t; +#endif +#else +#include +#endif + +typedef void (*AUD_convert_f)(data_t* target, data_t* source, int length); + +template +void AUD_convert_copy(data_t* target, data_t* source, int length) +{ + memcpy(target, source, length*sizeof(T)); +} + +void AUD_convert_u8_s16(data_t* target, data_t* source, int length); + +void AUD_convert_u8_s24_be(data_t* target, data_t* source, int length); + +void AUD_convert_u8_s24_le(data_t* target, data_t* source, int length); + +void AUD_convert_u8_s32(data_t* target, data_t* source, int length); + +void AUD_convert_u8_float(data_t* target, data_t* source, int length); + +void AUD_convert_u8_double(data_t* target, data_t* source, int length); + +void AUD_convert_s16_u8(data_t* target, data_t* source, int length); + +void AUD_convert_s16_s24_be(data_t* target, data_t* source, int length); + +void AUD_convert_s16_s24_le(data_t* target, data_t* source, int length); + +void AUD_convert_s16_s32(data_t* target, data_t* source, int length); + +void AUD_convert_s16_float(data_t* target, data_t* source, int length); + +void AUD_convert_s16_double(data_t* target, data_t* source, int length); + +void AUD_convert_s24_u8_be(data_t* target, data_t* source, int length); + +void AUD_convert_s24_u8_le(data_t* target, data_t* source, int length); + +void AUD_convert_s24_s16_be(data_t* target, data_t* source, int length); + +void AUD_convert_s24_s16_le(data_t* target, data_t* source, int length); + +void AUD_convert_s24_s24(data_t* target, data_t* source, int length); + +void AUD_convert_s24_s32_be(data_t* target, data_t* source, int length); + +void AUD_convert_s24_s32_le(data_t* target, data_t* source, int length); + +void AUD_convert_s24_float_be(data_t* target, data_t* source, int length); + +void AUD_convert_s24_float_le(data_t* target, data_t* source, int length); + +void AUD_convert_s24_double_be(data_t* target, data_t* source, int length); + +void AUD_convert_s24_double_le(data_t* target, data_t* source, int length); + +void AUD_convert_s32_u8(data_t* target, data_t* source, int length); + +void AUD_convert_s32_s16(data_t* target, data_t* source, int length); + +void AUD_convert_s32_s24_be(data_t* target, data_t* source, int length); + +void AUD_convert_s32_s24_le(data_t* target, data_t* source, int length); + +void AUD_convert_s32_float(data_t* target, data_t* source, int length); + +void AUD_convert_s32_double(data_t* target, data_t* source, int length); + +void AUD_convert_float_u8(data_t* target, data_t* source, int length); + +void AUD_convert_float_s16(data_t* target, data_t* source, int length); + +void AUD_convert_float_s24_be(data_t* target, data_t* source, int length); + +void AUD_convert_float_s24_le(data_t* target, data_t* source, int length); + +void AUD_convert_float_s32(data_t* target, data_t* source, int length); + +void AUD_convert_float_double(data_t* target, data_t* source, int length); + +void AUD_convert_double_u8(data_t* target, data_t* source, int length); + +void AUD_convert_double_s16(data_t* target, data_t* source, int length); + +void AUD_convert_double_s24_be(data_t* target, data_t* source, int length); + +void AUD_convert_double_s24_le(data_t* target, data_t* source, int length); + +void AUD_convert_double_s32(data_t* target, data_t* source, int length); + +void AUD_convert_double_float(data_t* target, data_t* source, int length); + +#endif //AUD_CONVERTERFUNCTIONS diff --git a/intern/audaspace/intern/AUD_ConverterReader.cpp b/intern/audaspace/intern/AUD_ConverterReader.cpp new file mode 100644 index 00000000000..379a7c85851 --- /dev/null +++ b/intern/audaspace/intern/AUD_ConverterReader.cpp @@ -0,0 +1,85 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_ConverterReader.h" + +AUD_ConverterReader::AUD_ConverterReader(AUD_IReader* reader, + AUD_DeviceSpecs specs) : + AUD_EffectReader(reader) +{ + m_specs.specs = reader->getSpecs(); + + int bigendian = 1; + bigendian = (((char*)&bigendian)[0]) ? 0: 1; // 1 if Big Endian + + switch(specs.format) + { + case AUD_FORMAT_U8: + m_convert = AUD_convert_float_u8; + break; + case AUD_FORMAT_S16: + m_convert = AUD_convert_float_s16; + break; + case AUD_FORMAT_S24: + if(bigendian) + m_convert = AUD_convert_float_s24_be; + else + m_convert = AUD_convert_float_s24_le; + break; + case AUD_FORMAT_S32: + m_convert = AUD_convert_float_s32; + break; + case AUD_FORMAT_FLOAT32: + m_convert = AUD_convert_copy; + break; + case AUD_FORMAT_FLOAT64: + m_convert = AUD_convert_float_double; + break; + default: + break; + } + + m_specs.format = specs.format; +} + +AUD_Specs AUD_ConverterReader::getSpecs() const +{ + return m_specs.specs; +} + +void AUD_ConverterReader::read(int & length, sample_t* & buffer) +{ + m_reader->read(length, buffer); + + int samplesize = AUD_SAMPLE_SIZE(m_specs); + + if(m_buffer.getSize() < length * samplesize) + m_buffer.resize(length * samplesize); + + m_convert((data_t*)m_buffer.getBuffer(), (data_t*)buffer, + length * m_specs.channels); + + buffer = m_buffer.getBuffer(); +} diff --git a/intern/audaspace/intern/AUD_ConverterReader.h b/intern/audaspace/intern/AUD_ConverterReader.h new file mode 100644 index 00000000000..64a46fd63e6 --- /dev/null +++ b/intern/audaspace/intern/AUD_ConverterReader.h @@ -0,0 +1,70 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_CONVERTERREADER +#define AUD_CONVERTERREADER + +#include "AUD_EffectReader.h" +#include "AUD_ConverterFunctions.h" +#include "AUD_Buffer.h" + +/** + * This class converts a sound source from one to another format. + */ +class AUD_ConverterReader : public AUD_EffectReader +{ +private: + /** + * The sound output buffer. + */ + AUD_Buffer m_buffer; + + /** + * The target specification. + */ + AUD_DeviceSpecs m_specs; + + /** + * Converter function. + */ + AUD_convert_f m_convert; + + // hide copy constructor and operator= + AUD_ConverterReader(const AUD_ConverterReader&); + AUD_ConverterReader& operator=(const AUD_ConverterReader&); + +public: + /** + * Creates a converter reader. + * \param reader The reader to convert. + * \param specs The target specification. + */ + AUD_ConverterReader(AUD_IReader* reader, AUD_DeviceSpecs specs); + + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_CONVERTERREADER diff --git a/intern/audaspace/intern/AUD_DefaultMixer.cpp b/intern/audaspace/intern/AUD_DefaultMixer.cpp new file mode 100644 index 00000000000..12faa10d866 --- /dev/null +++ b/intern/audaspace/intern/AUD_DefaultMixer.cpp @@ -0,0 +1,63 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_DefaultMixer.h" +#include "AUD_SRCResampleReader.h" +#include "AUD_ChannelMapperReader.h" +#include "AUD_ChannelMapperFactory.h" + +#include + +AUD_DefaultMixer::AUD_DefaultMixer(AUD_DeviceSpecs specs) : + AUD_Mixer(specs) +{ +} + +AUD_IReader* AUD_DefaultMixer::prepare(AUD_IReader* reader) +{ + // hacky for now, until a better channel mapper reader is available + AUD_ChannelMapperFactory cmf(NULL, m_specs); + + AUD_Specs specs = reader->getSpecs(); + + // if channel count is lower in output, rechannel before resampling + if(specs.channels < m_specs.channels) + { + reader = new AUD_ChannelMapperReader(reader, + cmf.getMapping(specs.channels)); + specs.channels = m_specs.channels; + } + + // resample + if(specs.rate != m_specs.rate) + reader = new AUD_SRCResampleReader(reader, m_specs.specs); + + // rechannel + if(specs.channels != m_specs.channels) + reader = new AUD_ChannelMapperReader(reader, + cmf.getMapping(specs.channels)); + + return reader; +} diff --git a/intern/audaspace/intern/AUD_DefaultMixer.h b/intern/audaspace/intern/AUD_DefaultMixer.h new file mode 100644 index 00000000000..c2f69629c88 --- /dev/null +++ b/intern/audaspace/intern/AUD_DefaultMixer.h @@ -0,0 +1,53 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_DEFAULTMIXER +#define AUD_DEFAULTMIXER + +#include "AUD_Mixer.h" + +/** + * This class is able to mix audiosignals of different channel count and sample + * rate and convert it to a specific output format. + * It uses a default ChannelMapperFactory and a SRCResampleFactory for + * the perparation. + */ +class AUD_DefaultMixer : public AUD_Mixer +{ +public: + /** + * Creates the mixer. + */ + AUD_DefaultMixer(AUD_DeviceSpecs specs); + + /** + * This funuction prepares a reader for playback. + * \param reader The reader to prepare. + * \return The reader that should be used for playback. + */ + virtual AUD_IReader* prepare(AUD_IReader* reader); +}; + +#endif //AUD_DEFAULTMIXER diff --git a/intern/audaspace/intern/AUD_FileFactory.cpp b/intern/audaspace/intern/AUD_FileFactory.cpp new file mode 100644 index 00000000000..986fc6c109a --- /dev/null +++ b/intern/audaspace/intern/AUD_FileFactory.cpp @@ -0,0 +1,81 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifdef WITH_FFMPEG +// needed for INT64_C +#ifndef __STDC_CONSTANT_MACROS +#define __STDC_CONSTANT_MACROS +#endif +#include "AUD_FFMPEGReader.h" +#endif + +#include "AUD_FileFactory.h" +#include "AUD_Buffer.h" + +#include + +#ifdef WITH_SNDFILE +#include "AUD_SndFileReader.h" +#endif + +AUD_FileFactory::AUD_FileFactory(std::string filename) : + m_filename(filename) +{ +} + +AUD_FileFactory::AUD_FileFactory(const data_t* buffer, int size) : + m_buffer(new AUD_Buffer(size)) +{ + memcpy(m_buffer.get()->getBuffer(), buffer, size); +} + +static const char* read_error = "AUD_FileFactory: File couldn't be read."; + +AUD_IReader* AUD_FileFactory::createReader() const +{ +#ifdef WITH_SNDFILE + try + { + if(m_buffer.get()) + return new AUD_SndFileReader(m_buffer); + else + return new AUD_SndFileReader(m_filename); + } + catch(AUD_Exception&) {} +#endif + +#ifdef WITH_FFMPEG + try + { + if(m_buffer.get()) + return new AUD_FFMPEGReader(m_buffer); + else + return new AUD_FFMPEGReader(m_filename); + } + catch(AUD_Exception&) {} +#endif + + AUD_THROW(AUD_ERROR_FILE, read_error); +} diff --git a/intern/audaspace/intern/AUD_FileFactory.h b/intern/audaspace/intern/AUD_FileFactory.h new file mode 100644 index 00000000000..9182667d72e --- /dev/null +++ b/intern/audaspace/intern/AUD_FileFactory.h @@ -0,0 +1,72 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_FILEFACTORY +#define AUD_FILEFACTORY + +#include "AUD_IFactory.h" +#include "AUD_Reference.h" +class AUD_Buffer; + +#include + +/** + * This factory tries to read a sound file via all available file readers. + */ +class AUD_FileFactory : public AUD_IFactory +{ +private: + /** + * The filename of the sound source file. + */ + std::string m_filename; + + /** + * The buffer to read from. + */ + AUD_Reference m_buffer; + + // hide copy constructor and operator= + AUD_FileFactory(const AUD_FileFactory&); + AUD_FileFactory& operator=(const AUD_FileFactory&); + +public: + /** + * Creates a new factory. + * \param filename The sound file path. + */ + AUD_FileFactory(std::string filename); + + /** + * Creates a new factory. + * \param buffer The buffer to read from. + * \param size The size of the buffer. + */ + AUD_FileFactory(const data_t* buffer, int size); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_FILEFACTORY diff --git a/intern/audaspace/intern/AUD_I3DDevice.h b/intern/audaspace/intern/AUD_I3DDevice.h new file mode 100644 index 00000000000..629b0997d4d --- /dev/null +++ b/intern/audaspace/intern/AUD_I3DDevice.h @@ -0,0 +1,314 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_I3DDEVICE +#define AUD_I3DDEVICE + +#include "AUD_Space.h" +#include "AUD_3DMath.h" + +struct AUD_Handle; + +/** + * This class represents an output device for 3D sound. + */ +class AUD_I3DDevice +{ +public: + /** + * Retrieves the listener location. + * \return The listener location. + */ + virtual AUD_Vector3 getListenerLocation() const=0; + + /** + * Sets the listener location. + * \param location The new location. + */ + virtual void setListenerLocation(const AUD_Vector3& location)=0; + + /** + * Retrieves the listener velocity. + * \return The listener velocity. + */ + virtual AUD_Vector3 getListenerVelocity() const=0; + + /** + * Sets the listener velocity. + * \param velocity The new velocity. + */ + virtual void setListenerVelocity(const AUD_Vector3& velocity)=0; + + /** + * Retrieves the listener orientation. + * \return The listener orientation as quaternion. + */ + virtual AUD_Quaternion getListenerOrientation() const=0; + + /** + * Sets the listener orientation. + * \param orientation The new orientation as quaternion. + */ + virtual void setListenerOrientation(const AUD_Quaternion& orientation)=0; + + + /** + * Retrieves the speed of sound. + * This value is needed for doppler effect calculation. + * \return The speed of sound. + */ + virtual float getSpeedOfSound() const=0; + + /** + * Sets the speed of sound. + * This value is needed for doppler effect calculation. + * \param speed The new speed of sound. + */ + virtual void setSpeedOfSound(float speed)=0; + + /** + * Retrieves the doppler factor. + * This value is a scaling factor for the velocity vectors of sources and + * listener which is used while calculating the doppler effect. + * \return The doppler factor. + */ + virtual float getDopplerFactor() const=0; + + /** + * Sets the doppler factor. + * This value is a scaling factor for the velocity vectors of sources and + * listener which is used while calculating the doppler effect. + * \param factor The new doppler factor. + */ + virtual void setDopplerFactor(float factor)=0; + + /** + * Retrieves the distance model. + * \return The distance model. + */ + virtual AUD_DistanceModel getDistanceModel() const=0; + + /** + * Sets the distance model. + * \param model distance model. + */ + virtual void setDistanceModel(AUD_DistanceModel model)=0; + + + + /** + * Retrieves the location of a source. + * \param handle The handle of the source. + * \return The location. + */ + virtual AUD_Vector3 getSourceLocation(AUD_Handle* handle)=0; + + /** + * Sets the location of a source. + * \param handle The handle of the source. + * \param location The new location. + * \return Whether the action succeeded. + */ + virtual bool setSourceLocation(AUD_Handle* handle, const AUD_Vector3& location)=0; + + /** + * Retrieves the velocity of a source. + * \param handle The handle of the source. + * \return The velocity. + */ + virtual AUD_Vector3 getSourceVelocity(AUD_Handle* handle)=0; + + /** + * Sets the velocity of a source. + * \param handle The handle of the source. + * \param velocity The new velocity. + * \return Whether the action succeeded. + */ + virtual bool setSourceVelocity(AUD_Handle* handle, const AUD_Vector3& velocity)=0; + + /** + * Retrieves the orientation of a source. + * \param handle The handle of the source. + * \return The orientation as quaternion. + */ + virtual AUD_Quaternion getSourceOrientation(AUD_Handle* handle)=0; + + /** + * Sets the orientation of a source. + * \param handle The handle of the source. + * \param orientation The new orientation as quaternion. + * \return Whether the action succeeded. + */ + virtual bool setSourceOrientation(AUD_Handle* handle, const AUD_Quaternion& orientation)=0; + + + /** + * Checks whether the source location, velocity and orientation are relative + * to the listener. + * \param handle The handle of the source. + * \return Whether the source is relative. + */ + virtual bool isRelative(AUD_Handle* handle)=0; + + /** + * Sets whether the source location, velocity and orientation are relative + * to the listener. + * \param handle The handle of the source. + * \param relative Whether the source is relative. + * \return Whether the action succeeded. + */ + virtual bool setRelative(AUD_Handle* handle, bool relative)=0; + + /** + * Retrieves the maximum volume of a source. + * \param handle The handle of the source. + * \return The maximum volume. + */ + virtual float getVolumeMaximum(AUD_Handle* handle)=0; + + /** + * Sets the maximum volume of a source. + * \param handle The handle of the source. + * \param volume The new maximum volume. + * \return Whether the action succeeded. + */ + virtual bool setVolumeMaximum(AUD_Handle* handle, float volume)=0; + + /** + * Retrieves the minimum volume of a source. + * \param handle The handle of the source. + * \return The minimum volume. + */ + virtual float getVolumeMinimum(AUD_Handle* handle)=0; + + /** + * Sets the minimum volume of a source. + * \param handle The handle of the source. + * \param volume The new minimum volume. + * \return Whether the action succeeded. + */ + virtual bool setVolumeMinimum(AUD_Handle* handle, float volume)=0; + + /** + * Retrieves the maximum distance of a source. + * If a source is further away from the reader than this distance, the + * volume will automatically be set to 0. + * \param handle The handle of the source. + * \return The maximum distance. + */ + virtual float getDistanceMaximum(AUD_Handle* handle)=0; + + /** + * Sets the maximum distance of a source. + * If a source is further away from the reader than this distance, the + * volume will automatically be set to 0. + * \param handle The handle of the source. + * \param distance The new maximum distance. + * \return Whether the action succeeded. + */ + virtual bool setDistanceMaximum(AUD_Handle* handle, float distance)=0; + + /** + * Retrieves the reference distance of a source. + * \param handle The handle of the source. + * \return The reference distance. + */ + virtual float getDistanceReference(AUD_Handle* handle)=0; + + /** + * Sets the reference distance of a source. + * \param handle The handle of the source. + * \param distance The new reference distance. + * \return Whether the action succeeded. + */ + virtual bool setDistanceReference(AUD_Handle* handle, float distance)=0; + + /** + * Retrieves the attenuation of a source. + * \param handle The handle of the source. + * \return The attenuation. + */ + virtual float getAttenuation(AUD_Handle* handle)=0; + + /** + * Sets the attenuation of a source. + * This value is used for distance calculation. + * \param handle The handle of the source. + * \param factor The new attenuation. + * \return Whether the action succeeded. + */ + virtual bool setAttenuation(AUD_Handle* handle, float factor)=0; + + /** + * Retrieves the outer angle of the cone of a source. + * \param handle The handle of the source. + * \return The outer angle of the cone. + */ + virtual float getConeAngleOuter(AUD_Handle* handle)=0; + + /** + * Sets the outer angle of the cone of a source. + * \param handle The handle of the source. + * \param angle The new outer angle of the cone. + * \return Whether the action succeeded. + */ + virtual bool setConeAngleOuter(AUD_Handle* handle, float angle)=0; + + /** + * Retrieves the inner angle of the cone of a source. + * \param handle The handle of the source. + * \return The inner angle of the cone. + */ + virtual float getConeAngleInner(AUD_Handle* handle)=0; + + /** + * Sets the inner angle of the cone of a source. + * \param handle The handle of the source. + * \param angle The new inner angle of the cone. + * \return Whether the action succeeded. + */ + virtual bool setConeAngleInner(AUD_Handle* handle, float angle)=0; + + /** + * Retrieves the outer volume of the cone of a source. + * The volume between inner and outer angle is interpolated between inner + * volume and this value. + * \param handle The handle of the source. + * \return The outer volume of the cone. + */ + virtual float getConeVolumeOuter(AUD_Handle* handle)=0; + + /** + * Sets the outer volume of the cone of a source. + * The volume between inner and outer angle is interpolated between inner + * volume and this value. + * \param handle The handle of the source. + * \param volume The new outer volume of the cone. + * \return Whether the action succeeded. + */ + virtual bool setConeVolumeOuter(AUD_Handle* handle, float volume)=0; +}; + +#endif //AUD_I3DDEVICE diff --git a/intern/audaspace/intern/AUD_IDevice.h b/intern/audaspace/intern/AUD_IDevice.h new file mode 100644 index 00000000000..d0925f6f647 --- /dev/null +++ b/intern/audaspace/intern/AUD_IDevice.h @@ -0,0 +1,244 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_IDEVICE +#define AUD_IDEVICE + +#include "AUD_Space.h" +class AUD_IFactory; + +/// Handle structure, for inherition. +struct AUD_Handle +{ +}; + +typedef void (*stopCallback)(void*); + +/** + * This class represents an output device for sound sources. + * Output devices may be several backends such as plattform independand like + * SDL or OpenAL or plattform specific like DirectSound, but they may also be + * files, RAM buffers or other types of streams. + * \warning Thread safety must be insured so that no reader is beeing called + * twice at the same time. + */ +class AUD_IDevice +{ +public: + /** + * Destroys the device. + */ + virtual ~AUD_IDevice() {} + + /** + * Returns the specification of the device. + */ + virtual AUD_DeviceSpecs getSpecs() const=0; + + /** + * Plays a sound source. + * \param factory The factory to create the reader for the sound source. + * \param keep When keep is true the sound source will not be deleted but + * set to paused when its end has been reached. + * \return Returns a handle with which the playback can be controlled. + * This is NULL if the sound couldn't be played back. + * \exception AUD_Exception Thrown if there's an unexpected (from the + * device side) error during creation of the reader. + */ + virtual AUD_Handle* play(AUD_IFactory* factory, bool keep = false)=0; + + /** + * Pauses a played back sound. + * \param handle The handle returned by the play function. + * \return + * - true if the sound has been paused. + * - false if the sound isn't playing back or the handle is invalid. + */ + virtual bool pause(AUD_Handle* handle)=0; + + /** + * Resumes a paused sound. + * \param handle The handle returned by the play function. + * \return + * - true if the sound has been resumed. + * - false if the sound isn't paused or the handle is invalid. + */ + virtual bool resume(AUD_Handle* handle)=0; + + /** + * Stops a played back or paused sound. The handle is definitely invalid + * afterwards. + * \param handle The handle returned by the play function. + * \return + * - true if the sound has been stopped. + * - false if the handle is invalid. + */ + virtual bool stop(AUD_Handle* handle)=0; + + /** + * Gets the behaviour of the device for a played back sound when the sound + * doesn't return any more samples. + * \param handle The handle returned by the play function. + * \return + * - true if the source will be paused when it's end is reached + * - false if the handle won't kept or is invalid. + */ + virtual bool getKeep(AUD_Handle* handle)=0; + + /** + * Sets the behaviour of the device for a played back sound when the sound + * doesn't return any more samples. + * \param handle The handle returned by the play function. + * \param keep True when the source should be paused and not deleted. + * \return + * - true if the behaviour has been changed. + * - false if the handle is invalid. + */ + virtual bool setKeep(AUD_Handle* handle, bool keep)=0; + + /** + * Seeks in a played back sound. + * \param handle The handle returned by the play function. + * \param position The new position from where to play back, in seconds. + * \return + * - true if the handle is valid. + * - false if the handle is invalid. + * \warning Whether the seek works or not depends on the sound source. + */ + virtual bool seek(AUD_Handle* handle, float position)=0; + + /** + * Retrieves the current playback position of a sound. + * \param handle The handle returned by the play function. + * \return The playback position in seconds, or 0.0 if the handle is + * invalid. + */ + virtual float getPosition(AUD_Handle* handle)=0; + + /** + * Returns the status of a played back sound. + * \param handle The handle returned by the play function. + * \return + * - AUD_STATUS_INVALID if the sound has stopped or the handle is + *. invalid + * - AUD_STATUS_PLAYING if the sound is currently played back. + * - AUD_STATUS_PAUSED if the sound is currently paused. + * \see AUD_Status + */ + virtual AUD_Status getStatus(AUD_Handle* handle)=0; + + /** + * Locks the device. + * Used to make sure that between lock and unlock, no buffers are read, so + * that it is possible to start, resume, pause, stop or seek several + * playback handles simultaneously. + * \warning Make sure the locking time is as small as possible to avoid + * playback delays that result in unexpected noise and cracks. + */ + virtual void lock()=0; + + /** + * Unlocks the previously locked device. + */ + virtual void unlock()=0; + + /** + * Retrieves the overall device volume. + * \return The overall device volume. + */ + virtual float getVolume() const=0; + + /** + * Sets the overall device volume. + * \param handle The sound handle. + * \param volume The overall device volume. + */ + virtual void setVolume(float volume)=0; + + /** + * Retrieves the volume of a playing sound. + * \param handle The sound handle. + * \return The volume. + */ + virtual float getVolume(AUD_Handle* handle)=0; + + /** + * Sets the volume of a playing sound. + * \param handle The sound handle. + * \param volume The volume. + * \return + * - true if the handle is valid. + * - false if the handle is invalid. + */ + virtual bool setVolume(AUD_Handle* handle, float volume)=0; + + /** + * Retrieves the pitch of a playing sound. + * \return The pitch. + */ + virtual float getPitch(AUD_Handle* handle)=0; + + /** + * Sets the pitch of a playing sound. + * \param handle The sound handle. + * \param pitch The pitch. + * \return + * - true if the handle is valid. + * - false if the handle is invalid. + */ + virtual bool setPitch(AUD_Handle* handle, float pitch)=0; + + /** + * Retrieves the loop count of a playing sound. + * A negative value indicates infinity. + * \return The remaining loop count. + */ + virtual int getLoopCount(AUD_Handle* handle)=0; + + /** + * Sets the loop count of a playing sound. + * A negative value indicates infinity. + * \param handle The sound handle. + * \param count The new loop count. + * \return + * - true if the handle is valid. + * - false if the handle is invalid. + */ + virtual bool setLoopCount(AUD_Handle* handle, int count)=0; + + /** + * Sets the callback function that's called when the end of a playing sound + * is reached. + * \param handle The sound handle. + * \param callback The callback function. + * \param data The data that should be passed to the callback function. + * \return + * - true if the handle is valid. + * - false if the handle is invalid. + */ + virtual bool setStopCallback(AUD_Handle* handle, stopCallback callback = 0, void* data = 0)=0; +}; + +#endif //AUD_IDevice diff --git a/intern/audaspace/intern/AUD_IFactory.h b/intern/audaspace/intern/AUD_IFactory.h new file mode 100644 index 00000000000..aed53cef749 --- /dev/null +++ b/intern/audaspace/intern/AUD_IFactory.h @@ -0,0 +1,55 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_IFACTORY +#define AUD_IFACTORY + +#include "AUD_Space.h" +class AUD_IReader; + +/** + * This class represents a type of sound source and saves the necessary values + * for it. It is able to create a reader that is actually usable for playback + * of the respective sound source through the factory method createReader. + */ +class AUD_IFactory +{ +public: + /** + * Destroys the factory. + */ + virtual ~AUD_IFactory(){} + + /** + * Creates a reader for playback of the sound source. + * \return A pointer to an AUD_IReader object or NULL if there has been an + * error. + * \exception AUD_Exception An exception may be thrown if there has been + * a more unexpected error during reader creation. + */ + virtual AUD_IReader* createReader() const=0; +}; + +#endif //AUD_IFACTORY diff --git a/intern/audaspace/intern/AUD_IReader.h b/intern/audaspace/intern/AUD_IReader.h new file mode 100644 index 00000000000..816abfea7f3 --- /dev/null +++ b/intern/audaspace/intern/AUD_IReader.h @@ -0,0 +1,100 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_IREADER +#define AUD_IREADER + +#include "AUD_Space.h" + +/** + * This class represents a sound source as stream or as buffer which can be read + * for example by another reader, a device or whatever. + */ +class AUD_IReader +{ +public: + /** + * Destroys the reader. + */ + virtual ~AUD_IReader(){} + + /** + * Tells whether the source provides seeking functionality or not. + * \warning This doesn't mean that the seeking always has to succeed. + * \return Always returns true for readers of the buffer type. + * \see getType + */ + virtual bool isSeekable() const=0; + + /** + * Seeks to a specific position in the source. + * This function must work for buffer type readers. + * \param position The position to seek for measured in samples. To get + * from a given time to the samples you simply have to multiply the + * time value in seconds with the sample rate of the reader. + * \warning This may work or not, depending on the actual reader. + * \see getType + */ + virtual void seek(int position)=0; + + /** + * Returns an approximated length of the source in samples. + * For readers of the type buffer this has to return a correct value! + * \return The length as sample count. May be negative if unknown. + * \see getType + */ + virtual int getLength() const=0; + + /** + * Returns the position of the source as a sample count value. + * \return The current position in the source. A negative value indicates + * that the position is unknown. + * \warning The value returned doesn't always have to be correct for readers + * of the stream type, especially after seeking, it must though for + * the buffer ones. + * \see getType + */ + virtual int getPosition() const=0; + + /** + * Returns the specification of the reader. + * \return The AUD_Specs structure. + */ + virtual AUD_Specs getSpecs() const=0; + + /** + * Request to read the next length samples out of the source. + * The buffer for reading has to stay valid until the next call of this + * method or until the reader is deleted. + * \param[in,out] length The count of samples that should be read. Shall + * contain the real count of samples after reading, in case + * there were only fewer samples available. + * A smaller value also indicates the end of the reader. + * \param[out] buffer The pointer to the buffer with the samples. + */ + virtual void read(int & length, sample_t* & buffer)=0; +}; + +#endif //AUD_IREADER diff --git a/intern/audaspace/intern/AUD_LinearResampleFactory.cpp b/intern/audaspace/intern/AUD_LinearResampleFactory.cpp new file mode 100644 index 00000000000..91414c6a392 --- /dev/null +++ b/intern/audaspace/intern/AUD_LinearResampleFactory.cpp @@ -0,0 +1,43 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LinearResampleFactory.h" +#include "AUD_LinearResampleReader.h" + +AUD_LinearResampleFactory::AUD_LinearResampleFactory(AUD_IFactory* factory, + AUD_DeviceSpecs specs) : + AUD_ResampleFactory(factory, specs) +{ +} + +AUD_IReader* AUD_LinearResampleFactory::createReader() const +{ + AUD_IReader* reader = getReader(); + + if(reader->getSpecs().rate != m_specs.rate) + reader = new AUD_LinearResampleReader(reader, m_specs.specs); + + return reader; +} diff --git a/intern/audaspace/intern/AUD_LinearResampleFactory.h b/intern/audaspace/intern/AUD_LinearResampleFactory.h new file mode 100644 index 00000000000..426641f6099 --- /dev/null +++ b/intern/audaspace/intern/AUD_LinearResampleFactory.h @@ -0,0 +1,47 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LINEARRESAMPLEFACTORY +#define AUD_LINEARRESAMPLEFACTORY + +#include "AUD_ResampleFactory.h" + +/** + * This factory creates a resampling reader that does simple linear resampling. + */ +class AUD_LinearResampleFactory : public AUD_ResampleFactory +{ +private: + // hide copy constructor and operator= + AUD_LinearResampleFactory(const AUD_LinearResampleFactory&); + AUD_LinearResampleFactory& operator=(const AUD_LinearResampleFactory&); + +public: + AUD_LinearResampleFactory(AUD_IFactory* factory, AUD_DeviceSpecs specs); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_LINEARRESAMPLEFACTORY diff --git a/intern/audaspace/intern/AUD_LinearResampleReader.cpp b/intern/audaspace/intern/AUD_LinearResampleReader.cpp new file mode 100644 index 00000000000..cfe7561d336 --- /dev/null +++ b/intern/audaspace/intern/AUD_LinearResampleReader.cpp @@ -0,0 +1,122 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_LinearResampleReader.h" + +#include +#include + +#define CC channels + channel + +AUD_LinearResampleReader::AUD_LinearResampleReader(AUD_IReader* reader, + AUD_Specs specs) : + AUD_EffectReader(reader), + m_sspecs(reader->getSpecs()), + m_factor(float(specs.rate) / float(m_sspecs.rate)), + m_tspecs(specs), + m_position(0), + m_sposition(0) +{ + m_tspecs.channels = m_sspecs.channels; + m_cache.resize(2 * AUD_SAMPLE_SIZE(m_tspecs)); +} + +void AUD_LinearResampleReader::seek(int position) +{ + m_position = position; + m_sposition = floor(position / m_factor); + m_reader->seek(m_sposition); +} + +int AUD_LinearResampleReader::getLength() const +{ + return m_reader->getLength() * m_factor; +} + +int AUD_LinearResampleReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_LinearResampleReader::getSpecs() const +{ + return m_tspecs; +} + +void AUD_LinearResampleReader::read(int & length, sample_t* & buffer) +{ + int samplesize = AUD_SAMPLE_SIZE(m_tspecs); + int size = length * samplesize; + + if(m_buffer.getSize() < size) + m_buffer.resize(size); + + int need = ceil((m_position + length) / m_factor) + 1 - m_sposition; + int len = need; + sample_t* buf; + buffer = m_buffer.getBuffer(); + + m_reader->read(len, buf); + + if(len < need) + length = floor((m_sposition + len - 1) * m_factor) - m_position; + + float spos; + sample_t low, high; + int channels = m_sspecs.channels; + + for(int channel = 0; channel < channels; channel++) + { + for(int i = 0; i < length; i++) + { + spos = (m_position + i) / m_factor - m_sposition; + + if(floor(spos) < 0) + { + low = m_cache.getBuffer()[(int)(floor(spos) + 2) * CC]; + if(ceil(spos) < 0) + high = m_cache.getBuffer()[(int)(ceil(spos) + 2) * CC]; + else + high = buf[(int)ceil(spos) * CC]; + } + else + { + low = buf[(int)floor(spos) * CC]; + high = buf[(int)ceil(spos) * CC]; + } + buffer[i * CC] = low + (spos - floor(spos)) * (high - low); + } + } + + if(len > 1) + memcpy(m_cache.getBuffer(), + buf + (len - 2) * channels, + 2 * samplesize); + else if(len == 1) + memcpy(m_cache.getBuffer() + 1 * channels, buf, samplesize); + + m_sposition += len; + m_position += length; +} diff --git a/intern/audaspace/intern/AUD_LinearResampleReader.h b/intern/audaspace/intern/AUD_LinearResampleReader.h new file mode 100644 index 00000000000..fbf56286857 --- /dev/null +++ b/intern/audaspace/intern/AUD_LinearResampleReader.h @@ -0,0 +1,92 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_LINEARRESAMPLEREADER +#define AUD_LINEARRESAMPLEREADER + +#include "AUD_EffectReader.h" +#include "AUD_Buffer.h" + +/** + * This resampling reader uses libsamplerate for resampling. + */ +class AUD_LinearResampleReader : public AUD_EffectReader +{ +private: + /** + * The sample specification of the source. + */ + const AUD_Specs m_sspecs; + + /** + * The resampling factor. + */ + const float m_factor; + + /** + * The target specification. + */ + AUD_Specs m_tspecs; + + /** + * The current position. + */ + int m_position; + + /** + * The current reading source position. + */ + int m_sposition; + + /** + * The sound output buffer. + */ + AUD_Buffer m_buffer; + + /** + * The input caching buffer. + */ + AUD_Buffer m_cache; + + // hide copy constructor and operator= + AUD_LinearResampleReader(const AUD_LinearResampleReader&); + AUD_LinearResampleReader& operator=(const AUD_LinearResampleReader&); + +public: + /** + * Creates a resampling reader. + * \param reader The reader to mix. + * \param specs The target specification. + */ + AUD_LinearResampleReader(AUD_IReader* reader, AUD_Specs specs); + + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_LINEARRESAMPLEREADER diff --git a/intern/audaspace/intern/AUD_Mixer.cpp b/intern/audaspace/intern/AUD_Mixer.cpp new file mode 100644 index 00000000000..419ac3af6ac --- /dev/null +++ b/intern/audaspace/intern/AUD_Mixer.cpp @@ -0,0 +1,109 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_Mixer.h" +#include "AUD_IReader.h" + +#include + +AUD_Mixer::AUD_Mixer(AUD_DeviceSpecs specs) : + m_specs(specs) +{ + int bigendian = 1; + bigendian = (((char*)&bigendian)[0]) ? 0: 1; // 1 if Big Endian + + switch(m_specs.format) + { + case AUD_FORMAT_U8: + m_convert = AUD_convert_float_u8; + break; + case AUD_FORMAT_S16: + m_convert = AUD_convert_float_s16; + break; + case AUD_FORMAT_S24: + if(bigendian) + m_convert = AUD_convert_float_s24_be; + else + m_convert = AUD_convert_float_s24_le; + break; + case AUD_FORMAT_S32: + m_convert = AUD_convert_float_s32; + break; + case AUD_FORMAT_FLOAT32: + m_convert = AUD_convert_copy; + break; + case AUD_FORMAT_FLOAT64: + m_convert = AUD_convert_float_double; + break; + default: + break; + } +} + +AUD_DeviceSpecs AUD_Mixer::getSpecs() const +{ + return m_specs; +} + +void AUD_Mixer::add(sample_t* buffer, int start, int length, float volume) +{ + AUD_MixerBuffer buf; + buf.buffer = buffer; + buf.start = start; + buf.length = length; + buf.volume = volume; + m_buffers.push_back(buf); +} + +void AUD_Mixer::superpose(data_t* buffer, int length, float volume) +{ + AUD_MixerBuffer buf; + + int channels = m_specs.channels; + + if(m_buffer.getSize() < length * channels * 4) + m_buffer.resize(length * channels * 4); + + sample_t* out = m_buffer.getBuffer(); + sample_t* in; + + memset(out, 0, length * channels * 4); + + int end; + + while(!m_buffers.empty()) + { + buf = m_buffers.front(); + m_buffers.pop_front(); + + end = buf.length * channels; + in = buf.buffer; + + for(int i = 0; i < end; i++) + out[i + buf.start * channels] += in[i] * buf.volume * volume; + } + + m_convert(buffer, (data_t*) out, length * channels); +} diff --git a/intern/audaspace/intern/AUD_Mixer.h b/intern/audaspace/intern/AUD_Mixer.h new file mode 100644 index 00000000000..a7f5fb274f0 --- /dev/null +++ b/intern/audaspace/intern/AUD_Mixer.h @@ -0,0 +1,111 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_MIXER +#define AUD_MIXER + +#include "AUD_ConverterFunctions.h" +#include "AUD_Buffer.h" +class AUD_IReader; +#include + +struct AUD_MixerBuffer +{ + sample_t* buffer; + int start; + int length; + float volume; +}; + +/** + * This abstract class is able to mix audiosignals of different channel count + * and sample rate and convert it to a specific output format. + */ +class AUD_Mixer +{ +protected: + /** + * The list of buffers to superpose. + */ + std::list m_buffers; + + /** + * The output specification. + */ + const AUD_DeviceSpecs m_specs; + + /** + * The temporary mixing buffer. + */ + AUD_Buffer m_buffer; + + /** + * Converter function. + */ + AUD_convert_f m_convert; + +public: + /** + * Creates the mixer. + */ + AUD_Mixer(AUD_DeviceSpecs specs); + + /** + * Destroys the mixer. + */ + virtual ~AUD_Mixer() {} + + /** + * Returns the target specification for superposing. + * \return The target specification. + */ + AUD_DeviceSpecs getSpecs() const; + + /** + * This funuction prepares a reader for playback. + * \param reader The reader to prepare. + * \return The reader that should be used for playback. + */ + virtual AUD_IReader* prepare(AUD_IReader* reader)=0; + + /** + * Adds a buffer for superposition. + * \param buffer The buffer to superpose. + * \param start The start sample of the buffer. + * \param length The length of the buffer in samples. + * \param volume The mixing volume. Must be a value between 0.0 and 1.0. + */ + virtual void add(sample_t* buffer, int start, int length, float volume); + + /** + * Superposes all added buffers into an output buffer. + * \param buffer The target buffer for superposing. + * \param length The length of the buffer in samples. + * \param volume The mixing volume. Must be a value between 0.0 and 1.0. + */ + virtual void superpose(data_t* buffer, int length, float volume); +}; + +#endif //AUD_MIXER diff --git a/intern/audaspace/intern/AUD_MixerFactory.cpp b/intern/audaspace/intern/AUD_MixerFactory.cpp new file mode 100644 index 00000000000..4370bed6ca6 --- /dev/null +++ b/intern/audaspace/intern/AUD_MixerFactory.cpp @@ -0,0 +1,48 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_MixerFactory.h" +#include "AUD_IReader.h" + +AUD_IReader* AUD_MixerFactory::getReader() const +{ + return m_factory->createReader(); +} + +AUD_MixerFactory::AUD_MixerFactory(AUD_IFactory* factory, + AUD_DeviceSpecs specs) : + m_specs(specs), m_factory(factory) +{ +} + +AUD_DeviceSpecs AUD_MixerFactory::getSpecs() const +{ + return m_specs; +} + +AUD_IFactory* AUD_MixerFactory::getFactory() const +{ + return m_factory; +} diff --git a/intern/audaspace/intern/AUD_MixerFactory.h b/intern/audaspace/intern/AUD_MixerFactory.h new file mode 100644 index 00000000000..909eca8c148 --- /dev/null +++ b/intern/audaspace/intern/AUD_MixerFactory.h @@ -0,0 +1,75 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_MIXERFACTORY +#define AUD_MIXERFACTORY + +#include "AUD_IFactory.h" + +/** + * This factory is a base class for all mixer factories. + */ +class AUD_MixerFactory : public AUD_IFactory +{ +protected: + /** + * The target specification for resampling. + */ + const AUD_DeviceSpecs m_specs; + + /** + * If there is no reader it is created out of this factory. + */ + AUD_IFactory* m_factory; + + /** + * Returns the reader created out of the factory. + * This method can be used for the createReader function of the implementing + * classes. + * \return The reader to mix. + */ + AUD_IReader* getReader() const; + +public: + /** + * Creates a new factory. + * \param factory The factory to create the readers to mix out of. + * \param specs The target specification. + */ + AUD_MixerFactory(AUD_IFactory* factory, AUD_DeviceSpecs specs); + + /** + * Returns the target specification for resampling. + */ + AUD_DeviceSpecs getSpecs() const; + + /** + * Returns the saved factory. + * \return The factory. + */ + AUD_IFactory* getFactory() const; +}; + +#endif //AUD_MIXERFACTORY diff --git a/intern/audaspace/intern/AUD_NULLDevice.cpp b/intern/audaspace/intern/AUD_NULLDevice.cpp new file mode 100644 index 00000000000..272e1e4b5b2 --- /dev/null +++ b/intern/audaspace/intern/AUD_NULLDevice.cpp @@ -0,0 +1,140 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include + +#include "AUD_NULLDevice.h" +#include "AUD_IReader.h" +#include "AUD_IFactory.h" + +AUD_NULLDevice::AUD_NULLDevice() +{ +} + +AUD_DeviceSpecs AUD_NULLDevice::getSpecs() const +{ + AUD_DeviceSpecs specs; + specs.channels = AUD_CHANNELS_INVALID; + specs.format = AUD_FORMAT_INVALID; + specs.rate = AUD_RATE_INVALID; + return specs; +} + +AUD_Handle* AUD_NULLDevice::play(AUD_IFactory* factory, bool keep) +{ + return 0; +} + +bool AUD_NULLDevice::pause(AUD_Handle* handle) +{ + return false; +} + +bool AUD_NULLDevice::resume(AUD_Handle* handle) +{ + return false; +} + +bool AUD_NULLDevice::stop(AUD_Handle* handle) +{ + return false; +} + +bool AUD_NULLDevice::getKeep(AUD_Handle* handle) +{ + return false; +} + +bool AUD_NULLDevice::setKeep(AUD_Handle* handle, bool keep) +{ + return false; +} + +bool AUD_NULLDevice::seek(AUD_Handle* handle, float position) +{ + return false; +} + +float AUD_NULLDevice::getPosition(AUD_Handle* handle) +{ + return std::numeric_limits::quiet_NaN(); +} + +AUD_Status AUD_NULLDevice::getStatus(AUD_Handle* handle) +{ + return AUD_STATUS_INVALID; +} + +void AUD_NULLDevice::lock() +{ +} + +void AUD_NULLDevice::unlock() +{ +} + +float AUD_NULLDevice::getVolume() const +{ + return 0; +} + +void AUD_NULLDevice::setVolume(float volume) +{ +} + +float AUD_NULLDevice::getVolume(AUD_Handle* handle) +{ + return std::numeric_limits::quiet_NaN(); +} + +bool AUD_NULLDevice::setVolume(AUD_Handle* handle, float volume) +{ + return false; +} + +float AUD_NULLDevice::getPitch(AUD_Handle* handle) +{ + return std::numeric_limits::quiet_NaN(); +} + +bool AUD_NULLDevice::setPitch(AUD_Handle* handle, float pitch) +{ + return false; +} + +int AUD_NULLDevice::getLoopCount(AUD_Handle* handle) +{ + return 0; +} + +bool AUD_NULLDevice::setLoopCount(AUD_Handle* handle, int count) +{ + return false; +} + +bool AUD_NULLDevice::setStopCallback(AUD_Handle* handle, stopCallback callback, void* data) +{ + return false; +} diff --git a/intern/audaspace/intern/AUD_NULLDevice.h b/intern/audaspace/intern/AUD_NULLDevice.h new file mode 100644 index 00000000000..a1ffdba201a --- /dev/null +++ b/intern/audaspace/intern/AUD_NULLDevice.h @@ -0,0 +1,65 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_NULLDEVICE +#define AUD_NULLDEVICE + +#include "AUD_IDevice.h" + +/** + * This device plays nothing. + */ +class AUD_NULLDevice : public AUD_IDevice +{ +public: + /** + * Creates a new NULL device. + */ + AUD_NULLDevice(); + + virtual AUD_DeviceSpecs getSpecs() const; + virtual AUD_Handle* play(AUD_IFactory* factory, bool keep = false); + virtual bool pause(AUD_Handle* handle); + virtual bool resume(AUD_Handle* handle); + virtual bool stop(AUD_Handle* handle); + virtual bool getKeep(AUD_Handle* handle); + virtual bool setKeep(AUD_Handle* handle, bool keep); + virtual bool seek(AUD_Handle* handle, float position); + virtual float getPosition(AUD_Handle* handle); + virtual AUD_Status getStatus(AUD_Handle* handle); + virtual void lock(); + virtual void unlock(); + virtual float getVolume() const; + virtual void setVolume(float volume); + virtual float getVolume(AUD_Handle* handle); + virtual bool setVolume(AUD_Handle* handle, float volume); + virtual float getPitch(AUD_Handle* handle); + virtual bool setPitch(AUD_Handle* handle, float pitch); + virtual int getLoopCount(AUD_Handle* handle); + virtual bool setLoopCount(AUD_Handle* handle, int count); + virtual bool setStopCallback(AUD_Handle* handle, stopCallback callback = 0, void* data = 0); +}; + +#endif //AUD_NULLDEVICE diff --git a/intern/audaspace/intern/AUD_PyInit.h b/intern/audaspace/intern/AUD_PyInit.h new file mode 100644 index 00000000000..bd67259449a --- /dev/null +++ b/intern/audaspace/intern/AUD_PyInit.h @@ -0,0 +1,47 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2010 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_PYINIT +#define AUD_PYINIT + +#ifndef DISABLE_PYTHON +#include "Python.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Initalizes the Python module. + */ +extern PyObject* AUD_initPython(); + +#ifdef __cplusplus +} +#endif + +#endif + +#endif //AUD_PYINIT diff --git a/intern/audaspace/intern/AUD_ReadDevice.cpp b/intern/audaspace/intern/AUD_ReadDevice.cpp new file mode 100644 index 00000000000..cde5694354e --- /dev/null +++ b/intern/audaspace/intern/AUD_ReadDevice.cpp @@ -0,0 +1,60 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_DefaultMixer.h" +#include "AUD_ReadDevice.h" +#include "AUD_IReader.h" + +#include + +AUD_ReadDevice::AUD_ReadDevice(AUD_DeviceSpecs specs) : + m_playing(false) +{ + m_specs = specs; + + create(); +} + +AUD_ReadDevice::~AUD_ReadDevice() +{ + destroy(); +} + +bool AUD_ReadDevice::read(data_t* buffer, int length) +{ + if(m_playing) + mix(buffer, length); + else + if(m_specs.format == AUD_FORMAT_U8) + memset(buffer, 0x80, length * AUD_DEVICE_SAMPLE_SIZE(m_specs)); + else + memset(buffer, 0, length * AUD_DEVICE_SAMPLE_SIZE(m_specs)); + return m_playing; +} + +void AUD_ReadDevice::playing(bool playing) +{ + m_playing = playing; +} diff --git a/intern/audaspace/intern/AUD_ReadDevice.h b/intern/audaspace/intern/AUD_ReadDevice.h new file mode 100644 index 00000000000..121bcb8612b --- /dev/null +++ b/intern/audaspace/intern/AUD_ReadDevice.h @@ -0,0 +1,72 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_READDEVICE +#define AUD_READDEVICE + +#include "AUD_SoftwareDevice.h" + +/** + * This device enables to let the user read raw data out of it. + */ +class AUD_ReadDevice : public AUD_SoftwareDevice +{ +private: + /** + * Whether the device currently. + */ + bool m_playing; + + // hide copy constructor and operator= + AUD_ReadDevice(const AUD_ReadDevice&); + AUD_ReadDevice& operator=(const AUD_ReadDevice&); + +protected: + virtual void playing(bool playing); + +public: + /** + * Creates a new read device. + * \param specs The wanted audio specification. + */ + AUD_ReadDevice(AUD_DeviceSpecs specs); + + /** + * Closes the device. + */ + virtual ~AUD_ReadDevice(); + + /** + * Reads the next bytes into the supplied buffer. + * \param buffer The target buffer. + * \param length The length in samples to be filled. + * \return True if the reading succeeded, false if there are no sounds + * played back currently, in that case the buffer is filled with + * silence. + */ + bool read(data_t* buffer, int length); +}; + +#endif //AUD_READDEVICE diff --git a/intern/audaspace/intern/AUD_Reference.h b/intern/audaspace/intern/AUD_Reference.h new file mode 100644 index 00000000000..6b1001e2b01 --- /dev/null +++ b/intern/audaspace/intern/AUD_Reference.h @@ -0,0 +1,115 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_REFERENCE +#define AUD_REFERENCE + +template +/** + * This class provides reference counting functionality. + */ +class AUD_Reference +{ +private: + /// The reference. + T* m_reference; + /// The reference counter. + int* m_refcount; +public: + /** + * Creates a new reference counter. + * \param reference The reference. + */ + AUD_Reference(T* reference = 0) + { + m_reference = reference; + m_refcount = new int; + *m_refcount = 1; + } + + /** + * Copies an AUD_Reference object. + * \param ref The AUD_Reference object to copy. + */ + AUD_Reference(const AUD_Reference& ref) + { + m_reference = ref.m_reference; + m_refcount = ref.m_refcount; + (*m_refcount)++; + } + + /** + * Destroys a AUD_Reference object, if there's no furthere reference on the + * reference, it is destroyed as well. + */ + ~AUD_Reference() + { + (*m_refcount)--; + if(*m_refcount == 0) + { + if(m_reference) + { + delete m_reference; + } + delete m_refcount; + } + } + + /** + * Assigns an AUD_Reference to this object. + * \param ref The AUD_Reference object to assign. + */ + AUD_Reference& operator=(const AUD_Reference& ref) + { + if(&ref == this) + return *this; + + (*m_refcount)--; + if(*m_refcount == 0) + { + if(m_reference) + { + delete m_reference; + } + delete m_refcount; + } + + m_reference = ref.m_reference; + m_refcount = ref.m_refcount; + (*m_refcount)++; + + return *this; + } + + /** + * Returns the reference. + */ + T* get() const + { + return m_reference; + } +}; + +#endif // AUD_REFERENCE diff --git a/intern/audaspace/intern/AUD_ResampleFactory.h b/intern/audaspace/intern/AUD_ResampleFactory.h new file mode 100644 index 00000000000..5493e1005b7 --- /dev/null +++ b/intern/audaspace/intern/AUD_ResampleFactory.h @@ -0,0 +1,33 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_RESAMPLEFACTORY +#define AUD_RESAMPLEFACTORY + +#include "AUD_MixerFactory.h" + +typedef AUD_MixerFactory AUD_ResampleFactory; + +#endif //AUD_RESAMPLEFACTORY diff --git a/intern/audaspace/intern/AUD_SequencerFactory.cpp b/intern/audaspace/intern/AUD_SequencerFactory.cpp new file mode 100644 index 00000000000..05c7fefea4a --- /dev/null +++ b/intern/audaspace/intern/AUD_SequencerFactory.cpp @@ -0,0 +1,117 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SequencerFactory.h" +#include "AUD_SequencerReader.h" + +typedef std::list::iterator AUD_ReaderIterator; + +AUD_SequencerFactory::AUD_SequencerFactory(AUD_Specs specs, void* data, + AUD_volumeFunction volume) : + m_specs(specs), + m_data(data), + m_volume(volume) +{ +} + +AUD_SequencerFactory::~AUD_SequencerFactory() +{ + AUD_SequencerReader* reader; + AUD_SequencerEntry* entry; + + while(!m_readers.empty()) + { + reader = m_readers.front(); + m_readers.pop_front(); + reader->destroy(); + } + + while(!m_entries.empty()) + { + entry = m_entries.front(); + m_entries.pop_front(); + delete entry; + } +} + +AUD_IReader* AUD_SequencerFactory::newReader() +{ + AUD_SequencerReader* reader = new AUD_SequencerReader(this, m_entries, + m_specs, m_data, + m_volume); + m_readers.push_front(reader); + + return reader; +} + +AUD_SequencerEntry* AUD_SequencerFactory::add(AUD_IFactory** sound, float begin, float end, float skip, void* data) +{ + AUD_SequencerEntry* entry = new AUD_SequencerEntry; + entry->sound = sound; + entry->begin = begin; + entry->skip = skip; + entry->end = end; + entry->muted = false; + entry->data = data; + + m_entries.push_front(entry); + + for(AUD_ReaderIterator i = m_readers.begin(); i != m_readers.end(); i++) + (*i)->add(entry); + + return entry; +} + +void AUD_SequencerFactory::remove(AUD_SequencerEntry* entry) +{ + for(AUD_ReaderIterator i = m_readers.begin(); i != m_readers.end(); i++) + (*i)->remove(entry); + + m_entries.remove(entry); + + delete entry; +} + +void AUD_SequencerFactory::move(AUD_SequencerEntry* entry, float begin, float end, float skip) +{ + entry->begin = begin; + entry->skip = skip; + entry->end = end; +} + +void AUD_SequencerFactory::mute(AUD_SequencerEntry* entry, bool mute) +{ + entry->muted = mute; +} + +AUD_IReader* AUD_SequencerFactory::createReader() const +{ + return const_cast(this)->newReader(); +} + +void AUD_SequencerFactory::removeReader(AUD_SequencerReader* reader) +{ + m_readers.remove(reader); +} diff --git a/intern/audaspace/intern/AUD_SequencerFactory.h b/intern/audaspace/intern/AUD_SequencerFactory.h new file mode 100644 index 00000000000..a2c8f48fa54 --- /dev/null +++ b/intern/audaspace/intern/AUD_SequencerFactory.h @@ -0,0 +1,83 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SEQUENCERFACTORY +#define AUD_SEQUENCERFACTORY + +#include "AUD_IFactory.h" + +#include + +typedef float (*AUD_volumeFunction)(void*, void*, float); + +struct AUD_SequencerEntry +{ + AUD_IFactory** sound; + float begin; + float end; + float skip; + bool muted; + void* data; +}; + +class AUD_SequencerReader; + +/** + * This factory creates a resampling reader that does simple linear resampling. + */ +class AUD_SequencerFactory : public AUD_IFactory +{ +private: + /** + * The target specification. + */ + AUD_Specs m_specs; + + std::list m_entries; + std::list m_readers; + void* m_data; + AUD_volumeFunction m_volume; + + AUD_IReader* newReader(); + + // hide copy constructor and operator= + AUD_SequencerFactory(const AUD_SequencerFactory&); + AUD_SequencerFactory& operator=(const AUD_SequencerFactory&); + +public: + AUD_SequencerFactory(AUD_Specs specs, void* data, AUD_volumeFunction volume); + ~AUD_SequencerFactory(); + + AUD_SequencerEntry* add(AUD_IFactory** sound, float begin, float end, float skip, void* data); + void remove(AUD_SequencerEntry* entry); + void move(AUD_SequencerEntry* entry, float begin, float end, float skip); + void mute(AUD_SequencerEntry* entry, bool mute); + + virtual AUD_IReader* createReader() const; + + void removeReader(AUD_SequencerReader* reader); +}; + +#endif //AUD_SEQUENCERFACTORY diff --git a/intern/audaspace/intern/AUD_SequencerReader.cpp b/intern/audaspace/intern/AUD_SequencerReader.cpp new file mode 100644 index 00000000000..69293205498 --- /dev/null +++ b/intern/audaspace/intern/AUD_SequencerReader.cpp @@ -0,0 +1,228 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SequencerReader.h" +#include "AUD_DefaultMixer.h" + +#include + +typedef std::list::iterator AUD_StripIterator; +typedef std::list::iterator AUD_EntryIterator; + +AUD_SequencerReader::AUD_SequencerReader(AUD_SequencerFactory* factory, + std::list &entries, AUD_Specs specs, + void* data, AUD_volumeFunction volume) +{ + AUD_DeviceSpecs dspecs; + dspecs.specs = specs; + dspecs.format = AUD_FORMAT_FLOAT32; + + m_mixer = new AUD_DefaultMixer(dspecs); + m_factory = factory; + m_data = data; + m_volume = volume; + + AUD_SequencerStrip* strip; + + for(AUD_EntryIterator i = entries.begin(); i != entries.end(); i++) + { + strip = new AUD_SequencerStrip; + strip->entry = *i; + strip->old_sound = NULL; + + if(strip->old_sound) + strip->reader = m_mixer->prepare(strip->old_sound->createReader()); + else + strip->reader = NULL; + + m_strips.push_front(strip); + } + + m_position = 0; +} + +AUD_SequencerReader::~AUD_SequencerReader() +{ + if(m_factory != NULL) + m_factory->removeReader(this); + + AUD_SequencerStrip* strip; + + while(!m_strips.empty()) + { + strip = m_strips.front(); + m_strips.pop_front(); + if(strip->reader) + { + delete strip->reader; + } + delete strip; + } + + delete m_mixer; +} + +void AUD_SequencerReader::destroy() +{ + m_factory = NULL; + AUD_SequencerStrip* strip; + + while(!m_strips.empty()) + { + strip = m_strips.front(); + m_strips.pop_front(); + delete strip; + } +} + +void AUD_SequencerReader::add(AUD_SequencerEntry* entry) +{ + AUD_SequencerStrip* strip = new AUD_SequencerStrip; + strip->entry = entry; + + if(*strip->entry->sound) + { + strip->old_sound = *strip->entry->sound; + strip->reader = m_mixer->prepare(strip->old_sound->createReader()); + } + else + { + strip->reader = NULL; + strip->old_sound = NULL; + } + m_strips.push_front(strip); +} + +void AUD_SequencerReader::remove(AUD_SequencerEntry* entry) +{ + AUD_SequencerStrip* strip; + for(AUD_StripIterator i = m_strips.begin(); i != m_strips.end(); i++) + { + strip = *i; + if(strip->entry == entry) + { + i++; + if(strip->reader) + { + delete strip->reader; + } + m_strips.remove(strip); + delete strip; + return; + } + } +} + +bool AUD_SequencerReader::isSeekable() const +{ + return true; +} + +void AUD_SequencerReader::seek(int position) +{ + m_position = position; +} + +int AUD_SequencerReader::getLength() const +{ + return -1; +} + +int AUD_SequencerReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_SequencerReader::getSpecs() const +{ + return m_mixer->getSpecs().specs; +} + +void AUD_SequencerReader::read(int & length, sample_t* & buffer) +{ + AUD_DeviceSpecs specs = m_mixer->getSpecs(); + int samplesize = AUD_SAMPLE_SIZE(specs); + int rate = specs.rate; + + int size = length * samplesize; + + int start, end, current, skip, len; + AUD_SequencerStrip* strip; + sample_t* buf; + + if(m_buffer.getSize() < size) + m_buffer.resize(size); + buffer = m_buffer.getBuffer(); + + for(AUD_StripIterator i = m_strips.begin(); i != m_strips.end(); i++) + { + strip = *i; + if(!strip->entry->muted) + { + if(strip->old_sound != *strip->entry->sound) + { + strip->old_sound = *strip->entry->sound; + if(strip->reader) + delete strip->reader; + + if(strip->old_sound) + strip->reader = m_mixer->prepare(strip->old_sound->createReader()); + else + strip->reader = NULL; + } + + if(strip->reader) + { + end = floor(strip->entry->end * rate); + if(m_position < end) + { + start = floor(strip->entry->begin * rate); + if(m_position + length > start) + { + current = m_position - start; + if(current < 0) + { + skip = -current; + current = 0; + } + else + skip = 0; + current += strip->entry->skip * rate; + len = length > end - m_position ? end - m_position : length; + len -= skip; + if(strip->reader->getPosition() != current) + strip->reader->seek(current); + strip->reader->read(len, buf); + m_mixer->add(buf, skip, len, m_volume(m_data, strip->entry->data, (float)m_position / (float)rate)); + } + } + } + } + } + + m_mixer->superpose((data_t*)buffer, length, 1.0f); + + m_position += length; +} diff --git a/intern/audaspace/intern/AUD_SequencerReader.h b/intern/audaspace/intern/AUD_SequencerReader.h new file mode 100644 index 00000000000..fd7d94c7578 --- /dev/null +++ b/intern/audaspace/intern/AUD_SequencerReader.h @@ -0,0 +1,102 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SEQUENCERREADER +#define AUD_SEQUENCERREADER + +#include "AUD_IReader.h" +#include "AUD_SequencerFactory.h" +#include "AUD_Buffer.h" +class AUD_Mixer; + +struct AUD_SequencerStrip +{ + AUD_IFactory* old_sound; + AUD_IReader* reader; + AUD_SequencerEntry* entry; +}; + +/** + * This resampling reader uses libsamplerate for resampling. + */ +class AUD_SequencerReader : public AUD_IReader +{ +private: + /** + * The current position. + */ + int m_position; + + /** + * The sound output buffer. + */ + AUD_Buffer m_buffer; + + /** + * The target specification. + */ + AUD_Mixer* m_mixer; + + /** + * Saves the SequencerFactory the reader belongs to. + */ + AUD_SequencerFactory* m_factory; + + std::list m_strips; + + void* m_data; + AUD_volumeFunction m_volume; + + // hide copy constructor and operator= + AUD_SequencerReader(const AUD_SequencerReader&); + AUD_SequencerReader& operator=(const AUD_SequencerReader&); + +public: + /** + * Creates a resampling reader. + * \param reader The reader to mix. + * \param specs The target specification. + */ + AUD_SequencerReader(AUD_SequencerFactory* factory, std::list &entries, const AUD_Specs specs, void* data, AUD_volumeFunction volume); + + /** + * Destroys the reader. + */ + ~AUD_SequencerReader(); + + void destroy(); + + void add(AUD_SequencerEntry* entry); + void remove(AUD_SequencerEntry* entry); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_SEQUENCERREADER diff --git a/intern/audaspace/intern/AUD_SilenceFactory.cpp b/intern/audaspace/intern/AUD_SilenceFactory.cpp new file mode 100644 index 00000000000..4e59d7486d5 --- /dev/null +++ b/intern/audaspace/intern/AUD_SilenceFactory.cpp @@ -0,0 +1,37 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SilenceFactory.h" +#include "AUD_SilenceReader.h" +#include "AUD_Space.h" + +AUD_SilenceFactory::AUD_SilenceFactory() +{ +} + +AUD_IReader* AUD_SilenceFactory::createReader() const +{ + return new AUD_SilenceReader(); +} diff --git a/intern/audaspace/intern/AUD_SilenceFactory.h b/intern/audaspace/intern/AUD_SilenceFactory.h new file mode 100644 index 00000000000..bc8a5b92727 --- /dev/null +++ b/intern/audaspace/intern/AUD_SilenceFactory.h @@ -0,0 +1,50 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SILENCEFACTORY +#define AUD_SILENCEFACTORY + +#include "AUD_IFactory.h" + +/** + * This factory creates a reader that plays a sine tone. + */ +class AUD_SilenceFactory : public AUD_IFactory +{ +private: + // hide copy constructor and operator= + AUD_SilenceFactory(const AUD_SilenceFactory&); + AUD_SilenceFactory& operator=(const AUD_SilenceFactory&); + +public: + /** + * Creates a new silence factory. + */ + AUD_SilenceFactory(); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SILENCEFACTORY diff --git a/intern/audaspace/intern/AUD_SilenceReader.cpp b/intern/audaspace/intern/AUD_SilenceReader.cpp new file mode 100644 index 00000000000..5243286c5e4 --- /dev/null +++ b/intern/audaspace/intern/AUD_SilenceReader.cpp @@ -0,0 +1,74 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SilenceReader.h" + +#include + +AUD_SilenceReader::AUD_SilenceReader() : + m_position(0) +{ +} + +bool AUD_SilenceReader::isSeekable() const +{ + return true; +} + +void AUD_SilenceReader::seek(int position) +{ + m_position = position; +} + +int AUD_SilenceReader::getLength() const +{ + return -1; +} + +int AUD_SilenceReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_SilenceReader::getSpecs() const +{ + AUD_Specs specs; + specs.rate = AUD_RATE_44100; + specs.channels = AUD_CHANNELS_MONO; + return specs; +} + +void AUD_SilenceReader::read(int & length, sample_t* & buffer) +{ + // resize if necessary + if(m_buffer.getSize() < length * sizeof(sample_t)) + { + m_buffer.resize(length * sizeof(sample_t)); + memset(m_buffer.getBuffer(), 0, m_buffer.getSize()); + } + + buffer = m_buffer.getBuffer(); + m_position += length; +} diff --git a/intern/audaspace/intern/AUD_SilenceReader.h b/intern/audaspace/intern/AUD_SilenceReader.h new file mode 100644 index 00000000000..a8b959b5309 --- /dev/null +++ b/intern/audaspace/intern/AUD_SilenceReader.h @@ -0,0 +1,71 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SILENCEREADER +#define AUD_SILENCEREADER + +#include "AUD_IReader.h" +#include "AUD_Buffer.h" + +/** + * This class is used for sine tone playback. + * The output format is in the 16 bit format and stereo, the sample rate can be + * specified. + * As the two channels both play the same the output could also be mono, but + * in most cases this will result in having to resample for output, so stereo + * sound is created directly. + */ +class AUD_SilenceReader : public AUD_IReader +{ +private: + /** + * The current position in samples. + */ + int m_position; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + // hide copy constructor and operator= + AUD_SilenceReader(const AUD_SilenceReader&); + AUD_SilenceReader& operator=(const AUD_SilenceReader&); + +public: + /** + * Creates a new reader. + */ + AUD_SilenceReader(); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_SILENCEREADER diff --git a/intern/audaspace/intern/AUD_SinusFactory.cpp b/intern/audaspace/intern/AUD_SinusFactory.cpp new file mode 100644 index 00000000000..3c8e5ccc567 --- /dev/null +++ b/intern/audaspace/intern/AUD_SinusFactory.cpp @@ -0,0 +1,44 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SinusFactory.h" +#include "AUD_SinusReader.h" +#include "AUD_Space.h" + +AUD_SinusFactory::AUD_SinusFactory(float frequency, AUD_SampleRate sampleRate) : + m_frequency(frequency), + m_sampleRate(sampleRate) +{ +} + +float AUD_SinusFactory::getFrequency() const +{ + return m_frequency; +} + +AUD_IReader* AUD_SinusFactory::createReader() const +{ + return new AUD_SinusReader(m_frequency, m_sampleRate); +} diff --git a/intern/audaspace/intern/AUD_SinusFactory.h b/intern/audaspace/intern/AUD_SinusFactory.h new file mode 100644 index 00000000000..2c5965e11f0 --- /dev/null +++ b/intern/audaspace/intern/AUD_SinusFactory.h @@ -0,0 +1,68 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SINUSFACTORY +#define AUD_SINUSFACTORY + +#include "AUD_IFactory.h" + +/** + * This factory creates a reader that plays a sine tone. + */ +class AUD_SinusFactory : public AUD_IFactory +{ +private: + /** + * The frequence of the sine wave. + */ + const float m_frequency; + + /** + * The target sample rate for output. + */ + const AUD_SampleRate m_sampleRate; + + // hide copy constructor and operator= + AUD_SinusFactory(const AUD_SinusFactory&); + AUD_SinusFactory& operator=(const AUD_SinusFactory&); + +public: + /** + * Creates a new sine factory. + * \param frequency The desired frequency. + * \param sampleRate The target sample rate for playback. + */ + AUD_SinusFactory(float frequency, + AUD_SampleRate sampleRate = AUD_RATE_44100); + + /** + * Returns the frequency of the sine wave. + */ + float getFrequency() const; + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SINUSFACTORY diff --git a/intern/audaspace/intern/AUD_SinusReader.cpp b/intern/audaspace/intern/AUD_SinusReader.cpp new file mode 100644 index 00000000000..e810c576a5c --- /dev/null +++ b/intern/audaspace/intern/AUD_SinusReader.cpp @@ -0,0 +1,84 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SinusReader.h" + +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +AUD_SinusReader::AUD_SinusReader(float frequency, AUD_SampleRate sampleRate) : + m_frequency(frequency), + m_position(0), + m_sampleRate(sampleRate) +{ +} + +bool AUD_SinusReader::isSeekable() const +{ + return true; +} + +void AUD_SinusReader::seek(int position) +{ + m_position = position; +} + +int AUD_SinusReader::getLength() const +{ + return -1; +} + +int AUD_SinusReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_SinusReader::getSpecs() const +{ + AUD_Specs specs; + specs.rate = m_sampleRate; + specs.channels = AUD_CHANNELS_MONO; + return specs; +} + +void AUD_SinusReader::read(int & length, sample_t* & buffer) +{ + // resize if necessary + if(m_buffer.getSize() < length * sizeof(sample_t)) + m_buffer.resize(length * sizeof(sample_t)); + + // fill with sine data + buffer = m_buffer.getBuffer(); + for(int i = 0; i < length; i++) + { + buffer[i] = sin((m_position + i) * 2 * M_PI * m_frequency / + (float)m_sampleRate); + } + + m_position += length; +} diff --git a/intern/audaspace/intern/AUD_SinusReader.h b/intern/audaspace/intern/AUD_SinusReader.h new file mode 100644 index 00000000000..4b88ed15db1 --- /dev/null +++ b/intern/audaspace/intern/AUD_SinusReader.h @@ -0,0 +1,83 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SINUSREADER +#define AUD_SINUSREADER + +#include "AUD_IReader.h" +#include "AUD_Buffer.h" + +/** + * This class is used for sine tone playback. + * The output format is in the 16 bit format and stereo, the sample rate can be + * specified. + * As the two channels both play the same the output could also be mono, but + * in most cases this will result in having to resample for output, so stereo + * sound is created directly. + */ +class AUD_SinusReader : public AUD_IReader +{ +private: + /** + * The frequency of the sine wave. + */ + const float m_frequency; + + /** + * The current position in samples. + */ + int m_position; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * The sample rate for the output. + */ + const AUD_SampleRate m_sampleRate; + + // hide copy constructor and operator= + AUD_SinusReader(const AUD_SinusReader&); + AUD_SinusReader& operator=(const AUD_SinusReader&); + +public: + /** + * Creates a new reader. + * \param frequency The frequency of the sine wave. + * \param sampleRate The output sample rate. + */ + AUD_SinusReader(float frequency, AUD_SampleRate sampleRate); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_SINUSREADER diff --git a/intern/audaspace/intern/AUD_SoftwareDevice.cpp b/intern/audaspace/intern/AUD_SoftwareDevice.cpp new file mode 100644 index 00000000000..0c0206608e3 --- /dev/null +++ b/intern/audaspace/intern/AUD_SoftwareDevice.cpp @@ -0,0 +1,512 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SoftwareDevice.h" +#include "AUD_IReader.h" +#include "AUD_DefaultMixer.h" +#include "AUD_IFactory.h" + +#include +#include + +/// Saves the data for playback. +struct AUD_SoftwareHandle : AUD_Handle +{ + /// The reader source. + AUD_IReader* reader; + + /// Whether to keep the source if end of it is reached. + bool keep; + + /// The volume of the source. + float volume; + + /// The loop count of the source. + int loopcount; + + /// The stop callback. + stopCallback stop; + + /// Stop callback data. + void* stop_data; +}; + +typedef std::list::iterator AUD_HandleIterator; + +void AUD_SoftwareDevice::create() +{ + m_playback = false; + m_volume = 1.0f; + m_mixer = new AUD_DefaultMixer(m_specs); + + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + + pthread_mutex_init(&m_mutex, &attr); + + pthread_mutexattr_destroy(&attr); +} + +void AUD_SoftwareDevice::destroy() +{ + if(m_playback) + playing(m_playback = false); + + delete m_mixer; + + AUD_SoftwareHandle* handle; + + // delete all playing sounds + while(!m_playingSounds.empty()) + { + handle = m_playingSounds.front(); + m_playingSounds.pop_front(); + delete handle->reader; + delete handle; + } + + // delete all paused sounds + while(!m_pausedSounds.empty()) + { + handle = m_pausedSounds.front(); + m_pausedSounds.pop_front(); + delete handle->reader; + delete handle; + } + + pthread_mutex_destroy(&m_mutex); +} + +void AUD_SoftwareDevice::mix(data_t* buffer, int length) +{ + lock(); + + { + AUD_SoftwareHandle* sound; + int len; + int pos; + sample_t* buf; + std::list stopSounds; + std::list tempBufs; + AUD_Buffer* tempbuf; + int samplesize = AUD_SAMPLE_SIZE(m_specs); + + // for all sounds + AUD_HandleIterator it = m_playingSounds.begin(); + while(it != m_playingSounds.end()) + { + sound = *it; + // increment the iterator to make sure it's valid, + // in case the sound gets deleted after stopping + ++it; + + // get the buffer from the source + pos = 0; + len = length; + sound->reader->read(len, buf); + + // in case of looping + while(pos + len < length && sound->loopcount) + { + tempbuf = new AUD_Buffer(len * samplesize); + memcpy(tempbuf->getBuffer(), buf, len * samplesize); + tempBufs.push_back(tempbuf); + m_mixer->add(tempbuf->getBuffer(), pos, len, sound->volume); + + pos += len; + + if(sound->loopcount > 0) + sound->loopcount--; + + sound->reader->seek(0); + + len = length - pos; + sound->reader->read(len, buf); + + // prevent endless loop + if(!len) + break; + } + + m_mixer->add(buf, pos, len, sound->volume); + pos += len; + + // in case the end of the sound is reached + if(pos < length) + { + if(sound->stop) + sound->stop(sound->stop_data); + + if(sound->keep) + pause(sound); + else + stopSounds.push_back(sound); + } + } + + // superpose + m_mixer->superpose(buffer, length, m_volume); + + // cleanup + while(!stopSounds.empty()) + { + sound = stopSounds.front(); + stopSounds.pop_front(); + stop(sound); + } + + while(!tempBufs.empty()) + { + tempbuf = tempBufs.front(); + tempBufs.pop_front(); + delete tempbuf; + } + } + + unlock(); +} + +bool AUD_SoftwareDevice::isValid(AUD_Handle* handle) +{ + for(AUD_HandleIterator i = m_playingSounds.begin(); + i != m_playingSounds.end(); i++) + if(*i == handle) + return true; + for(AUD_HandleIterator i = m_pausedSounds.begin(); + i != m_pausedSounds.end(); i++) + if(*i == handle) + return true; + return false; +} + +AUD_DeviceSpecs AUD_SoftwareDevice::getSpecs() const +{ + return m_specs; +} + +AUD_Handle* AUD_SoftwareDevice::play(AUD_IFactory* factory, bool keep) +{ + AUD_IReader* reader = factory->createReader(); + + // prepare the reader + reader = m_mixer->prepare(reader); + if(reader == NULL) + return NULL; + + // play sound + AUD_SoftwareHandle* sound = new AUD_SoftwareHandle; + sound->keep = keep; + sound->reader = reader; + sound->volume = 1.0f; + sound->loopcount = 0; + sound->stop = NULL; + sound->stop_data = NULL; + + lock(); + m_playingSounds.push_back(sound); + + if(!m_playback) + playing(m_playback = true); + unlock(); + + return sound; +} + +bool AUD_SoftwareDevice::pause(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + // only songs that are played can be paused + for(AUD_HandleIterator i = m_playingSounds.begin(); + i != m_playingSounds.end(); i++) + { + if(*i == handle) + { + m_pausedSounds.push_back(*i); + m_playingSounds.erase(i); + if(m_playingSounds.empty()) + playing(m_playback = false); + result = true; + break; + } + } + + unlock(); + + return result; +} + +bool AUD_SoftwareDevice::resume(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + // only songs that are paused can be resumed + for(AUD_HandleIterator i = m_pausedSounds.begin(); + i != m_pausedSounds.end(); i++) + { + if(*i == handle) + { + m_playingSounds.push_back(*i); + m_pausedSounds.erase(i); + if(!m_playback) + playing(m_playback = true); + result = true; + break; + } + } + + unlock(); + + return result; +} + +bool AUD_SoftwareDevice::stop(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + for(AUD_HandleIterator i = m_playingSounds.begin(); + i != m_playingSounds.end(); i++) + { + if(*i == handle) + { + delete (*i)->reader; + delete *i; + m_playingSounds.erase(i); + if(m_playingSounds.empty()) + playing(m_playback = false); + result = true; + break; + } + } + if(!result) + { + for(AUD_HandleIterator i = m_pausedSounds.begin(); + i != m_pausedSounds.end(); i++) + { + if(*i == handle) + { + delete (*i)->reader; + delete *i; + m_pausedSounds.erase(i); + result = true; + break; + } + } + } + + unlock(); + + return result; +} + +bool AUD_SoftwareDevice::getKeep(AUD_Handle* handle) +{ + bool result = false; + + lock(); + + if(isValid(handle)) + result = ((AUD_SoftwareHandle*)handle)->keep; + + unlock(); + + return result; +} + +bool AUD_SoftwareDevice::setKeep(AUD_Handle* handle, bool keep) +{ + bool result = false; + + lock(); + + if(isValid(handle)) + { + ((AUD_SoftwareHandle*)handle)->keep = keep; + result = true; + } + + unlock(); + + return result; +} + +bool AUD_SoftwareDevice::seek(AUD_Handle* handle, float position) +{ + lock(); + + bool result = false; + + if(isValid(handle)) + { + AUD_IReader* reader = ((AUD_SoftwareHandle*)handle)->reader; + reader->seek((int)(position * reader->getSpecs().rate)); + result = true; + } + + unlock(); + + return result; +} + +float AUD_SoftwareDevice::getPosition(AUD_Handle* handle) +{ + lock(); + + float position = 0.0f; + + if(isValid(handle)) + { + AUD_SoftwareHandle* h = (AUD_SoftwareHandle*)handle; + position = h->reader->getPosition() / (float)m_specs.rate; + } + + unlock(); + + return position; +} + +AUD_Status AUD_SoftwareDevice::getStatus(AUD_Handle* handle) +{ + AUD_Status status = AUD_STATUS_INVALID; + + lock(); + + for(AUD_HandleIterator i = m_playingSounds.begin(); + i != m_playingSounds.end(); i++) + { + if(*i == handle) + { + status = AUD_STATUS_PLAYING; + break; + } + } + if(status == AUD_STATUS_INVALID) + { + for(AUD_HandleIterator i = m_pausedSounds.begin(); + i != m_pausedSounds.end(); i++) + { + if(*i == handle) + { + status = AUD_STATUS_PAUSED; + break; + } + } + } + + unlock(); + + return status; +} + +void AUD_SoftwareDevice::lock() +{ + pthread_mutex_lock(&m_mutex); +} + +void AUD_SoftwareDevice::unlock() +{ + pthread_mutex_unlock(&m_mutex); +} + +float AUD_SoftwareDevice::getVolume() const +{ + return m_volume; +} + +void AUD_SoftwareDevice::setVolume(float volume) +{ + m_volume = volume; +} + +float AUD_SoftwareDevice::getVolume(AUD_Handle* handle) +{ + lock(); + float result = std::numeric_limits::quiet_NaN(); + if(isValid(handle)) + result = ((AUD_SoftwareHandle*)handle)->volume; + unlock(); + return result; +} + +bool AUD_SoftwareDevice::setVolume(AUD_Handle* handle, float volume) +{ + lock(); + bool result = isValid(handle); + if(result) + ((AUD_SoftwareHandle*)handle)->volume = volume; + unlock(); + return result; +} + +float AUD_SoftwareDevice::getPitch(AUD_Handle* handle) +{ + return std::numeric_limits::quiet_NaN(); +} + +bool AUD_SoftwareDevice::setPitch(AUD_Handle* handle, float pitch) +{ + return false; +} + +int AUD_SoftwareDevice::getLoopCount(AUD_Handle* handle) +{ + lock(); + int result = 0; + if(isValid(handle)) + result = ((AUD_SoftwareHandle*)handle)->loopcount; + unlock(); + return result; +} + +bool AUD_SoftwareDevice::setLoopCount(AUD_Handle* handle, int count) +{ + lock(); + bool result = isValid(handle); + if(result) + ((AUD_SoftwareHandle*)handle)->loopcount = count; + unlock(); + return result; +} + +bool AUD_SoftwareDevice::setStopCallback(AUD_Handle* handle, stopCallback callback, void* data) +{ + lock(); + bool result = isValid(handle); + if(result) + { + AUD_SoftwareHandle* h = (AUD_SoftwareHandle*)handle; + h->stop = callback; + h->stop_data = data; + } + unlock(); + return result; +} diff --git a/intern/audaspace/intern/AUD_SoftwareDevice.h b/intern/audaspace/intern/AUD_SoftwareDevice.h new file mode 100644 index 00000000000..93b0f165c7a --- /dev/null +++ b/intern/audaspace/intern/AUD_SoftwareDevice.h @@ -0,0 +1,138 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SOFTWAREDEVICE +#define AUD_SOFTWAREDEVICE + +#include "AUD_IDevice.h" +struct AUD_SoftwareHandle; +class AUD_Mixer; +class AUD_Buffer; + +#include +#include + +/** + * This device plays is a generic device with software mixing. + * Classes implementing this have to: + * - Implement the playing function. + * - Prepare the m_specs, m_mixer variables. + * - Call the create and destroy functions. + * - Call the mix function to retrieve their audio data. + */ +class AUD_SoftwareDevice : public AUD_IDevice +{ +protected: + /** + * The specification of the device. + */ + AUD_DeviceSpecs m_specs; + + /** + * The mixer. + */ + AUD_Mixer* m_mixer; + + /** + * Initializes member variables. + */ + void create(); + + /** + * Uninitializes member variables. + */ + void destroy(); + + /** + * Mixes the next samples into the buffer. + * \param buffer The target buffer. + * \param length The length in samples to be filled. + */ + void mix(data_t* buffer, int length); + + /** + * This function tells the device, to start or pause playback. + * \param playing True if device should playback. + */ + virtual void playing(bool playing)=0; + +private: + /** + * The list of sounds that are currently playing. + */ + std::list m_playingSounds; + + /** + * The list of sounds that are currently paused. + */ + std::list m_pausedSounds; + + /** + * Whether there is currently playback. + */ + bool m_playback; + + /** + * The mutex for locking. + */ + pthread_mutex_t m_mutex; + + /** + * The overall volume of the device. + */ + float m_volume; + + /** + * Checks if a handle is valid. + * \param handle The handle to check. + * \return Whether the handle is valid. + */ + bool isValid(AUD_Handle* handle); + +public: + virtual AUD_DeviceSpecs getSpecs() const; + virtual AUD_Handle* play(AUD_IFactory* factory, bool keep = false); + virtual bool pause(AUD_Handle* handle); + virtual bool resume(AUD_Handle* handle); + virtual bool stop(AUD_Handle* handle); + virtual bool getKeep(AUD_Handle* handle); + virtual bool setKeep(AUD_Handle* handle, bool keep); + virtual bool seek(AUD_Handle* handle, float position); + virtual float getPosition(AUD_Handle* handle); + virtual AUD_Status getStatus(AUD_Handle* handle); + virtual void lock(); + virtual void unlock(); + virtual float getVolume() const; + virtual void setVolume(float volume); + virtual float getVolume(AUD_Handle* handle); + virtual bool setVolume(AUD_Handle* handle, float volume); + virtual float getPitch(AUD_Handle* handle); + virtual bool setPitch(AUD_Handle* handle, float pitch); + virtual int getLoopCount(AUD_Handle* handle); + virtual bool setLoopCount(AUD_Handle* handle, int count); + virtual bool setStopCallback(AUD_Handle* handle, stopCallback callback = NULL, void* data = NULL); +}; + +#endif //AUD_SOFTWAREDEVICE diff --git a/intern/audaspace/intern/AUD_Space.h b/intern/audaspace/intern/AUD_Space.h new file mode 100644 index 00000000000..e411b831d52 --- /dev/null +++ b/intern/audaspace/intern/AUD_Space.h @@ -0,0 +1,193 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SPACE +#define AUD_SPACE + +/// The size of a format in bytes. +#define AUD_FORMAT_SIZE(format) (format & 0x0F) +/// The size of a sample in the specified device format in bytes. +#define AUD_DEVICE_SAMPLE_SIZE(specs) (specs.channels * (specs.format & 0x0F)) +/// The size of a sample in the specified format in bytes. +#define AUD_SAMPLE_SIZE(specs) (specs.channels * sizeof(sample_t)) +/// Throws a AUD_Exception with the provided error code. +#define AUD_THROW(exception, errorstr) { AUD_Exception e; e.error = exception; e.str = errorstr; throw e; } + +/// Returns the smaller of the two values. +#define AUD_MIN(a, b) (((a) < (b)) ? (a) : (b)) +/// Returns the bigger of the two values. +#define AUD_MAX(a, b) (((a) > (b)) ? (a) : (b)) + +// 5 sec * 44100 samples/sec * 4 bytes/sample * 6 channels +/// The size by which a buffer should be resized if the final extent is unknown. +#define AUD_BUFFER_RESIZE_BYTES 5292000 + +/// The default playback buffer size of a device. +#define AUD_DEFAULT_BUFFER_SIZE 1024 + +/** + * The format of a sample. + * The last 4 bit save the byte count of the format. + */ +typedef enum +{ + AUD_FORMAT_INVALID = 0x00, /// Invalid sample format. + AUD_FORMAT_U8 = 0x01, /// 1 byte unsigned byte. + AUD_FORMAT_S16 = 0x12, /// 2 byte signed integer. + AUD_FORMAT_S24 = 0x13, /// 3 byte signed integer. + AUD_FORMAT_S32 = 0x14, /// 4 byte signed integer. + AUD_FORMAT_FLOAT32 = 0x24, /// 4 byte float. + AUD_FORMAT_FLOAT64 = 0x28 /// 8 byte float. +} AUD_SampleFormat; + +/// The channel count. +typedef enum +{ + AUD_CHANNELS_INVALID = 0, /// Invalid channel count. + AUD_CHANNELS_MONO = 1, /// Mono. + AUD_CHANNELS_STEREO = 2, /// Stereo. + AUD_CHANNELS_STEREO_LFE = 3, /// Stereo with LFE channel. + AUD_CHANNELS_SURROUND4 = 4, /// 4 channel surround sound. + AUD_CHANNELS_SURROUND5 = 5, /// 5 channel surround sound. + AUD_CHANNELS_SURROUND51 = 6, /// 5.1 surround sound. + AUD_CHANNELS_SURROUND61 = 7, /// 6.1 surround sound. + AUD_CHANNELS_SURROUND71 = 8, /// 7.1 surround sound. + AUD_CHANNELS_SURROUND72 = 9 /// 7.2 surround sound. +} AUD_Channels; + +/** + * The sample rate tells how many samples are played back within one second. + * Some exotic formats may use other sample rates than provided here. + */ +typedef enum +{ + AUD_RATE_INVALID = 0, /// Invalid sample rate. + AUD_RATE_8000 = 8000, /// 8000 Hz. + AUD_RATE_16000 = 16000, /// 16000 Hz. + AUD_RATE_11025 = 11025, /// 11025 Hz. + AUD_RATE_22050 = 22050, /// 22050 Hz. + AUD_RATE_32000 = 32000, /// 32000 Hz. + AUD_RATE_44100 = 44100, /// 44100 Hz. + AUD_RATE_48000 = 48000, /// 48000 Hz. + AUD_RATE_88200 = 88200, /// 88200 Hz. + AUD_RATE_96000 = 96000, /// 96000 Hz. + AUD_RATE_192000 = 192000 /// 192000 Hz. +} AUD_SampleRate; + +/// Status of a playback handle. +typedef enum +{ + AUD_STATUS_INVALID = 0, /// Invalid handle. Maybe due to stopping. + AUD_STATUS_PLAYING, /// Sound is playing. + AUD_STATUS_PAUSED /// Sound is being paused. +} AUD_Status; + +/// Error codes for exceptions (C++ library) or for return values (C API). +typedef enum +{ + AUD_NO_ERROR = 0, + AUD_ERROR_SPECS, + AUD_ERROR_PROPS, + AUD_ERROR_FILE, + AUD_ERROR_SRC, + AUD_ERROR_FFMPEG, + AUD_ERROR_OPENAL, + AUD_ERROR_SDL, + AUD_ERROR_JACK, +} AUD_Error; + +/// Fading types. +typedef enum +{ + AUD_FADE_IN, + AUD_FADE_OUT +} AUD_FadeType; + +/// Possible distance models for the 3D device. +typedef enum +{ + AUD_DISTANCE_MODEL_INVALID = 0, + AUD_DISTANCE_MODEL_INVERSE, + AUD_DISTANCE_MODEL_INVERSE_CLAMPED, + AUD_DISTANCE_MODEL_LINEAR, + AUD_DISTANCE_MODEL_LINEAR_CLAMPED, + AUD_DISTANCE_MODEL_EXPONENT, + AUD_DISTANCE_MODEL_EXPONENT_CLAMPED, +} AUD_DistanceModel; + +/// Sample type.(float samples) +typedef float sample_t; + +/// Sample data type (format samples) +typedef unsigned char data_t; + +/// Specification of a sound source. +typedef struct +{ + /// Sample rate in Hz. + AUD_SampleRate rate; + + /// Channel count. + AUD_Channels channels; +} AUD_Specs; + +/// Specification of a sound device. +typedef struct +{ + /// Sample format. + AUD_SampleFormat format; + + union + { + struct + { + /// Sample rate in Hz. + AUD_SampleRate rate; + + /// Channel count. + AUD_Channels channels; + }; + AUD_Specs specs; + }; +} AUD_DeviceSpecs; + +/// Exception structure. +typedef struct +{ + /** + * Error code. + * \see AUD_Error + */ + AUD_Error error; + + /** + * Error string. + */ + const char* str; + + // void* userData; - for the case it is needed someday +} AUD_Exception; + +#endif //AUD_SPACE diff --git a/intern/audaspace/intern/AUD_StreamBufferFactory.cpp b/intern/audaspace/intern/AUD_StreamBufferFactory.cpp new file mode 100644 index 00000000000..c1eb8161e30 --- /dev/null +++ b/intern/audaspace/intern/AUD_StreamBufferFactory.cpp @@ -0,0 +1,75 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_StreamBufferFactory.h" +#include "AUD_BufferReader.h" +#include "AUD_Buffer.h" + +#include + +AUD_StreamBufferFactory::AUD_StreamBufferFactory(AUD_IFactory* factory) : + m_buffer(new AUD_Buffer()) +{ + AUD_IReader* reader = factory->createReader(); + + m_specs = reader->getSpecs(); + + int sample_size = AUD_SAMPLE_SIZE(m_specs); + int length; + int index = 0; + sample_t* buffer; + + // get an approximated size if possible + int size = reader->getLength(); + + if(size <= 0) + size = AUD_BUFFER_RESIZE_BYTES / sample_size; + else + size += m_specs.rate; + + // as long as we fill our buffer to the end + while(index == m_buffer.get()->getSize() / sample_size) + { + // increase + m_buffer.get()->resize(size*sample_size, true); + + // read more + length = size-index; + reader->read(length, buffer); + memcpy(m_buffer.get()->getBuffer() + index * m_specs.channels, + buffer, + length * sample_size); + size += AUD_BUFFER_RESIZE_BYTES / sample_size; + index += length; + } + + m_buffer.get()->resize(index * sample_size, true); + delete reader; +} + +AUD_IReader* AUD_StreamBufferFactory::createReader() const +{ + return new AUD_BufferReader(m_buffer, m_specs); +} diff --git a/intern/audaspace/intern/AUD_StreamBufferFactory.h b/intern/audaspace/intern/AUD_StreamBufferFactory.h new file mode 100644 index 00000000000..d0cf1001e5c --- /dev/null +++ b/intern/audaspace/intern/AUD_StreamBufferFactory.h @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_STREAMBUFFERFACTORY +#define AUD_STREAMBUFFERFACTORY + +#include "AUD_IFactory.h" +#include "AUD_Reference.h" +class AUD_Buffer; + +/** + * This factory creates a buffer out of a reader. This way normally streamed + * sound sources can be loaded into memory for buffered playback. + */ +class AUD_StreamBufferFactory : public AUD_IFactory +{ +private: + /** + * The buffer that holds the audio data. + */ + AUD_Reference m_buffer; + + /** + * The specification of the samples. + */ + AUD_Specs m_specs; + + // hide copy constructor and operator= + AUD_StreamBufferFactory(const AUD_StreamBufferFactory&); + AUD_StreamBufferFactory& operator=(const AUD_StreamBufferFactory&); + +public: + /** + * Creates the factory and reads the reader created by the factory supplied + * to the buffer. + * \param factory The factory that creates the reader for buffering. + * \exception AUD_Exception Thrown if the reader cannot be created. + */ + AUD_StreamBufferFactory(AUD_IFactory* factory); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_STREAMBUFFERFACTORY diff --git a/intern/audaspace/intern/Makefile b/intern/audaspace/intern/Makefile new file mode 100644 index 00000000000..b89a8bcd397 --- /dev/null +++ b/intern/audaspace/intern/Makefile @@ -0,0 +1,74 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = audaspace +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_PYTHON)/include/python$(NAN_PYTHON_VERSION) + +ifeq ($(WITH_SDL),true) + CPPFLAGS += -DWITH_SDL + CPPFLAGS += $(NAN_SDLCFLAGS) +endif + +ifeq ($(WITH_OPENAL),true) + CPPFLAGS += -DWITH_OPENAL + CPPFLAGS += -I../OpenAL +endif + +ifeq ($(WITH_JACK),true) + CPPFLAGS += -DWITH_JACK + CPPFLAGS += $(NAN_JACKCFLAGS) + CPPFLAGS += -I../jack +endif + +ifeq ($(WITH_FFMPEG),true) + CPPFLAGS += -DWITH_FFMPEG + CPPFLAGS += $(NAN_FFMPEGCFLAGS) +endif + +ifeq ($(WITH_SNDFILE),true) + CPPFLAGS += -DWITH_SNDFILE + CPPFLAGS += -I../sndfile + CPPFLAGS += -I$(NAN_SNDFILE)/include +endif + +CPPFLAGS += -I$(NAN_SAMPLERATE)/include/ +CPPFLAGS += -I../ffmpeg +CPPFLAGS += -I../FX +CPPFLAGS += -I../SDL +CPPFLAGS += -I../SRC +CPPFLAGS += -I../Python +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/jack/AUD_JackDevice.cpp b/intern/audaspace/jack/AUD_JackDevice.cpp new file mode 100644 index 00000000000..5aa3f7b3fc1 --- /dev/null +++ b/intern/audaspace/jack/AUD_JackDevice.cpp @@ -0,0 +1,339 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_Mixer.h" +#include "AUD_JackDevice.h" +#include "AUD_IReader.h" + +#include +#include + +void* AUD_JackDevice::runMixingThread(void* device) +{ + ((AUD_JackDevice*)device)->updateRingBuffers(); + return NULL; +} + +void AUD_JackDevice::updateRingBuffers() +{ + size_t size, temp; + unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs); + unsigned int i, j; + unsigned int channels = m_specs.channels; + sample_t* buffer = m_buffer.getBuffer(); + float* deinterleave = m_deinterleavebuf.getBuffer(); + jack_transport_state_t state; + jack_position_t position; + + pthread_mutex_lock(&m_mixingLock); + while(m_valid) + { + if(m_sync > 1) + { + if(m_syncFunc) + { + state = jack_transport_query(m_client, &position); + m_syncFunc(m_syncFuncData, state != JackTransportStopped, position.frame / (float) m_specs.rate); + } + + for(i = 0; i < channels; i++) + jack_ringbuffer_reset(m_ringbuffers[i]); + } + + size = jack_ringbuffer_write_space(m_ringbuffers[0]); + for(i = 1; i < channels; i++) + if((temp = jack_ringbuffer_write_space(m_ringbuffers[i])) < size) + size = temp; + + while(size > samplesize) + { + size /= samplesize; + mix((data_t*)buffer, size); + for(i = 0; i < channels; i++) + { + for(j = 0; j < size; j++) + deinterleave[i * size + j] = buffer[i + j * channels]; + jack_ringbuffer_write(m_ringbuffers[i], (char*)(deinterleave + i * size), size * sizeof(float)); + } + + size = jack_ringbuffer_write_space(m_ringbuffers[0]); + for(i = 1; i < channels; i++) + if((temp = jack_ringbuffer_write_space(m_ringbuffers[i])) < size) + size = temp; + } + + if(m_sync > 1) + { + m_sync = 3; + } + + pthread_cond_wait(&m_mixingCondition, &m_mixingLock); + } + pthread_mutex_unlock(&m_mixingLock); +} + +int AUD_JackDevice::jack_mix(jack_nframes_t length, void *data) +{ + AUD_JackDevice* device = (AUD_JackDevice*)data; + unsigned int i; + int count = device->m_specs.channels; + char* buffer; + + if(device->m_sync) + { + // play silence while syncing + for(unsigned int i = 0; i < count; i++) + memset(jack_port_get_buffer(device->m_ports[i], length), 0, length * sizeof(float)); + } + else + { + size_t temp; + size_t readsamples = jack_ringbuffer_read_space(device->m_ringbuffers[0]); + for(i = 1; i < count; i++) + if((temp = jack_ringbuffer_read_space(device->m_ringbuffers[i])) < readsamples) + readsamples = temp; + + readsamples = AUD_MIN(readsamples / sizeof(float), length); + + for(unsigned int i = 0; i < count; i++) + { + buffer = (char*)jack_port_get_buffer(device->m_ports[i], length); + jack_ringbuffer_read(device->m_ringbuffers[i], buffer, readsamples * sizeof(float)); + if(readsamples < length) + memset(buffer + readsamples * sizeof(float), 0, (length - readsamples) * sizeof(float)); + } + + if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0) + { + pthread_cond_signal(&(device->m_mixingCondition)); + pthread_mutex_unlock(&(device->m_mixingLock)); + } + } + + return 0; +} + +int AUD_JackDevice::jack_sync(jack_transport_state_t state, jack_position_t* pos, void* data) +{ + AUD_JackDevice* device = (AUD_JackDevice*)data; + + if(state == JackTransportStopped) + return 1; + + if(pthread_mutex_trylock(&(device->m_mixingLock)) == 0) + { + if(device->m_sync > 2) + { + if(device->m_sync == 3) + { + device->m_sync = 0; + pthread_mutex_unlock(&(device->m_mixingLock)); + return 1; + } + } + else + { + device->m_sync = 2; + pthread_cond_signal(&(device->m_mixingCondition)); + } + pthread_mutex_unlock(&(device->m_mixingLock)); + } + else if(!device->m_sync) + device->m_sync = 1; + + return 0; +} + +void AUD_JackDevice::jack_shutdown(void *data) +{ + AUD_JackDevice* device = (AUD_JackDevice*)data; + device->m_valid = false; +} + +static const char* clientopen_error = "AUD_JackDevice: Couldn't connect to " + "jack server."; +static const char* port_error = "AUD_JackDevice: Couldn't create output port."; +static const char* activate_error = "AUD_JackDevice: Couldn't activate the " + "client."; + +AUD_JackDevice::AUD_JackDevice(std::string name, AUD_DeviceSpecs specs, int buffersize) +{ + if(specs.channels == AUD_CHANNELS_INVALID) + specs.channels = AUD_CHANNELS_STEREO; + + // jack uses floats + m_specs = specs; + m_specs.format = AUD_FORMAT_FLOAT32; + + jack_options_t options = JackNullOption; + jack_status_t status; + + // open client + m_client = jack_client_open(name.c_str(), options, &status); + if(m_client == NULL) + AUD_THROW(AUD_ERROR_JACK, clientopen_error); + + // set callbacks + jack_set_process_callback(m_client, AUD_JackDevice::jack_mix, this); + jack_on_shutdown(m_client, AUD_JackDevice::jack_shutdown, this); + jack_set_sync_callback(m_client, AUD_JackDevice::jack_sync, this); + + // register our output channels which are called ports in jack + m_ports = new jack_port_t*[m_specs.channels]; + + try + { + char portname[64]; + for(int i = 0; i < m_specs.channels; i++) + { + sprintf(portname, "out %d", i+1); + m_ports[i] = jack_port_register(m_client, portname, + JACK_DEFAULT_AUDIO_TYPE, + JackPortIsOutput, 0); + if(m_ports[i] == NULL) + AUD_THROW(AUD_ERROR_JACK, port_error); + } + } + catch(AUD_Exception&) + { + jack_client_close(m_client); + delete[] m_ports; + throw; + } + + m_specs.rate = (AUD_SampleRate)jack_get_sample_rate(m_client); + + buffersize *= sizeof(sample_t); + m_ringbuffers = new jack_ringbuffer_t*[specs.channels]; + for(unsigned int i = 0; i < specs.channels; i++) + m_ringbuffers[i] = jack_ringbuffer_create(buffersize); + buffersize *= specs.channels; + m_deinterleavebuf.resize(buffersize); + m_buffer.resize(buffersize); + + create(); + + m_valid = true; + m_playing = false; + m_sync = 0; + m_syncFunc = NULL; + + pthread_mutex_init(&m_mixingLock, NULL); + pthread_cond_init(&m_mixingCondition, NULL); + + // activate the client + if(jack_activate(m_client)) + { + jack_client_close(m_client); + delete[] m_ports; + for(unsigned int i = 0; i < specs.channels; i++) + jack_ringbuffer_free(m_ringbuffers[i]); + delete[] m_ringbuffers; + pthread_mutex_destroy(&m_mixingLock); + pthread_cond_destroy(&m_mixingCondition); + destroy(); + + AUD_THROW(AUD_ERROR_JACK, activate_error); + } + + const char** ports = jack_get_ports(m_client, NULL, NULL, + JackPortIsPhysical | JackPortIsInput); + if(ports != NULL) + { + for(int i = 0; i < m_specs.channels && ports[i]; i++) + jack_connect(m_client, jack_port_name(m_ports[i]), ports[i]); + + free(ports); + } + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + pthread_create(&m_mixingThread, &attr, runMixingThread, this); + + pthread_attr_destroy(&attr); +} + +AUD_JackDevice::~AUD_JackDevice() +{ + if(m_valid) + jack_client_close(m_client); + m_valid = false; + + delete[] m_ports; + + pthread_mutex_lock(&m_mixingLock); + pthread_cond_signal(&m_mixingCondition); + pthread_mutex_unlock(&m_mixingLock); + pthread_join(m_mixingThread, NULL); + + pthread_cond_destroy(&m_mixingCondition); + pthread_mutex_destroy(&m_mixingLock); + for(unsigned int i = 0; i < m_specs.channels; i++) + jack_ringbuffer_free(m_ringbuffers[i]); + delete[] m_ringbuffers; + + destroy(); +} + +void AUD_JackDevice::playing(bool playing) +{ + // Do nothing. +} + +void AUD_JackDevice::startPlayback() +{ + jack_transport_start(m_client); +} + +void AUD_JackDevice::stopPlayback() +{ + jack_transport_stop(m_client); +} + +void AUD_JackDevice::seekPlayback(float time) +{ + if(time >= 0.0f) + jack_transport_locate(m_client, time * m_specs.rate); +} + +void AUD_JackDevice::setSyncCallback(AUD_syncFunction sync, void* data) +{ + m_syncFunc = sync; + m_syncFuncData = data; +} + +float AUD_JackDevice::getPlaybackPosition() +{ + jack_position_t position; + jack_transport_query(m_client, &position); + return position.frame / (float) m_specs.rate; +} + +bool AUD_JackDevice::doesPlayback() +{ + return jack_transport_query(m_client, NULL) != JackTransportStopped; +} diff --git a/intern/audaspace/jack/AUD_JackDevice.h b/intern/audaspace/jack/AUD_JackDevice.h new file mode 100644 index 00000000000..418992e0db1 --- /dev/null +++ b/intern/audaspace/jack/AUD_JackDevice.h @@ -0,0 +1,153 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_JACKDEVICE +#define AUD_JACKDEVICE + + +#include "AUD_SoftwareDevice.h" +#include "AUD_Buffer.h" + +#include + +#include +#include + +typedef void (*AUD_syncFunction)(void*, int, float); + +/** + * This device plays back through Jack. + */ +class AUD_JackDevice : public AUD_SoftwareDevice +{ +private: + /** + * The output ports of jack. + */ + jack_port_t** m_ports; + + /** + * The jack client. + */ + jack_client_t* m_client; + + /** + * The output buffer. + */ + AUD_Buffer m_buffer; + + /** + * The deinterleaving buffer. + */ + AUD_Buffer m_deinterleavebuf; + + jack_ringbuffer_t** m_ringbuffers; + + /** + * Whether the device is valid. + */ + bool m_valid; + + /** + * Invalidates the jack device. + * \param data The jack device that gets invalidet by jack. + */ + static void jack_shutdown(void *data); + + /** + * Mixes the next bytes into the buffer. + * \param length The length in samples to be filled. + * \param data A pointer to the jack device. + * \return 0 what shows success. + */ + static int jack_mix(jack_nframes_t length, void *data); + + static int jack_sync(jack_transport_state_t state, jack_position_t* pos, void* data); + + /** + * Last Jack Transport playing state. + */ + bool m_playing; + + /** + * Syncronisation state. + */ + int m_sync; + + /** + * External syncronisation callback function. + */ + AUD_syncFunction m_syncFunc; + + /** + * Data for the sync function. + */ + void* m_syncFuncData; + + /** + * The mixing thread. + */ + pthread_t m_mixingThread; + + pthread_mutex_t m_mixingLock; + + pthread_cond_t m_mixingCondition; + + static void* runMixingThread(void* device); + + void updateRingBuffers(); + + // hide copy constructor and operator= + AUD_JackDevice(const AUD_JackDevice&); + AUD_JackDevice& operator=(const AUD_JackDevice&); + +protected: + virtual void playing(bool playing); + +public: + /** + * Creates a Jack client for audio output. + * \param name The client name. + * \param specs The wanted audio specification, where only the channel count + * is important. + * \param buffersize The size of the internal buffer. + * \exception AUD_Exception Thrown if the audio device cannot be opened. + */ + AUD_JackDevice(std::string name, AUD_DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Closes the Jack client. + */ + virtual ~AUD_JackDevice(); + + void startPlayback(); + void stopPlayback(); + void seekPlayback(float time); + void setSyncCallback(AUD_syncFunction sync, void* data); + float getPlaybackPosition(); + bool doesPlayback(); +}; + +#endif //AUD_JACKDEVICE diff --git a/intern/audaspace/jack/Makefile b/intern/audaspace/jack/Makefile new file mode 100644 index 00000000000..23cadf559c0 --- /dev/null +++ b/intern/audaspace/jack/Makefile @@ -0,0 +1,44 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): GSR +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_jack +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +# If we are here, jack is enable. +CPPFLAGS += -DWITH_JACK +CPPFLAGS += $(NAN_JACKCFLAGS) + +CPPFLAGS += -I../intern +CPPFLAGS += -I.. +CPPFLAGS += -I. diff --git a/intern/audaspace/make/msvc_9_0/audaspace.vcproj b/intern/audaspace/make/msvc_9_0/audaspace.vcproj new file mode 100644 index 00000000000..e6d83f18b01 --- /dev/null +++ b/intern/audaspace/make/msvc_9_0/audaspace.vcproj @@ -0,0 +1,924 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/intern/audaspace/sndfile/AUD_SndFileFactory.cpp b/intern/audaspace/sndfile/AUD_SndFileFactory.cpp new file mode 100644 index 00000000000..d8ea7006073 --- /dev/null +++ b/intern/audaspace/sndfile/AUD_SndFileFactory.cpp @@ -0,0 +1,49 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SndFileFactory.h" +#include "AUD_SndFileReader.h" +#include "AUD_Buffer.h" + +#include + +AUD_SndFileFactory::AUD_SndFileFactory(std::string filename) : + m_filename(filename) +{ +} + +AUD_SndFileFactory::AUD_SndFileFactory(const data_t* buffer, int size) : + m_buffer(new AUD_Buffer(size)) +{ + memcpy(m_buffer.get()->getBuffer(), buffer, size); +} + +AUD_IReader* AUD_SndFileFactory::createReader() const +{ + if(m_buffer.get()) + return new AUD_SndFileReader(m_buffer); + else + return new AUD_SndFileReader(m_filename); +} diff --git a/intern/audaspace/sndfile/AUD_SndFileFactory.h b/intern/audaspace/sndfile/AUD_SndFileFactory.h new file mode 100644 index 00000000000..350e48abef8 --- /dev/null +++ b/intern/audaspace/sndfile/AUD_SndFileFactory.h @@ -0,0 +1,72 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SNDFILEFACTORY +#define AUD_SNDFILEFACTORY + +#include "AUD_IFactory.h" +#include "AUD_Reference.h" +class AUD_Buffer; + +#include + +/** + * This factory reads a sound file via libsndfile. + */ +class AUD_SndFileFactory : public AUD_IFactory +{ +private: + /** + * The filename of the sound source file. + */ + std::string m_filename; + + /** + * The buffer to read from. + */ + AUD_Reference m_buffer; + + // hide copy constructor and operator= + AUD_SndFileFactory(const AUD_SndFileFactory&); + AUD_SndFileFactory& operator=(const AUD_SndFileFactory&); + +public: + /** + * Creates a new factory. + * \param filename The sound file path. + */ + AUD_SndFileFactory(std::string filename); + + /** + * Creates a new factory. + * \param buffer The buffer to read from. + * \param size The size of the buffer. + */ + AUD_SndFileFactory(const data_t* buffer, int size); + + virtual AUD_IReader* createReader() const; +}; + +#endif //AUD_SNDFILEFACTORY diff --git a/intern/audaspace/sndfile/AUD_SndFileReader.cpp b/intern/audaspace/sndfile/AUD_SndFileReader.cpp new file mode 100644 index 00000000000..488a06d8728 --- /dev/null +++ b/intern/audaspace/sndfile/AUD_SndFileReader.cpp @@ -0,0 +1,171 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#include "AUD_SndFileReader.h" + +#include + +sf_count_t AUD_SndFileReader::vio_get_filelen(void *user_data) +{ + AUD_SndFileReader* reader = (AUD_SndFileReader*)user_data; + return reader->m_membuffer.get()->getSize(); +} + +sf_count_t AUD_SndFileReader::vio_seek(sf_count_t offset, int whence, + void *user_data) +{ + AUD_SndFileReader* reader = (AUD_SndFileReader*)user_data; + + switch(whence) + { + case SEEK_SET: + reader->m_memoffset = offset; + break; + case SEEK_CUR: + reader->m_memoffset = reader->m_memoffset + offset; + break; + case SEEK_END: + reader->m_memoffset = reader->m_membuffer.get()->getSize() + offset; + break; + } + + return reader->m_memoffset; +} + +sf_count_t AUD_SndFileReader::vio_read(void *ptr, sf_count_t count, + void *user_data) +{ + AUD_SndFileReader* reader = (AUD_SndFileReader*)user_data; + + if(reader->m_memoffset + count > reader->m_membuffer.get()->getSize()) + count = reader->m_membuffer.get()->getSize() - reader->m_memoffset; + + memcpy(ptr, ((data_t*)reader->m_membuffer.get()->getBuffer()) + + reader->m_memoffset, count); + reader->m_memoffset += count; + + return count; +} + +sf_count_t AUD_SndFileReader::vio_tell(void *user_data) +{ + AUD_SndFileReader* reader = (AUD_SndFileReader*)user_data; + + return reader->m_memoffset; +} + +static const char* fileopen_error = "AUD_SndFileReader: File couldn't be " + "read."; + +AUD_SndFileReader::AUD_SndFileReader(std::string filename) : + m_position(0) +{ + SF_INFO sfinfo; + + sfinfo.format = 0; + m_sndfile = sf_open(filename.c_str(), SFM_READ, &sfinfo); + + if(!m_sndfile) + AUD_THROW(AUD_ERROR_FILE, fileopen_error); + + m_specs.channels = (AUD_Channels) sfinfo.channels; + m_specs.rate = (AUD_SampleRate) sfinfo.samplerate; + m_length = sfinfo.frames; + m_seekable = sfinfo.seekable; +} + +AUD_SndFileReader::AUD_SndFileReader(AUD_Reference buffer) : + m_position(0), + m_membuffer(buffer), + m_memoffset(0) +{ + m_vio.get_filelen = vio_get_filelen; + m_vio.read = vio_read; + m_vio.seek = vio_seek; + m_vio.tell = vio_tell; + m_vio.write = NULL; + + SF_INFO sfinfo; + + sfinfo.format = 0; + m_sndfile = sf_open_virtual(&m_vio, SFM_READ, &sfinfo, this); + + if(!m_sndfile) + AUD_THROW(AUD_ERROR_FILE, fileopen_error); + + m_specs.channels = (AUD_Channels) sfinfo.channels; + m_specs.rate = (AUD_SampleRate) sfinfo.samplerate; + m_length = sfinfo.frames; + m_seekable = sfinfo.seekable; +} + +AUD_SndFileReader::~AUD_SndFileReader() +{ + sf_close(m_sndfile); +} + +bool AUD_SndFileReader::isSeekable() const +{ + return m_seekable; +} + +void AUD_SndFileReader::seek(int position) +{ + if(m_seekable) + { + position = sf_seek(m_sndfile, position, SEEK_SET); + m_position = position; + } +} + +int AUD_SndFileReader::getLength() const +{ + return m_length; +} + +int AUD_SndFileReader::getPosition() const +{ + return m_position; +} + +AUD_Specs AUD_SndFileReader::getSpecs() const +{ + return m_specs; +} + +void AUD_SndFileReader::read(int & length, sample_t* & buffer) +{ + int sample_size = AUD_SAMPLE_SIZE(m_specs); + + // resize output buffer if necessary + if(m_buffer.getSize() < length*sample_size) + m_buffer.resize(length*sample_size); + + buffer = m_buffer.getBuffer(); + + length = sf_readf_float(m_sndfile, buffer, length); + + m_position += length; +} diff --git a/intern/audaspace/sndfile/AUD_SndFileReader.h b/intern/audaspace/sndfile/AUD_SndFileReader.h new file mode 100644 index 00000000000..a53189fdecd --- /dev/null +++ b/intern/audaspace/sndfile/AUD_SndFileReader.h @@ -0,0 +1,129 @@ +/* + * $Id$ + * + * ***** BEGIN LGPL LICENSE BLOCK ***** + * + * Copyright 2009 Jörg Hermann Müller + * + * This file is part of AudaSpace. + * + * AudaSpace is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AudaSpace is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with AudaSpace. If not, see . + * + * ***** END LGPL LICENSE BLOCK ***** + */ + +#ifndef AUD_SNDFILEREADER +#define AUD_SNDFILEREADER + +#include "AUD_IReader.h" +#include "AUD_Reference.h" +#include "AUD_Buffer.h" + +#include +#include + +typedef sf_count_t (*sf_read_f)(SNDFILE *sndfile, void *ptr, sf_count_t frames); + +/** + * This class reads a sound file via libsndfile. + */ +class AUD_SndFileReader : public AUD_IReader +{ +private: + /** + * The current position in samples. + */ + int m_position; + + /** + * The sample count in the file. + */ + int m_length; + + /** + * Whether the file is seekable. + */ + bool m_seekable; + + /** + * The specification of the audio data. + */ + AUD_Specs m_specs; + + /** + * The playback buffer. + */ + AUD_Buffer m_buffer; + + /** + * The sndfile. + */ + SNDFILE* m_sndfile; + + /** + * The virtual IO structure for memory file reading. + */ + SF_VIRTUAL_IO m_vio; + + /** + * The pointer to the memory file. + */ + AUD_Reference m_membuffer; + + /** + * The current reading pointer of the memory file. + */ + int m_memoffset; + + // Functions for libsndfile virtual IO functionality + static sf_count_t vio_get_filelen(void *user_data); + static sf_count_t vio_seek(sf_count_t offset, int whence, void *user_data); + static sf_count_t vio_read(void *ptr, sf_count_t count, void *user_data); + static sf_count_t vio_tell(void *user_data); + + // hide copy constructor and operator= + AUD_SndFileReader(const AUD_SndFileReader&); + AUD_SndFileReader& operator=(const AUD_SndFileReader&); + +public: + /** + * Creates a new reader. + * \param filename The path to the file to be read. + * \exception AUD_Exception Thrown if the file specified does not exist or + * cannot be read with libsndfile. + */ + AUD_SndFileReader(std::string filename); + + /** + * Creates a new reader. + * \param buffer The buffer to read from. + * \exception AUD_Exception Thrown if the buffer specified cannot be read + * with libsndfile. + */ + AUD_SndFileReader(AUD_Reference buffer); + + /** + * Destroys the reader and closes the file. + */ + virtual ~AUD_SndFileReader(); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual AUD_Specs getSpecs() const; + virtual void read(int & length, sample_t* & buffer); +}; + +#endif //AUD_SNDFILEREADER diff --git a/intern/audaspace/sndfile/Makefile b/intern/audaspace/sndfile/Makefile new file mode 100644 index 00000000000..0012a11203e --- /dev/null +++ b/intern/audaspace/sndfile/Makefile @@ -0,0 +1,41 @@ +# +# $Id$ +# +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): +# +# ***** END GPL LICENSE BLOCK ***** +# +# + +LIBNAME = aud_sndfile +DIR = $(OCGDIR)/intern/audaspace + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_SNDFILE)/include +CPPFLAGS += -I../intern +CPPFLAGS += -I.. +CPPFLAGS += -I.