pax_global_header00006660000000000000000000000064137135474110014520gustar00rootroot0000000000000052 comment=fab5861da3898a34003b56de98729d06b03fb874 ocaml-alsa-0.3.0/000077500000000000000000000000001371354741100135315ustar00rootroot00000000000000ocaml-alsa-0.3.0/.github/000077500000000000000000000000001371354741100150715ustar00rootroot00000000000000ocaml-alsa-0.3.0/.github/workflows/000077500000000000000000000000001371354741100171265ustar00rootroot00000000000000ocaml-alsa-0.3.0/.github/workflows/ci.yml000066400000000000000000000007661371354741100202550ustar00rootroot00000000000000name: CI on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Setup OCaml uses: avsm/setup-ocaml@v1.0 - name: Install depext module run: opam install -y depext - name: Pin locally run: opam pin -y add --no-action . - name: Install locally run: opam depext -y -i alsa - name: Build locally run: eval $(opam env) && dune build - name: Run tests locally run: eval $(opam env) && dune runtest ocaml-alsa-0.3.0/.gitignore000066400000000000000000000000631371354741100155200ustar00rootroot00000000000000*~ _build *.byte *.native _tests .merlin *.install ocaml-alsa-0.3.0/.ocamlformat000066400000000000000000000003201371354741100160310ustar00rootroot00000000000000profile = conventional break-separators = after space-around-lists = false doc-comments = before match-indent = 2 match-indent-nested = always parens-ite exp-grouping = preserve module-item-spacing = compact ocaml-alsa-0.3.0/.travis-ci.sh000077500000000000000000000005171371354741100160520ustar00rootroot00000000000000# Hacking the build into Travis-CI "C" environment # See http://anil.recoil.org/2013/09/30/travis-and-ocaml.html export OPAMYES=1 opam init if [ -n "${OPAM_SWITCH}" ]; then opam switch ${OPAM_SWITCH} fi eval `opam config env` opam install -y depext dune opam pin -y add --no-action . opam depext -y -i alsa # compile dune build ocaml-alsa-0.3.0/.travis.yml000066400000000000000000000001701371354741100156400ustar00rootroot00000000000000language: c sudo: false addons: apt: sources: - avsm packages: - opam script: bash -ex .travis-ci.sh ocaml-alsa-0.3.0/CHANGES.md000066400000000000000000000026151371354741100151270ustar00rootroot000000000000000.3.0 (2020-08-08) ===== - Switch to `bytes` for I/O operations (makes the API incompatible with previous versions). - Switch to `dune` for building. - Add `readn_float_ba` and `writen_float_ba`. - Start adding support for sequencers (`Sequencer` module). 0.2.3 (2016-11-18) ===== - Fix `CAMLlocal` bug (#1). 0.2.2 (2015-07-29) ===== - Dummy github release. 0.2.1 (2010-05-12) ===== - Use `snd_pcm_sframes_t` for error codes to avoid int overflow. Thanks to Peter Retep for reporting and patching! - Added `get_periods_{min,max}` and `get_buffer_max`. 0.2.0 (2010-08-19) ===== - Added resume and recover functions. - Cleaned exception handling. 0.1.4 (2008-10-11) ===== - Added support for `--enable-debugging` configure option - Added NO_CUSTOM to build in standard mode. - Added prefix to main compilation variables if passed to configure. - Makefile now honnors `LIBDIRS` variable for linking against libraries located in other places than then standard ones. 0.1.3 (2008-04-14) ===== - Install .cmx file with others 0.1.2 (2007-10-17) ===== - Added `readn` and `writen`. - Improved configure support. 0.1.1 (2007-05-22) ===== - Better error reporting. - Added `set_nonblock`. - Added `{readn,writen}_float{,64}` functions to access devices directly using floats. - Added get_version. - Correctly install on non-native architectures (for OCaml). 0.1.0 (2006-07-18) ===== - Initial release. ocaml-alsa-0.3.0/COPYING000066400000000000000000000431311371354741100145660ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) 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 this service 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 make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. 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. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute 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 and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), 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 distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the 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 a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, 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. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE 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. 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 convey 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 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 Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision 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, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This 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 Library General Public License instead of this License. ocaml-alsa-0.3.0/Makefile000066400000000000000000000000471371354741100151720ustar00rootroot00000000000000all: @dune build clean: @dune clean ocaml-alsa-0.3.0/README.md000066400000000000000000000015211371354741100150070ustar00rootroot00000000000000ocaml-alsa ========== This package contains an OCaml interface for the ALSA library, otherwise known as libasound2. Prerequisites ------------- - ocaml >= 3.0.6 (haven't tried earlier versions) - libasound2 >= 1.0.14a (haven't tried earlier versions) - findlib >= 0.8.1 (haven't tried earlier versions) - dune >= 2.0 Compilation ----------- ```sh dune build ``` This should build both the native and the byte-code version of the extension library. Installation ------------ Via `opam`: ```sh opam install alsa ``` Via `dune` (for developers): ```sh dune install ``` This should install the library file (using ocamlfind) in the appropriate place. License ------- Please see the COPYING file. Author ------ This author of this software may be contacted by electronic mail at the following address: savonet-users@lists.sourceforge.net. ocaml-alsa-0.3.0/alsa.opam000066400000000000000000000020641371354741100153310ustar00rootroot00000000000000# This file is generated by dune, edit dune-project instead opam-version: "2.0" version: "0.3.0" synopsis: "Bindings for the ALSA library which provides functions for using soundcards" maintainer: ["The Savonet Team "] authors: ["The Savonet Team "] license: "GPL-2.0" homepage: "https://github.com/savonet/ocaml-alsa" bug-reports: "https://github.com/savonet/ocaml-alsa/issues" depends: [ "ocaml" {>= "4.02.0"} "dune" {> "2.0"} "dune-configurator" ] conflicts: [ "liquidsoap" {< "1.5.0"} ] build: [ ["dune" "subst"] {pinned} [ "dune" "build" "-p" name "-j" jobs "@install" "@runtest" {with-test} "@doc" {with-doc} ] ] dev-repo: "git+https://github.com/savonet/ocaml-alsa.git" depexts: [ ["alsa-lib-dev"] {os-distribution = "alpine"} ["alsa-lib-devel"] {os-distribution = "centos"} ["alsa-lib-devel"] {os-distribution = "fedora"} ["alsa-lib-devel"] {os-family = "suse"} ["libasound2-dev"] {os-family = "debian"} ] available: [os = "linux"] ocaml-alsa-0.3.0/alsa.opam.template000066400000000000000000000004211371354741100171360ustar00rootroot00000000000000depexts: [ ["alsa-lib-dev"] {os-distribution = "alpine"} ["alsa-lib-devel"] {os-distribution = "centos"} ["alsa-lib-devel"] {os-distribution = "fedora"} ["alsa-lib-devel"] {os-family = "suse"} ["libasound2-dev"] {os-family = "debian"} ] available: [os = "linux"] ocaml-alsa-0.3.0/dune-project000066400000000000000000000007501371354741100160550ustar00rootroot00000000000000(lang dune 2.0) (version 0.3.0) (name alsa) (source (github savonet/ocaml-alsa)) (license GPL-2.0) (authors "The Savonet Team ") (maintainers "The Savonet Team ") (generate_opam_files true) (package (name alsa) (synopsis "Bindings for the ALSA library which provides functions for using soundcards") (depends (ocaml (>= 4.02.0)) (dune (> 2.0)) dune-configurator) (conflicts (liquidsoap (< 1.5.0))) ) ocaml-alsa-0.3.0/examples/000077500000000000000000000000001371354741100153475ustar00rootroot00000000000000ocaml-alsa-0.3.0/examples/midi-output/000077500000000000000000000000001371354741100176275ustar00rootroot00000000000000ocaml-alsa-0.3.0/examples/midi-output/Makefile000066400000000000000000000001531371354741100212660ustar00rootroot00000000000000all: build test: build @OCAMLRUNPARAM=b dune exec ./output.exe build: @dune build clean: @dune clean ocaml-alsa-0.3.0/examples/midi-output/dune000066400000000000000000000000631371354741100205040ustar00rootroot00000000000000(executable (name output) (libraries alsa unix)) ocaml-alsa-0.3.0/examples/midi-output/output.ml000066400000000000000000000014601371354741100215220ustar00rootroot00000000000000open Alsa let () = Printf.printf "Using ALSA %s\n\n%!" (Alsa.get_version ()); let seq = Sequencer.create "default" `Output in Sequencer.set_client_name seq "OCaml test client"; let port = Sequencer.create_port seq "Test port" [Port_cap_read; Port_cap_subs_read] [Port_type_application] in Printf.printf "port: %d\n\n%!" port; Sequencer.subscribe_write_all seq port; for i = 0 to 40 do Printf.printf "note: %d\n%!" i; Sequencer.output_event seq (Sequencer.Event.Note_on {Sequencer.Event. note_channel = 0; note_note = i; note_velocity = 127; note_off_velocity = 127; note_duration = 1000}); Unix.sleepf 0.1; Sequencer.output_event seq (Sequencer.Event.Note_on {Sequencer.Event. note_channel = 0; note_note = i; note_velocity = 0; note_off_velocity = 127; note_duration = 1000}) done; ocaml-alsa-0.3.0/examples/recorder/000077500000000000000000000000001371354741100171545ustar00rootroot00000000000000ocaml-alsa-0.3.0/examples/recorder/dune000066400000000000000000000000601371354741100200260ustar00rootroot00000000000000(executable (name recorder) (libraries alsa)) ocaml-alsa-0.3.0/examples/recorder/recorder.ml000066400000000000000000000033421371354741100213150ustar00rootroot00000000000000(* $Id$ *) open Alsa let period_size = 4096 let nb_periods = 4 let period_frames = period_size / 4 type mode = Recorder | Player | Loopback let mode = ref Recorder let set_params dev = let params = Pcm.get_params dev in Pcm.set_access dev params Pcm.Access_rw_interleaved; Pcm.set_format dev params Pcm.Format_s16_le; ignore (Pcm.set_rate_near dev params 44100 Dir_eq); Pcm.set_channels dev params 2; Pcm.set_buffer_size dev params (period_frames * nb_periods); Pcm.set_params dev params let player () = let buf = Bytes.create period_size in let dev = Pcm.open_pcm "hw:0,0" [Pcm.Playback] [] in set_params dev; Pcm.prepare dev; while true do assert (input stdin buf 0 period_size = period_size); try ignore (Pcm.writei dev buf 0 period_frames) with Buffer_xrun -> Printf.eprintf "Buffer xrun!\n%!" done let recorder () = let buf = Bytes.create period_size in let dev = Pcm.open_pcm "hw:0,0" [Pcm.Capture] [] in set_params dev; Pcm.prepare dev; while true do let _ = Pcm.readi dev buf 0 period_frames in Printf.printf "%s%!" (Bytes.to_string buf) done let loopback () = let buf = Bytes.create period_size in let dev = Pcm.open_pcm "hw:0,0" [Pcm.Playback; Pcm.Capture] [] in set_params dev; Pcm.prepare dev; while true do let _ = Pcm.readi dev buf 0 period_frames in ignore (Pcm.wait dev (-1)); Pcm.prepare dev; ignore (Pcm.writei dev buf 0 period_frames) done let () = Arg.parse [ ("-p", Arg.Unit (fun () -> mode := Player), "player mode"); ("-l", Arg.Unit (fun () -> mode := Loopback), "loopback mode"); ] (fun _ -> ()) "recorder"; match !mode with | Recorder -> recorder () | Player -> player () | Loopback -> loopback () ocaml-alsa-0.3.0/examples/sequencer/000077500000000000000000000000001371354741100173415ustar00rootroot00000000000000ocaml-alsa-0.3.0/examples/sequencer/Makefile000066400000000000000000000001361371354741100210010ustar00rootroot00000000000000all: build test: build @dune exec ./sequencer.exe build: @dune build clean: @dune clean ocaml-alsa-0.3.0/examples/sequencer/dune000066400000000000000000000000611371354741100202140ustar00rootroot00000000000000(executable (name sequencer) (libraries alsa)) ocaml-alsa-0.3.0/examples/sequencer/sequencer.ml000066400000000000000000000022371371354741100216710ustar00rootroot00000000000000open Alsa let () = Printf.printf "Using ALSA %s\n\n%!" (Alsa.get_version ()); let seq = Sequencer.create "default" `Input in Sequencer.set_client_name seq "OCaml test client"; let port = Sequencer.create_port seq "Test port" [Port_cap_write; Port_cap_subs_write] [Port_type_MIDI_generic] in Printf.printf "port: %d\n%!" port; Sequencer.subscribe_read_all seq port; try while true do match (Sequencer.input_event seq).ev_event with | Sequencer.Event.Note_on n -> Printf.printf "note on: %d at %d\n%!" n.note_note n.note_velocity | Sequencer.Event.Note_off n -> Printf.printf "note off: %d\n%!" n.note_note | Sequencer.Event.Controller c -> Printf.printf "controller: %d / %d\n%!" c.controller_value c.controller_param | Sequencer.Event.Program_change c -> Printf.printf "program change: %d / %d\n%!" c.controller_value c.controller_param | Sequencer.Event.Pitch_bend c -> Printf.printf "pitch bend: %d\n%!" c.controller_value | Sequencer.Event.Unhandled n -> Printf.printf "unhandled event: %d\n%!" n | _ -> Printf.printf "ignored event\n%!"; done with e -> Printf.printf "Error: %s\n%!" (Printexc.to_string e) ocaml-alsa-0.3.0/examples/sine/000077500000000000000000000000001371354741100163055ustar00rootroot00000000000000ocaml-alsa-0.3.0/examples/sine/Makefile000066400000000000000000000001361371354741100177450ustar00rootroot00000000000000all: build build: @dune build test: build @export OCAMLRUNPARAM=b && dune exec ./sine.exe ocaml-alsa-0.3.0/examples/sine/dune000066400000000000000000000000651371354741100171640ustar00rootroot00000000000000(executable (name sine) (libraries bigarray alsa)) ocaml-alsa-0.3.0/examples/sine/sine.ml000066400000000000000000000021441371354741100175760ustar00rootroot00000000000000open Alsa let samplerate = 44100 let channels = 2 let () = Printf.printf "Using ALSA %s.\n%!" (Alsa.get_version ()); let dev = Pcm.open_pcm "default" [Pcm.Playback] [] in let params = Pcm.get_params dev in Pcm.set_access dev params Pcm.Access_rw_interleaved; Pcm.set_format dev params Pcm.Format_float; let samplerate = Pcm.set_rate_near dev params samplerate Dir_eq in Pcm.set_channels dev params channels; Pcm.set_params dev params; let buffer_size = Pcm.get_buffer_size params in let period_size = Pcm.get_period_size params in Printf.printf "samplerate: %d, buffer size: %d, period size: %d\n%!" samplerate buffer_size period_size; Pcm.prepare dev; let buf = Bigarray.Array1.create Bigarray.Float32 Bigarray.C_layout (channels * period_size) in let t = ref 0. in while true do Printf.printf "time: %f\r%!" !t; for i = 0 to period_size - 1 do let x = sin (2. *. Float.pi *. !t *. 440.) in t := !t +. 1. /. float samplerate; for c = 0 to channels - 1 do buf.{channels*i+c} <- x done done; ignore (Pcm.writei_float_ba dev channels buf) done ocaml-alsa-0.3.0/src/000077500000000000000000000000001371354741100143205ustar00rootroot00000000000000ocaml-alsa-0.3.0/src/Makefile000066400000000000000000000000361371354741100157570ustar00rootroot00000000000000all clean: @$(MAKE) -C .. $@ ocaml-alsa-0.3.0/src/alsa.ml000066400000000000000000000210551371354741100155750ustar00rootroot00000000000000(* Copyright 2005-2020 Savonet team This file is part of Ocaml-alsa. Ocaml-alsa 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. Ocaml-alsa 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 Ocaml-alsa; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *) external get_version : unit -> string = "ocaml_alsa_version" exception Buffer_xrun exception Bad_state exception Suspended exception IO_error exception Device_busy exception Invalid_argument exception Device_removed exception Interrupted exception Try_again exception Unknown_error of int type direction = Dir_down | Dir_eq | Dir_up let () = Callback.register_exception "alsa_exn_buffer_xrun" Buffer_xrun; Callback.register_exception "alsa_exn_bad_state" Bad_state; Callback.register_exception "alsa_exn_suspended" Suspended; Callback.register_exception "alsa_exn_io_error" IO_error; Callback.register_exception "alsa_exn_device_busy" Device_busy; Callback.register_exception "alsa_exn_invalid_argument" Invalid_argument; Callback.register_exception "alsa_exn_device_removed" Device_removed; Callback.register_exception "alsa_exn_interrupted" Interrupted; Callback.register_exception "alsa_exn_try_again" Try_again; Callback.register_exception "alsa_exn_unknown_error" (Unknown_error 0) external no_stderr_report : unit -> unit = "ocaml_snd_no_stderr_report" external string_of_error : int -> string = "ocaml_snd_string_of_error" external int_of_error : string -> int = "ocaml_snd_int_of_error" let int_of_error e = let f = int_of_error in match e with | Buffer_xrun -> f "alsa_exn_buffer_xrun" | Bad_state -> f "alsa_exn_bad_state" | Suspended -> f "alsa_exn_suspended" | IO_error -> f "alsa_exn_io_error" | Device_busy -> f "alsa_exn_device_busy" | Invalid_argument -> f "alsa_exn_invalid_argument" | Device_removed -> f "alsa_exn_device_removed" | Interrupted -> f "alsa_exn_interrupted" | Unknown_error x -> x | _ -> raise e let string_of_error e = string_of_error (int_of_error e) module Pcm = struct type handle type params type stream = Playback | Capture type mode = Async | Non_blocking type state = | St_open | St_setup | St_prepared | St_running | St_xrun | St_draining | St_paused | St_suspended | St_disconnected external open_pcm : string -> stream list -> mode list -> handle = "ocaml_snd_pcm_open" external close : handle -> unit = "ocaml_snd_pcm_close" external prepare : handle -> unit = "ocaml_snd_pcm_prepare" external resume : handle -> unit = "ocaml_snd_pcm_resume" external recover : handle -> int -> bool -> unit = "ocaml_snd_pcm_recover" let recover ?(verbose = false) h e = recover h (int_of_error e) verbose external start : handle -> unit = "ocaml_snd_pcm_start" external drain : handle -> unit = "ocaml_snd_pcm_drain" external drop : handle -> unit = "ocaml_snd_pcm_drop" external pause : handle -> bool -> unit = "ocaml_snd_pcm_pause" external reset : handle -> unit = "ocaml_snd_pcm_reset" external wait : handle -> int -> bool = "ocaml_snd_pcm_wait" external readi : handle -> bytes -> int -> int -> int = "ocaml_snd_pcm_readi" external writei : handle -> bytes -> int -> int -> int = "ocaml_snd_pcm_writei" external readn : handle -> bytes array -> int -> int -> int = "ocaml_snd_pcm_readn" external writen : handle -> bytes array -> int -> int -> int = "ocaml_snd_pcm_writen" external readn_float : handle -> float array array -> int -> int -> int = "ocaml_snd_pcm_readn_float" external writen_float : handle -> float array array -> int -> int -> int = "ocaml_snd_pcm_writen_float" external readn_float_ba : handle -> (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t array -> int = "ocaml_snd_pcm_readn_float_ba" external writen_float_ba : handle -> (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t array -> int = "ocaml_snd_pcm_writen_float_ba" external writei_float_ba : handle -> int -> (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t -> int = "ocaml_snd_pcm_writei_float_ba" external readn_float64 : handle -> float array array -> int -> int -> int = "ocaml_snd_pcm_readn_float64" external writen_float64 : handle -> float array array -> int -> int -> int = "ocaml_snd_pcm_writen_float64" external get_state : handle -> state = "ocaml_snd_pcm_get_state" external get_delay : handle -> int = "ocaml_snd_pcm_get_delay" external get_params : handle -> params = "ocaml_snd_pcm_get_params" external set_params : handle -> params -> unit = "ocaml_snd_pcm_set_params" type access = Access_rw_interleaved | Access_rw_noninterleaved external set_access : handle -> params -> access -> unit = "ocaml_snd_set_access" type fmt = | Format_s16_le (* TODO *) | Format_s24_3le | Format_float | Format_float64 external set_format : handle -> params -> fmt -> unit = "ocaml_snd_pcm_set_format" external set_rate_near : handle -> params -> int -> direction -> int = "ocaml_snd_pcm_set_rate_near" external set_channels : handle -> params -> int -> unit = "ocaml_snd_pcm_set_channels" external set_periods : handle -> params -> int -> direction -> unit = "ocaml_snd_pcm_set_periods" external get_period_size : params -> int = "ocaml_snd_pcm_get_period_size" external get_periods_max : params -> int * direction = "ocaml_snd_pcm_get_periods_max" external get_periods_min : params -> int * direction = "ocaml_snd_pcm_get_periods_min" external set_buffer_size : handle -> params -> int -> unit = "ocaml_snd_pcm_set_buffer_size" external set_buffer_size_near : handle -> params -> int -> int = "ocaml_snd_pcm_set_buffer_size_near" external get_buffer_size : params -> int = "ocaml_snd_pcm_get_buffer_size" external get_buffer_size_min : params -> int = "ocaml_snd_pcm_get_buffer_size_min" external get_buffer_size_max : params -> int = "ocaml_snd_pcm_get_buffer_size_max" external set_nonblock : handle -> bool -> unit = "ocaml_snd_pcm_set_nonblock" (* TODO *) let get_frame_size _ = 4 end module Sequencer = struct type t external create : string -> int -> int -> t = "ocaml_snd_seq_open" let create name ?(blocking=true) stream = let stream = match stream with | `Input -> 2 | `Output -> 1 | `Duplex -> 3 in let mode = if blocking then 0 else 1 in create name stream mode external set_client_name : t -> string -> unit = "ocaml_snd_seq_set_client_name" type port_caps = Port_cap_read | Port_cap_write | Port_cap_sync_read | Port_cap_sync_write | Port_cap_duplex | Port_cap_subs_read | Port_cap_subs_write | Port_cap_no_export type port_type = Port_type_specific | Port_type_MIDI_generic | Port_type_MIDI_GM | Port_type_MIDI_GM2 | Port_type_MIDI_GS | Port_type_MIDI_XG | Port_type_MIDI_MT32 | Port_type_hardware | Port_type_software | Port_type_sythesizer | Port_type_port | Port_type_application external create_port : t -> string -> port_caps list -> port_type list -> int = "ocaml_snd_seq_create_port" external subscribe_read_all : t -> int -> unit = "ocaml_snd_subscribe_read_all" external subscribe_write_all : t -> int -> unit = "ocaml_snd_subscribe_write_all" module Event = struct type note = { note_channel : int; note_note : int; note_velocity : int; note_off_velocity : int; note_duration : int; } type controller = { controller_channel : int; controller_param : int; controller_value : int; } type t = | System of int * int | Result of int * int | Note of note | Note_on of note | Note_off of note | Keypress of note | Controller of controller | Program_change of controller | Channel_pressure of controller | Pitch_bend of controller | Unhandled of int end type time = unit type event = { ev_event : Event.t; ev_time : time; } external input_event : t -> event = "ocaml_snd_seq_event_input" external output_event : t -> Event.t -> unit = "ocaml_snd_seq_event_output" end ocaml-alsa-0.3.0/src/alsa.mli000066400000000000000000000211401371354741100157410ustar00rootroot00000000000000(* Copyright 2005-2020 Savonet team This file is part of Ocaml-alsa. Ocaml-alsa 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. Ocaml-alsa 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 Ocaml-alsa; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *) (** * Interface with the alsa drivers. * * @author Samuel Mimram *) (** Get the ALSA sound library version in ASCII format. *) val get_version : unit -> string (** A buffer underrun / overrun occured. *) exception Buffer_xrun (** PCM is not in the right state. *) exception Bad_state (** A suspend event occurred (stream is suspended and waiting for an application * recovery). *) exception Suspended (** Input/output error. *) exception IO_error (** Device or resource was busy. *) exception Device_busy (** Function was called with an invalid argument. *) exception Invalid_argument (** This error can happen when device is physically * removed (for example some hotplug devices like USB * or PCMCIA, CardBus or ExpressCard can be removed on the fly). *) exception Device_removed exception Interrupted exception Unknown_error of int type direction = Dir_down | Dir_eq | Dir_up (** Get an error message corresponding to an error. * Raise the given exception if it is not known. *) val string_of_error : exn -> string (** Do not report errors on stderr. *) val no_stderr_report : unit -> unit module Pcm : sig (** Handle to a device. *) type handle (** Parameters of a device. *) type params (** Wanted stream. *) type stream = | Playback (** Playback stream. *) | Capture (** Capture stream. *) (** Modes for opening a stream. *) type mode = | Async (** Asynchronous notification (not supported yet). *) | Non_blocking (** Non blocking I/O. *) val open_pcm : string -> stream list -> mode list -> handle val close : handle -> unit (** Prepare PCM for use. *) val prepare : handle -> unit (** Resume from suspend, no samples are lost. *) val resume : handle -> unit (** Recover the stream state from an error or suspend. * This a high-level helper function building on other functions. * This functions handles Interrupted, Buffer_xrun and Suspended * exceptions trying to prepare given stream for next I/O. * Raises the given exception when not recognized/used. *) val recover : ?verbose:bool -> handle -> exn -> unit val start : handle -> unit (** Stop a PCM preserving pending frames. *) val drain : handle -> unit (** Stop a PCM dropping pending frames. *) val drop : handle -> unit (** [pause hnd pause] pauses (when [pause] is [true]) or resume (when [pause] * is [false]) a PCM. *) val pause : handle -> bool -> unit val reset : handle -> unit (** Wait for a PCM to become ready. The second argument is the timeout in * milliseconds (negative for infinite). Returns [false] if a timeout * occured. *) val wait : handle -> int -> bool (** [readi handle buf ofs len] reads [len] interleaved {i frames} in [buf] * starting at offset [ofs] (in bytes). It returns the actual number of * frames read. *) val readi : handle -> bytes -> int -> int -> int (** [writei handle buf ofs len] writes [len] interleaved {i frames} of [buf] * starting at offset [ofs] (in bytes). *) val writei : handle -> bytes -> int -> int -> int val readn : handle -> bytes array -> int -> int -> int val writen : handle -> bytes array -> int -> int -> int val readn_float : handle -> float array array -> int -> int -> int val writen_float : handle -> float array array -> int -> int -> int val readn_float_ba : handle -> (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t array -> int val writen_float_ba : handle -> (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t array -> int val writei_float_ba : handle -> int -> (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t -> int val readn_float64 : handle -> float array array -> int -> int -> int val writen_float64 : handle -> float array array -> int -> int -> int (** Get the delay (in frames). *) val get_delay : handle -> int (** State. *) type state = | St_open (** open *) | St_setup (** setup installed *) | St_prepared (** ready to start *) | St_running (** running *) | St_xrun (** stopped: underrun (playback) or overrun (capture) detected *) | St_draining (** draining: running (playback) or stopped (capture) *) | St_paused (** paused *) | St_suspended (** hardware is suspended *) | St_disconnected (** hardward is disconnected *) (** Get the current state. *) val get_state : handle -> state val get_params : handle -> params val set_params : handle -> params -> unit (** Access mode. *) type access = Access_rw_interleaved | Access_rw_noninterleaved (** Set the access mode. *) val set_access : handle -> params -> access -> unit (** Format of audio data. *) type fmt = | Format_s16_le (** 16 bits, little endian *) | Format_s24_3le | Format_float (** float 32 bit CPU endian *) | Format_float64 (** float 64 bit CPU endian *) (** Set the format of audio data. *) val set_format : handle -> params -> fmt -> unit (** [set_rate_near handle params rate dir] sets the sampling rate (in Hz). * If the rate is not avalaible, [dir] is used to determine the direction of * the nearest available sampling rate to use . The actual sampling rate used * is returned. *) val set_rate_near : handle -> params -> int -> direction -> int (** Set the number of channels. *) val set_channels : handle -> params -> int -> unit (** Set the number of periods. *) val set_periods : handle -> params -> int -> direction -> unit (** Get the number of periods. *) val get_periods_min : params -> int * direction val get_periods_max : params -> int * direction val get_period_size : params -> int (** Set the buffer size in {i frames}. *) val set_buffer_size : handle -> params -> int -> unit (** Set the buffer size near a value in {i frames}. *) val set_buffer_size_near : handle -> params -> int -> int val get_buffer_size : params -> int (* Get the buffer's size. *) val get_buffer_size_min : params -> int val get_buffer_size_max : params -> int (** Set blocking mode ([true] means non-blocking). *) val set_nonblock : handle -> bool -> unit (** Get the size of a frame in bytes. *) val get_frame_size : params -> int end module Sequencer : sig type t val create : string -> ?blocking:bool -> [ `Input | `Output | `Duplex] -> t val set_client_name : t -> string -> unit type port_caps = Port_cap_read | Port_cap_write | Port_cap_sync_read | Port_cap_sync_write | Port_cap_duplex | Port_cap_subs_read | Port_cap_subs_write | Port_cap_no_export type port_type = Port_type_specific | Port_type_MIDI_generic | Port_type_MIDI_GM | Port_type_MIDI_GM2 | Port_type_MIDI_GS | Port_type_MIDI_XG | Port_type_MIDI_MT32 | Port_type_hardware | Port_type_software | Port_type_sythesizer | Port_type_port | Port_type_application val create_port : t -> string -> port_caps list -> port_type list -> int (** Real all possible input ports on given port. *) val subscribe_read_all : t -> int -> unit (** Write all possible output ports from given port. *) val subscribe_write_all : t -> int -> unit module Event : sig type note = { note_channel : int; note_note : int; note_velocity : int; note_off_velocity : int; note_duration : int; } type controller = { controller_channel : int; controller_param : int; controller_value : int; } type t = | System of int * int | Result of int * int | Note of note | Note_on of note | Note_off of note | Keypress of note | Controller of controller | Program_change of controller | Channel_pressure of controller | Pitch_bend of controller | Unhandled of int end type time = unit type event = { ev_event : Event.t; ev_time : time; } val input_event : t -> event val output_event : t -> Event.t -> unit end ocaml-alsa-0.3.0/src/alsa_stubs.c000066400000000000000000000740321371354741100166320ustar00rootroot00000000000000/* * OCaml bindings for alsa * * Copyright 2005-2020 Savonet team * * This file is part of ocaml-alsa. * * ocaml-alsa 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. * * ocaml-alsa 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 ocaml-alsa; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * @author Samuel Mimram */ #include #include #include #include #include #include #include #include #include #include #include #include #include CAMLprim value ocaml_alsa_version(value unit) { return caml_copy_string(snd_asoundlib_version()); } typedef struct my_snd_pcm_t { snd_pcm_t *handle; int frame_size; } my_snd_pcm_t; /********** PCM **********/ static struct custom_operations pcm_handle_ops = { "ocaml_alsa_pcm_handle", NULL, custom_compare_default, custom_hash_default, custom_serialize_default, custom_deserialize_default }; #define Pcm_val(v) ((my_snd_pcm_t*)Data_custom_val(v)) #define Pcm_handle_val(v) Pcm_val(v)->handle #define Frame_size_val(v) Pcm_val(v)->frame_size #define int_of_direction(dir) (Int_val(dir)-1) #define direction_of_int(dir) (Val_int(dir+1)) static int int_of_pcm_stream(value stream) { int ans = 0; while (stream != Val_emptylist) { switch (Int_val(Field(stream, 0))) { case 0: ans |= SND_PCM_STREAM_PLAYBACK; break; case 1: ans |= SND_PCM_STREAM_CAPTURE; break; default: assert(0); } stream = Field(stream, 1); } return ans; } static int int_of_pcm_mode(value mode) { int ans = 0; while (mode != Val_emptylist) { switch (Int_val(Field(mode, 0))) { case 0: ans |= SND_PCM_ASYNC; break; case 1: ans |= SND_PCM_NONBLOCK; break; default: assert(0); } mode = Field(mode, 1); } return ans; } static void check_for_err(int ret) { if (ret >= 0) return; #ifdef DEBUG fprintf(stderr, "[DD] ocaml-alsa error: %s\n", strerror(-ret)); #endif switch(-ret) { case EIO: caml_raise_constant(*caml_named_value("alsa_exn_io_error")); break; case EBUSY: caml_raise_constant(*caml_named_value("alsa_exn_device_busy")); break; case EINVAL: caml_raise_constant(*caml_named_value("alsa_exn_invalid_argument")); break; case EPIPE: caml_raise_constant(*caml_named_value("alsa_exn_buffer_xrun")); break; case ESTRPIPE: caml_raise_constant(*caml_named_value("alsa_exn_suspended")); break; case EBADFD: caml_raise_constant(*caml_named_value("alsa_exn_bad_state")); break; case EINTR: caml_raise_constant(*caml_named_value("alsa_exn_interrupted")); break; case ENOTTY: case ENODEV: caml_raise_constant(*caml_named_value("alsa_exn_device_busy")); break; case EAGAIN: caml_raise_constant(*caml_named_value("alsa_exn_try_again")); break; default: caml_raise_with_arg(*caml_named_value("alsa_exn_unknown_error"), Val_int(-ret)); break; } } CAMLprim value ocaml_snd_int_of_error(value name) { CAMLparam1(name); const char *s = String_val(name); if (!strcmp(s,"alsa_exn_io_error")) CAMLreturn(Val_int(-EIO)); if (!strcmp(s,"alsa_exn_device_busy")) CAMLreturn(Val_int(-EBUSY)); if (!strcmp(s,"alsa_exn_invalid_argument")) CAMLreturn(Val_int(-EINVAL)); if (!strcmp(s,"alsa_exn_buffer_xrun")) CAMLreturn(Val_int(-EPIPE)); if (!strcmp(s,"alsa_exn_suspended")) CAMLreturn(Val_int(-ESTRPIPE)); if (!strcmp(s,"alsa_exn_bad_state")) CAMLreturn(Val_int(-EBADFD)); if (!strcmp(s,"alsa_exn_interrupted")) CAMLreturn(Val_int(-EINTR)); if (!strcmp(s,"alsa_exn_device_busy")) CAMLreturn(Val_int(-ENODEV)); caml_failwith("unknown value"); } CAMLprim value ocaml_snd_string_of_error(value n) { CAMLparam1(n); CAMLreturn(caml_copy_string(snd_strerror(-Int_val(n)))); } static void no_sterr_report_cb(const char *file, int line, const char *function, int err, const char *fmt, ...) { } CAMLprim value ocaml_snd_no_stderr_report(value unit) { CAMLparam1(unit); snd_lib_error_set_handler(no_sterr_report_cb); CAMLreturn(Val_unit); } #define Hw_params_val(v) (*((snd_pcm_hw_params_t**)Data_custom_val(v))) static void finalize_hw_params(value params) { snd_pcm_hw_params_free(Hw_params_val(params)); } static struct custom_operations hw_params_ops = { "ocaml_alsa_hw_params", finalize_hw_params, custom_compare_default, custom_hash_default, custom_serialize_default, custom_deserialize_default }; static value create_hw_params() { value ans; snd_pcm_hw_params_t *params; check_for_err(snd_pcm_hw_params_malloc(¶ms)); ans = caml_alloc_custom(&hw_params_ops, sizeof(snd_pcm_hw_params_t*), 0, 1); Hw_params_val(ans) = params; return ans; } CAMLprim value ocaml_snd_pcm_open(value name, value stream, value mode) { CAMLparam3(name, stream, mode); CAMLlocal1(ans); snd_pcm_sframes_t ret; my_snd_pcm_t *hnd; ans = caml_alloc_custom(&pcm_handle_ops, sizeof(my_snd_pcm_t), 0, 1); hnd = Pcm_val(ans); ret = snd_pcm_open(&(hnd->handle), String_val(name), int_of_pcm_stream(stream), int_of_pcm_mode(mode)); if (ret < 0) check_for_err(ret); hnd->frame_size = -1; CAMLreturn(ans); } CAMLprim value ocaml_snd_pcm_close(value handle) { CAMLparam1(handle); snd_pcm_close(Pcm_handle_val(handle)); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_prepare(value handle) { CAMLparam1(handle); check_for_err(snd_pcm_prepare(Pcm_handle_val(handle))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_resume(value handle) { CAMLparam1(handle); check_for_err(snd_pcm_resume(Pcm_handle_val(handle))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_recover(value handle, value err, value log) { CAMLparam1(handle); check_for_err(snd_pcm_recover(Pcm_handle_val(handle),Int_val(err),Bool_val(log))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_wait(value handle, value timeout) { CAMLparam2(handle, timeout); int ans; ans = snd_pcm_wait(Pcm_handle_val(handle), Int_val(timeout)); CAMLreturn(Val_int(ans)); } CAMLprim value ocaml_snd_pcm_readi(value handle_, value dbuf, value ofs_, value len_) { CAMLparam4(handle_, dbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); snd_pcm_t *handle = Pcm_handle_val(handle_); char *buf; snd_pcm_sframes_t ret; if (ofs + len * Frame_size_val(handle_) > caml_string_length(dbuf)) caml_invalid_argument("buffer"); buf = malloc(len * Frame_size_val(handle_)); caml_enter_blocking_section(); ret = snd_pcm_readi(handle, buf, len); caml_leave_blocking_section(); memcpy((void*)String_val(dbuf) + ofs, buf, len * Frame_size_val(handle_)); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_writei(value handle_, value sbuf, value ofs_, value len_) { CAMLparam4(handle_, sbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); snd_pcm_t *handle = Pcm_handle_val(handle_); char *buf; snd_pcm_sframes_t ret; if (ofs + len * Frame_size_val(handle_) > caml_string_length(sbuf)) caml_invalid_argument("buffer"); buf = malloc(len * Frame_size_val(handle_)); memcpy(buf, String_val(sbuf) + ofs, len * Frame_size_val(handle_)); caml_enter_blocking_section(); ret = snd_pcm_writei(handle, buf, len); caml_leave_blocking_section(); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_readn(value handle_, value dbuf, value ofs_, value len_) { CAMLparam4(handle_, dbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); int chans = Wosize_val(dbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); char **buf; int c; snd_pcm_sframes_t ret; /* TODO: check the size of dbuf */ buf = malloc(chans * sizeof(char*)); for(c = 0; c < chans; c++) buf[c] = malloc(2 * len); caml_enter_blocking_section(); ret = snd_pcm_readn(handle, (void**)buf, len); caml_leave_blocking_section(); for(c = 0; c < chans; c++) { memcpy((void*)String_val(Field(dbuf, c)) + ofs, buf[c], 2 * len); free(buf[c]); } free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_writen(value handle_, value sbuf, value ofs_, value len_) { CAMLparam4(handle_, sbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); int chans = Wosize_val(sbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); char **buf; int c; snd_pcm_sframes_t ret; /* TODO: check the size of sbuf */ buf = malloc(chans * sizeof(char*)); for(c = 0; c < chans; c++) { buf[c] = malloc(2 * len); memcpy(buf[c], String_val(Field(sbuf, c)) + ofs, 2 * len); } caml_enter_blocking_section(); ret = snd_pcm_writen(handle, (void**)buf, len); caml_leave_blocking_section(); for(c = 0; c < chans; c++) free(buf[c]); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_readn_float(value handle_, value dbuf, value ofs_, value len_) { CAMLparam4(handle_, dbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); int chans = Wosize_val(dbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); float **buf; int c, i; snd_pcm_sframes_t ret; /* TODO: check the size of dbuf */ buf = malloc(chans * sizeof(float*)); for(c = 0; c < chans; c++) buf[c] = malloc(len * sizeof(float)); caml_enter_blocking_section(); ret = snd_pcm_readn(handle, (void**)buf, len); caml_leave_blocking_section(); for(c = 0; c < chans; c++) { for(i = 0; i < len; i++) Store_double_field(Field(dbuf, c), i + ofs, buf[c][i]); free(buf[c]); } free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_writen_float(value handle_, value fbuf, value ofs_, value len_) { CAMLparam4(handle_, fbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); int chans = Wosize_val(fbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); float **buf; int c, i; snd_pcm_sframes_t ret; /* TODO: check the size of fbuf */ buf = malloc(chans * sizeof(float*)); for(c = 0; c < chans; c++) { buf[c] = malloc(len * sizeof(float)); for(i = 0; i < len; i++) buf[c][i] = Double_field(Field(fbuf, c), i + ofs); } caml_enter_blocking_section(); ret = snd_pcm_writen(handle, (void**)buf, len); caml_leave_blocking_section(); for(c = 0; c < chans; c++) free(buf[c]); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_readn_float_ba(value handle_, value dbuf) { CAMLparam2(handle_, dbuf); int chans = Wosize_val(dbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); int len = 0; float **buf; struct caml_ba_array *ba; snd_pcm_sframes_t ret; int c; buf = malloc(chans * sizeof(float*)); for(c = 0; c < chans; c++) { ba = Caml_ba_array_val(Field(dbuf, c)); if (c == 0) len = ba->dim[0]; else { if (ba->dim[0] != len) caml_failwith("Invalid argument"); } buf[c] = ba->data; } caml_enter_blocking_section(); ret = snd_pcm_readn(handle, (void**)buf, len); caml_leave_blocking_section(); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_writen_float_ba(value handle_, value fbuf) { CAMLparam2(handle_, fbuf); int chans = Wosize_val(fbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); int len = 0; float **buf = malloc(chans * sizeof(float*)); snd_pcm_sframes_t ret; struct caml_ba_array *ba; int c; for(c = 0; c < chans; c++) { ba = Caml_ba_array_val(Field(fbuf, c)); if (c == 0) len = ba->dim[0]; else { if (ba->dim[0] != len) caml_failwith("Invalid argument"); } buf[c] = ba->data; } caml_enter_blocking_section(); ret = snd_pcm_writen(handle, (void**)buf, len); caml_leave_blocking_section(); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_writei_float_ba(value handle_, value channels, value fbuf) { CAMLparam3(handle_, channels, fbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); int chans = Int_val(channels); struct caml_ba_array *ba = Caml_ba_array_val(fbuf); int len = ba->dim[0]; float *buf = ba->data; snd_pcm_sframes_t ret; caml_enter_blocking_section(); ret = snd_pcm_writei(handle, buf, len / chans); caml_leave_blocking_section(); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_readn_float64(value handle_, value dbuf, value ofs_, value len_) { CAMLparam4(handle_, dbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); int chans = Wosize_val(dbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); double **buf; int c, i; snd_pcm_sframes_t ret; /* TODO: check the size of fbuf */ buf = malloc(chans * sizeof(double*)); for(c = 0; c < chans; c++) buf[c] = malloc(len * sizeof(double)); caml_enter_blocking_section(); ret = snd_pcm_readn(handle, (void**)buf, len); caml_leave_blocking_section(); for(c = 0; c < chans; c++) { for(i = 0; i < len; i++) Store_double_field(Field(dbuf, c), i + ofs, buf[c][i]); free(buf[c]); } free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_writen_float64(value handle_, value fbuf, value ofs_, value len_) { CAMLparam4(handle_, fbuf, ofs_, len_); int len = Int_val(len_); int ofs = Int_val(ofs_); int chans = Wosize_val(fbuf); snd_pcm_t *handle = Pcm_handle_val(handle_); double **buf; int c, i; snd_pcm_sframes_t ret; /* TODO: check the size of fbuf */ buf = malloc(chans * sizeof(double*)); for(c = 0; c < chans; c++) { buf[c] = malloc(len * sizeof(double)); for(i = 0; i < len; i++) buf[c][i] = Double_field(Field(fbuf, c), i + ofs); } caml_enter_blocking_section(); ret = snd_pcm_writen(handle, (void**)buf, len); caml_leave_blocking_section(); for(c = 0; c < chans; c++) free(buf[c]); free(buf); check_for_err(ret); CAMLreturn(Val_int(ret)); } CAMLprim value ocaml_snd_pcm_start(value handle) { CAMLparam1(handle); snd_pcm_sframes_t ret; ret = snd_pcm_start(Pcm_handle_val(handle)); check_for_err(ret); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_drain(value handle) { CAMLparam1(handle); check_for_err(snd_pcm_drain(Pcm_handle_val(handle))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_drop(value handle) { CAMLparam1(handle); check_for_err(snd_pcm_drop(Pcm_handle_val(handle))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_pause(value handle, value pause) { CAMLparam2(handle, pause); check_for_err(snd_pcm_pause(Pcm_handle_val(handle), Int_val(pause))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_reset(value handle) { CAMLparam1(handle); check_for_err(snd_pcm_reset(Pcm_handle_val(handle))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_get_delay(value handle) { CAMLparam1(handle); long ans; check_for_err(snd_pcm_delay(Pcm_handle_val(handle), &ans)); CAMLreturn(Val_int(ans)); } CAMLprim value ocaml_snd_pcm_get_state(value handle) { CAMLparam1(handle); snd_pcm_state_t state; int ans; state = snd_pcm_state(Pcm_handle_val(handle)); switch (state) { case SND_PCM_STATE_OPEN: ans = 0; break; case SND_PCM_STATE_SETUP: ans = 1; break; case SND_PCM_STATE_PREPARED: ans = 2; break; case SND_PCM_STATE_RUNNING: ans = 3; break; case SND_PCM_STATE_XRUN: ans = 4; break; case SND_PCM_STATE_DRAINING: ans = 5; break; case SND_PCM_STATE_PAUSED: ans = 6; break; case SND_PCM_STATE_SUSPENDED: ans = 7; break; case SND_PCM_STATE_DISCONNECTED: ans = 8; break; default: assert(0); } CAMLreturn(Val_int(ans)); } CAMLprim value ocaml_snd_pcm_get_params(value handle) { CAMLparam1(handle); CAMLlocal1(ans); ans = create_hw_params(); check_for_err(snd_pcm_hw_params_any(Pcm_handle_val(handle), Hw_params_val(ans))); CAMLreturn(ans); } CAMLprim value ocaml_snd_pcm_set_params(value handle, value params) { CAMLparam2(handle, params); check_for_err(snd_pcm_hw_params(Pcm_handle_val(handle), Hw_params_val(params))); /* TODO: compute real value */ Frame_size_val(handle) = 4; CAMLreturn(Val_unit); } static int int_of_access(value access) { int a = Int_val(access); if (!a--) return SND_PCM_ACCESS_RW_INTERLEAVED; else if (!a--) return SND_PCM_ACCESS_RW_NONINTERLEAVED; else assert(0); } CAMLprim value ocaml_snd_pcm_set_access(value handle, value params, value access) { CAMLparam3(handle, params, access); check_for_err(snd_pcm_hw_params_set_access(Pcm_handle_val(handle), Hw_params_val(params), int_of_access(access))); CAMLreturn(Val_unit); } static int int_of_format(value format) { int f = Int_val(format); if (!f--) return SND_PCM_FORMAT_S16_LE; else if (!f--) return SND_PCM_FORMAT_S24_3LE; else if (!f--) return SND_PCM_FORMAT_FLOAT; else if (!f--) return SND_PCM_FORMAT_FLOAT64; else assert(0); } CAMLprim value ocaml_snd_pcm_set_format(value handle, value params, value format) { CAMLparam3(handle, params, format); check_for_err(snd_pcm_hw_params_set_format(Pcm_handle_val(handle), Hw_params_val(params), int_of_format(format))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_set_rate_near(value handle, value params, value rate_, value dir_) { CAMLparam4(handle, params, rate_, dir_); unsigned int rate = Int_val(rate_); int dir = int_of_direction(dir_); check_for_err(snd_pcm_hw_params_set_rate_near(Pcm_handle_val(handle), Hw_params_val(params), &rate, &dir)); CAMLreturn(Val_int(rate)); } CAMLprim value ocaml_snd_pcm_set_channels(value handle, value params, value chans) { CAMLparam3(handle, params, chans); check_for_err(snd_pcm_hw_params_set_channels(Pcm_handle_val(handle), Hw_params_val(params), Int_val(chans))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_set_periods(value handle, value params, value periods, value dir) { CAMLparam4(handle, params, periods, dir); check_for_err(snd_pcm_hw_params_set_periods(Pcm_handle_val(handle), Hw_params_val(params), Int_val(periods), int_of_direction(dir))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_get_periods_min(value params) { CAMLparam1(params); CAMLlocal1(result); int ret, dir ; unsigned int periods ; ret = snd_pcm_hw_params_get_periods_min(Hw_params_val(params), &periods, &dir); check_for_err(ret); result = caml_alloc_tuple(2); Store_field(result,0,Val_int(periods)); Store_field(result,1,direction_of_int(dir)); CAMLreturn(result); } CAMLprim value ocaml_snd_pcm_get_periods_max(value params) { CAMLparam1(params); CAMLlocal1(result); int ret, dir ; unsigned int periods ; ret = snd_pcm_hw_params_get_periods_max(Hw_params_val(params), &periods, &dir); check_for_err(ret); result = caml_alloc_tuple(2); Store_field(result,0,Val_int(periods)); Store_field(result,1,direction_of_int(dir)); CAMLreturn(result); } CAMLprim value ocaml_snd_pcm_get_period_size(value params) { CAMLparam1(params); snd_pcm_uframes_t ans; check_for_err(snd_pcm_hw_params_get_period_size(Hw_params_val(params), &ans, 0)); CAMLreturn(Val_int(ans)); } CAMLprim value ocaml_snd_pcm_set_buffer_size(value handle, value params, value size) { CAMLparam3(handle, params, size); check_for_err(snd_pcm_hw_params_set_buffer_size(Pcm_handle_val(handle), Hw_params_val(params), Int_val(size))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_set_buffer_size_near(value handle, value params, value size) { CAMLparam3(handle, params, size); snd_pcm_uframes_t s = Int_val(size); check_for_err(snd_pcm_hw_params_set_buffer_size_near(Pcm_handle_val(handle), Hw_params_val(params), &s)); CAMLreturn(Val_int(s)); } CAMLprim value ocaml_snd_pcm_get_buffer_size(value params) { CAMLparam1(params); snd_pcm_uframes_t ans; check_for_err(snd_pcm_hw_params_get_buffer_size(Hw_params_val(params), &ans)); CAMLreturn(Val_int(ans)); } CAMLprim value ocaml_snd_pcm_get_buffer_size_min(value params) { CAMLparam1(params); snd_pcm_uframes_t ans; check_for_err(snd_pcm_hw_params_get_buffer_size_min(Hw_params_val(params), &ans)); CAMLreturn(Val_int(ans)); } CAMLprim value ocaml_snd_pcm_get_buffer_size_max(value params) { CAMLparam1(params); snd_pcm_uframes_t ans; check_for_err(snd_pcm_hw_params_get_buffer_size_max(Hw_params_val(params), &ans)); CAMLreturn(Val_int(ans)); } static int get_access(value access) { int a = Int_val(access); if (!a--) return SND_PCM_ACCESS_RW_INTERLEAVED; else if (!a--) return SND_PCM_ACCESS_RW_NONINTERLEAVED; else assert(0); } CAMLprim value ocaml_snd_set_access(value handle, value params, value access) { CAMLparam3(handle, params, access); check_for_err(snd_pcm_hw_params_set_access(Pcm_handle_val(handle), Hw_params_val(params), get_access(access))); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_pcm_set_nonblock(value handle, value nonblocking) { CAMLparam1(handle); check_for_err(snd_pcm_nonblock(Pcm_handle_val(handle), Bool_val(nonblocking))); CAMLreturn(Val_unit); } /********** Sequencer **********/ static struct custom_operations seq_handle_ops = { "ocaml_alsa_seq_handle", NULL, custom_compare_default, custom_hash_default, custom_serialize_default, custom_deserialize_default }; #define Seq_val(v) (*((snd_seq_t**)Data_custom_val(v))) CAMLprim value ocaml_snd_seq_open(value name, value stream_, value mode_) { CAMLparam3(name, stream_, mode_); CAMLlocal1(seq); int stream = Int_val(stream_); int mode = Int_val(mode_); snd_seq_t *seq_handle = NULL; int ret; seq = caml_alloc_custom(&seq_handle_ops, sizeof(snd_seq_t*), 0, 1); ret = snd_seq_open(&seq_handle, String_val(name), stream, mode); check_for_err(ret); Seq_val(seq) = seq_handle; CAMLreturn(seq); } CAMLprim value ocaml_snd_seq_set_client_name(value seq, value name) { CAMLparam2(seq, name); int ret; ret = snd_seq_set_client_name(Seq_val(seq), String_val(name)); check_for_err(ret); CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_seq_create_port(value seq, value name, value _caps, value _type) { CAMLparam4(seq, name, _caps, _type); unsigned int caps = 0; unsigned int type = 0; static unsigned int capsv[] = {SND_SEQ_PORT_CAP_READ, SND_SEQ_PORT_CAP_WRITE, SND_SEQ_PORT_CAP_SYNC_READ, SND_SEQ_PORT_CAP_SYNC_WRITE, SND_SEQ_PORT_CAP_DUPLEX, SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_CAP_NO_EXPORT}; static unsigned int typev[] = {SND_SEQ_PORT_TYPE_SPECIFIC, SND_SEQ_PORT_TYPE_MIDI_GENERIC, SND_SEQ_PORT_TYPE_MIDI_GM, SND_SEQ_PORT_TYPE_MIDI_GM2, SND_SEQ_PORT_TYPE_MIDI_GS, SND_SEQ_PORT_TYPE_MIDI_XG, SND_SEQ_PORT_TYPE_MIDI_MT32, SND_SEQ_PORT_TYPE_HARDWARE, SND_SEQ_PORT_TYPE_SOFTWARE, SND_SEQ_PORT_TYPE_SYNTHESIZER, SND_SEQ_PORT_TYPE_PORT, SND_SEQ_PORT_TYPE_APPLICATION}; int port; while (_caps != Val_emptylist) { caps |= capsv[Int_val(Field(_caps, 0))]; _caps = Field(_caps, 1); } while (_type != Val_emptylist) { type |= typev[Int_val(Field(_type, 0))]; _type = Field(_type, 1); } port = snd_seq_create_simple_port(Seq_val(seq), String_val(name), caps, type); check_for_err(port); CAMLreturn(Val_int(port)); } /* Read from every possible port */ CAMLprim value ocaml_snd_subscribe_read_all(value _seq, value _dst) { CAMLparam2(_seq, _dst); snd_seq_t *seq = Seq_val(_seq); int dst = Int_val(dst); snd_seq_client_info_t *cinfo; snd_seq_port_info_t *pinfo; snd_seq_client_info_alloca(&cinfo); snd_seq_port_info_alloca(&pinfo); snd_seq_client_info_set_client(cinfo, -1); while (snd_seq_query_next_client(seq, cinfo) >= 0) { snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo)); snd_seq_port_info_set_port(pinfo, -1); // Avoid system device, midi through and self while (snd_seq_client_info_get_client(cinfo) != 0 && snd_seq_client_info_get_client(cinfo) != 14 && snd_seq_client_info_get_client(cinfo) != snd_seq_client_id(seq) && snd_seq_query_next_port(seq, pinfo) >= 0) { if ((snd_seq_port_info_get_capability(pinfo) & (SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ)) == (SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ)) { caml_release_runtime_system(); snd_seq_addr_t sender, dest; sender.client = snd_seq_client_info_get_client(cinfo); sender.port = snd_seq_port_info_get_port(pinfo); dest.client = snd_seq_client_id(seq); dest.port = dst; snd_seq_port_subscribe_t *subs; snd_seq_port_subscribe_alloca(&subs); snd_seq_port_subscribe_set_sender(subs, &sender); snd_seq_port_subscribe_set_dest(subs, &dest); snd_seq_subscribe_port(seq, subs); caml_acquire_runtime_system(); } } } CAMLreturn(Val_unit); } CAMLprim value ocaml_snd_subscribe_write_all(value _seq, value _src) { CAMLparam2(_seq, _src); snd_seq_t *seq = Seq_val(_seq); int src = Int_val(src); snd_seq_client_info_t *cinfo; snd_seq_port_info_t *pinfo; snd_seq_client_info_alloca(&cinfo); snd_seq_port_info_alloca(&pinfo); snd_seq_client_info_set_client(cinfo, -1); while (snd_seq_query_next_client(seq, cinfo) >= 0) { snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo)); snd_seq_port_info_set_port(pinfo, -1); while (snd_seq_client_info_get_client(cinfo) != 0 && snd_seq_client_info_get_client(cinfo) != 14 && snd_seq_client_info_get_client(cinfo) != snd_seq_client_id(seq) && snd_seq_query_next_port(seq, pinfo) >= 0) { if ((snd_seq_port_info_get_capability(pinfo) & (SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE)) == (SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE)) { caml_release_runtime_system(); snd_seq_addr_t sender, dest; sender.client = snd_seq_client_id(seq); sender.port = src; dest.client = snd_seq_client_info_get_client(cinfo); dest.port = snd_seq_port_info_get_port(pinfo); snd_seq_port_subscribe_t *subs; snd_seq_port_subscribe_alloca(&subs); snd_seq_port_subscribe_set_sender(subs, &sender); snd_seq_port_subscribe_set_dest(subs, &dest); snd_seq_subscribe_port(seq, subs); caml_acquire_runtime_system(); } } } CAMLreturn(Val_unit); } static value Val_note(snd_seq_ev_note_t n) { CAMLparam0(); CAMLlocal1(ans); ans = caml_alloc_tuple(5); Store_field(ans, 0, Val_int(n.channel)); Store_field(ans, 1, Val_int(n.note)); Store_field(ans, 2, Val_int(n.velocity)); Store_field(ans, 3, Val_int(n.off_velocity)); Store_field(ans, 4, Val_int(n.duration)); CAMLreturn(ans); } static value Val_controller(snd_seq_ev_ctrl_t c) { CAMLparam0(); CAMLlocal1(ans); ans = caml_alloc_tuple(3); Store_field(ans, 0, Val_int(c.channel)); Store_field(ans, 1, Val_int(c.param)); Store_field(ans, 2, Val_int(c.value)); CAMLreturn(ans); } CAMLprim value ocaml_snd_seq_event_input(value handle) { CAMLparam1(handle); CAMLlocal1(ans); CAMLlocal1(event); snd_seq_t *seq_handle = Seq_val(handle); snd_seq_event_t *ev = NULL; int ret = 0; caml_release_runtime_system(); ret = snd_seq_event_input(seq_handle, &ev); caml_acquire_runtime_system(); check_for_err(ret); assert(ev); switch (ev->type) { case SND_SEQ_EVENT_NOTEON: event = caml_alloc(1, 3); Store_field(event, 0, Val_note(ev->data.note)); break; case SND_SEQ_EVENT_NOTEOFF: event = caml_alloc(1, 4); Store_field(event, 0, Val_note(ev->data.note)); break; case SND_SEQ_EVENT_CONTROLLER: event = caml_alloc(1, 6); Store_field(event, 0, Val_controller(ev->data.control)); break; case SND_SEQ_EVENT_PGMCHANGE: event = caml_alloc(1, 7); Store_field(event, 0, Val_controller(ev->data.control)); break; case SND_SEQ_EVENT_PITCHBEND: event = caml_alloc(1, 9); Store_field(event, 0, Val_controller(ev->data.control)); break; default: // TODO: change this number when adding new constructors... event = caml_alloc(1, 10); Store_field(event, 0, Val_int(ev->type)); break; } ans = caml_alloc_tuple(2); Store_field(ans, 0, event); Store_field(ans, 1, Val_unit); CAMLreturn(ans); } CAMLprim value ocaml_snd_seq_event_output(value handle, value event) { CAMLparam2(handle, event); CAMLlocal1(v); snd_seq_t *seq_handle = Seq_val(handle); snd_seq_event_t ev; int ret = 0; snd_seq_ev_clear(&ev); //snd_seq_ev_set_source(&ev, Int_val(port)); snd_seq_ev_set_subs(&ev); snd_seq_ev_set_direct(&ev); switch (Tag_val(event)) { case 3: v = Field(event, 0); snd_seq_ev_set_noteon(&ev, Int_val(Field(v, 0)), Int_val(Field(v, 1)), Int_val(Field(v, 2))); break; case 4: v = Field(event, 0); snd_seq_ev_set_noteoff(&ev, Int_val(Field(v, 0)), Int_val(Field(v, 1)), Int_val(Field(v, 2))); break; default: caml_failwith("TODO: unhandled constructor"); break; } caml_release_runtime_system(); ret = snd_seq_event_output(seq_handle, &ev); caml_acquire_runtime_system(); check_for_err(ret); // TODO: other function for this caml_release_runtime_system(); ret = snd_seq_drain_output(seq_handle); caml_acquire_runtime_system(); check_for_err(ret); CAMLreturn(Val_unit); } ocaml-alsa-0.3.0/src/config/000077500000000000000000000000001371354741100155655ustar00rootroot00000000000000ocaml-alsa-0.3.0/src/config/discover.ml000066400000000000000000000010421371354741100177320ustar00rootroot00000000000000module C = Configurator.V1 let () = C.main ~name:"alsa-pkg-config" (fun c -> let default : C.Pkg_config.package_conf = { libs = ["-lasound"]; cflags = [] } in let conf = match C.Pkg_config.get c with | None -> default | Some pc -> ( match C.Pkg_config.query pc ~package:"alsa" with | None -> default | Some deps -> deps ) in C.Flags.write_sexp "c_flags.sexp" conf.cflags; C.Flags.write_sexp "c_library_flags.sexp" conf.libs) ocaml-alsa-0.3.0/src/config/dune000066400000000000000000000000751371354741100164450ustar00rootroot00000000000000(executable (name discover) (libraries dune.configurator)) ocaml-alsa-0.3.0/src/dune000066400000000000000000000004741371354741100152030ustar00rootroot00000000000000(library (name alsa) (public_name alsa) (synopsis "OCaml bindings to alsa") (foreign_stubs (language c) (names alsa_stubs) (flags (:include c_flags.sexp))) (c_library_flags (:include c_library_flags.sexp))) (rule (targets c_flags.sexp c_library_flags.sexp) (action (run ./config/discover.exe)))