From mtmorg@n@b|oc @end|ng |rom gm@||@com  Wed May  6 22:04:19 2020
From: mtmorg@n@b|oc @end|ng |rom gm@||@com (Martin Morgan)
Date: Wed, 6 May 2020 20:04:19 +0000
Subject: [Rd] defining r audio connections
In-Reply-To: <d6158363-d59a-3fa9-7ccd-0a80985094ab@gmail.com>
References: <20200506170919.GA34684@localhost>
 <d6158363-d59a-3fa9-7ccd-0a80985094ab@gmail.com>
Message-ID: <BL0PR04MB6609459170C7D73F98768D17F9A40@BL0PR04MB6609.namprd04.prod.outlook.com>

The public connection API is defined in

https://github.com/wch/r-source/blob/trunk/src/include/R_ext/Connections.h

I'm not sure of a good pedagogic example; people who want to write their own connections usually want to do so for complicated reasons!

This is my own abandoned attempt https://github.com/mtmorgan/socketeer/blob/b0a1448191fe5f79a3f09d1f939e1e235a22cf11/src/connection.c#L169-L192 where connection_local_client() is called from R and _connection_local() creates and populates the appropriate structure. Probably I have done things totally wrong (e.g., by not checking the version of the API, as advised in the header file!)

Martin Morgan

?On 5/6/20, 2:26 PM, "R-devel on behalf of Duncan Murdoch" <r-devel-bounces at r-project.org on behalf of murdoch.duncan at gmail.com> wrote:

    On 06/05/2020 1:09 p.m., frederik at ofb.net wrote:
    > Dear R Devel,
    > 
    > Since Linux moved away from using a file-system interface for audio, I think it is necessary to write special libraries to interface with audio hardware from various languages on Linux.
    > 
    > In R, it seems like the appropriate datatype for a `snd_pcm_t` handle pointing to an open ALSA source or sink would be a "connection". Connection types are already defined in R for "file", "url", "pipe", "fifo", "socketConnection", etc.
    > 
    > Is there a tutorial or an example package where a new type of connection is defined, so that I can see how to do this properly in a package?
    > 
    > I can see from the R source that, for example, `do_gzfile` is defined in `connections.c` and referenced in `names.c`. However, I thought I should ask here first in case there is a better place to start, than trying to copy this code.
    > 
    > I only want an object that I can use `readBin` and `writeBin` on, to read and write audio data using e.g. `snd_pcm_writei` which is part of the `alsa-lib` package.

    I don't think R supports user-defined connections, but probably writing 
    readBin and writeBin equivalents specific to your library wouldn't be 
    any harder than creating a connection.  For those, you will probably 
    want to work with an "external pointer" (see Writing R Extensions). 
    Rcpp probably has support for these if you're working in C++.

    Duncan Murdoch

    ______________________________________________
    R-devel at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] defining r audio connections
Martin Morgan