Hi,
I have a project involving a teensy card for synthesis and a Bela as an effects processor for the teensy. Now I have 3 possible ways to send audio from one card to the other: analog audio, usb audio and (not sure at all how...) teensy can send an audio stream though the i2c bus, maybe this is another option. I guess a digital link would be the cleanest option but this is not supported by the Bela, am I wrong?

Analog audio is without doubt the least painful one. USB audio is not an option, because you would not be able to maintain the low-latency of Bela (though you could in principle load the audio cape overlay and use the Bela cape via ALSA/Jack, but then you would lose access to the analog I/O and all the Bela programming environment).

In principle I2S should be doable, and also an interesting application. Getting that to work would require some changes in:
- the McASP configuration (lines 779-846 in pru_rtaudio.p, perhaps more),
- the Bela TLV320AIC3104 codec configuration (core/I2c_Codec.cpp and core/RTAudio.cpp), described here.
- possibly the Teensy I2S configuration.

The Beaglebone's McASP (Multi-channel audio serial port) is described in detail in the am335x technical reference manual. It allows plain I2S mode, as well as TDM (time-division multiplexing), to host up to 32 channels per port, possibly from multiple devices/codecs, assuming all devices/codecs support it. There are three clocks (interface clock, frame clock, bit clock) which you will need to configure properly (I think currently the McASP generates the interface clock, but the frame clock and bit clock are generated by the TLV320AIC3104 codec and sent back into the McASP.

0) So, the normal situation on Bela is

           I2S                               I2S
Bela codec --> McASP --> Bela code --> McASP --> Bela codec --> analog outs

1) You would probably use the Teensy's I2S output into the BBB's McASP, but then use the TLV320 codec for the final analog output:

             I2S                               I2S
Teensy codec --> McASP --> Bela code --> McASP --> Bela codec --> analog outs

2) If TDM is available on the Teensy codec, you could even figure out a way to do:

             TDM (slots2-3)
Teensy codec ------->|                              TDM
                     | McASP --> Bela code --> McASP --> Bela codec --> analog outs
Bela codec   ------->|
             TDM(slots0-1)

3) You could also explore the possibility of sending the Bela audio out back over I2S to the Teensy codec, if you can then send it to the analog output there (it seems unlikely to me that this is possible, but I know nothing about the Teensy). If this option would work, it could simplify things as you could simply disable the TLV320 codec and only deal with one codec (the Teensy's) on the McASP, however it requires some "magic" (loopback?) on the Teensy.

             I2S                               I2S
Teensy codec --> McASP --> Bela code --> McASP --> Teensy codec --> magic happens -->analog outs

In cases 1) and 3), the Bela audio code would not see any change with respect to 0). In case 2), you would have to make it aware of the extra audio inputs (as you would have 4!).

10 months later
3 months later

minovski How many i2s channels (or microphones ) do bela support at its maximum ?

Each I2S port can only have 2 input and 2 output channels. However, on the BeagleBone, the same ports (called McASP) can be turned into TDM (time-division multiplexing), supporting up to 16 inputs and 16 outputs, which is what we do for the CTAG (although we only use 8 inputs). Additionally, you could think of bitbanging the protocol on some spare pins, as minovski did above, thus increasing that count, but I am not sure then what the maximum number of channels would be at that point!

Clearly, this is just for the digital communication to the board, however, you'll also need some ADC converters that convert from analog to I2S, if you really want to increase the channel count.

    giuliomoro We have a board, which has support tdm intput and output.
    So how can i connect my tdm board with bela?

    You need to be more specific than that, and be prepared, this is a serious undertaking.
    What do you want to achieve?

    5 months later

    Thanks for the info on I2S on bela, @giuliomoro and @minovski !

    I would like to connect a bluetooth module to a bela mini to stream 2 channel audio (or, perhaps, also stream a microphone signal back in a headset mode while receiving a monaural audio stream). The I2S protocol appears to be best-suited for that. But getting this to work looks a little complicated to me.
    If I understand correctly, the MPU's McASP units can be configured to the I2S protocol, but the available pins on the pocketbeagle only allow for use of McASP0 (and a few McASP1 pins). Now some of these pins are already required for the bela cape.
    Hence, receiving an I2S signal could still be possible via these pins:
    - P2.04 (mode 6, McASP_AXR)
    - P2.08 (mode 6, McASP_ACLKR)
    - P2.06 (McASP_FSX)
    However, transmitting a signal back is not possible, since pin P1.36 (McASP_ACLKX) is already occupied by the bela cape (although pins P2.06 (McASP_FSX) and P2.02 (McASP_AXR) are available).

    So this is where minovski 's solution could come in, implementing the I2S protocol via the bela mini's second PRU. However, the code references several hex addresses for pins, which are a bit hard to decipher. That makes adapting this from the bela to the bela mini a bit difficult for me. Could I just enter the addresses for any pin connected to PRU1 there in pru_gpio.p (i.e., P1.02, P1.04, P1.30, P1.32, P1.35 and P2.11, P2.31, P2.35)?
    Many of these pins are used for digital IO on the bela mini, but can be reconfigured via the pinmux, I read.

    Is all this correct? Is there a better way to integrate a bluetooth connection?

    Would it then be possible to read the I2S input via the audioIn function or would I have to implement another function for this purpose?

    Cheers

      Would you use the Bela codec as well? Would you send the same signal to the Bela audio out and the I2S BT module?

        giuliomoro Yes, I would like to use the bela codec and
        case a): Process the streamed stereo audio coming in from the bluetooth on the bela, then send it via Bela audio out,
        case b): Enable calling via bluetooth, i.e., processing a mono audio stream coming in via bluetooth, and a microphone input via analog audio in on bela, then sending the mix via audio out and, also, the microphone signal out via bluetooth.

        4 days later

        Depending on your application, Bluetooth latency might be an inherent issue.

        I have yet to see audio Bluetooth that provides anything close to the latencies a musician could use in real time. Bt for playback of a of previously recorded streams is fine, but nothing where you are interactively creating the sound and monitoring it

        a year later

        najan Hi - did you ever get this to work on the Bela mini?

        I want to use the second PRU to create an in-series I2S out/in on the bela mini to create a sort of effect send/return loop that can delegate to a second chipset (like a dedicated M4 for instance). Also currently struggling a bit getting my head around the pin addresses ...

          adriaan I want to use the second PRU to create an in-series I2S out/in on the bela mini to create a sort of effect send/return loop that can delegate to a second chipset (like a dedicated M4 for instance).

          You wouldn't necessarily need a separate PRU. You would probably be better off just adding I/O ports to the current McASP configuration, or move from using two channels per data line to having 16 channels (the way it's done on the CTAG BEAST), which is compatible with Bela Mini codec's "256-clock mode". In the second case you'd need the M4 (or whatever your external device is) to be able to handle a TDM stream (instead of I2S).

            giuliomoro Thanks for this! (sorry I only noticed your reply now)

            I do want to stick with a dedicated I2S, because the second board will have to interact with other 3rd party hardware, and I2S seems to be a well adopted format. Basically, we want to use the Bela for its ADC & DCA (and some sensors via I2C) as a test harness & dev platform.

            Also very interested to figure out how everything's connected, so getting the seconds PRU up & running is a good exercise.

              adriaan I do want to stick with a dedicated I2S, because the second board will have to interact with other 3rd party hardware, and I2S seems to be a well adopted format.

              The best option is still to use other data channels of the McASP. There are up to 4 data channels on each McASP, each of which can be an input or an output. The format (I2S vs TDM) will be the same for all the channels. So if using the codec in I2S mode on two data channels (one for input one for output), you can use another two channels (one for input, one for output) in I2S mode.

                giuliomoro Ah ok, thanks - I'll have a look into this.

                Presumably the code I load onto PRU0 will have to retain the existing functionality, or I'll lose audio input/output altogether, or are you saying this is possible through config only?

                If it's the former, is this the right code to base my changes on: /root/Bela/pru/pru_rtaudio.p?

                There would be only one PRU running and it would feed the McASP 2x the amount of data. The McASP would then split that data across the 2x as many data lines. It's best to start from pru_rtaudio_irq.p because it has the McASP FIFOs enabled. That is quite some work, however. We have an in-house solution for this which works great but it took weeks of work to get there.

                3 years later
                7 months later

                Dear list,

                Is there a straight forward tutorial/guide to implement an I2S microphone on Bela mini yet? The (ultra) small form factor of the microphone is essential in the project I am working on as is the small form factor of the mini bela itself. The microphone would be working as a gate but also for pitch detection.

                Context:
                I had this running as an analogue solution on a pocketbeagle using an analogue input on that board utilizing the PRU, but the large difference in volume between noise and sinusoidal sounds on my instrument could not be handled by the limited bit depth of these inputs (and large bit noise on the lower bits). Plus, the piezo microphone I used took too much of the surrounding sound to properly work as a gate especially when using soft noisy sounds. The idea is to put the microphone closer to the source (trumpet lead pipe) and as such make it much smaller.

                Best and thanks, Hans.

                What's the distance between the microphone and the BelaMini? Anything more than a few cm may have signal quality issues if you are trying using I2S.