I have to point out that your code is not thread safe and you could have some cases where the ioLoop thread doesn't run in between executions of render() (e.g.: because the system is busy doing something else and doesn't manage to schedule it on time), thus losing one block worth of readings. Having a circular buffer of adequate size would guarantee that no blocks are lost. Also, in principle there is no guarantee that the array variable's content will be fully written by the time the ioLoop thread sees that NewBlock is true, though for a single-core system like this, where ioLoop is guaranteed not to run alongside and/or preempt render(), then you'll probably be fine in practice.

Thanks for sharing your code. To make it more useful to the you probably want to remove stale references to serial and unused variables/unneeded includes.

What's all this for, btw?

    giuliomoro ahh got it - yes those are great suggestions - thank you!

    how's this update look? I've removed the stale references and implemented a circular buffer. any/all suggestions welcome-- thank you again!

    #include <Bela.h>
    #include <cmath>
    #include <iostream>
    #include <libraries/UdpClient/UdpClient.h>
    
    constexpr int kNumChannels = 8; // Number of ADC channels
    constexpr int kBufferSize = 64; // Total size of the circular buffer
    constexpr int kSamplesPerChannel = 8; //Number of samples transmitted per cycle per channel
    
    float input; //from Bela->context
    uint16_t circularBuffer[kNumChannels][kBufferSize]; // Circular buffer
    UdpClient udpClient(4567, "192.168.7.1");
    AuxiliaryTask serialCommsTask;
    
    int readIndex = 0;  // Current read index
    int writeIndex = 0; // Current write index
    
    void ioLoop(void* arg) {
        while (!Bela_stopRequested()) {
            if (readIndex != writeIndex) {
                uint16_t buffer[kNumChannels][kSamplesPerChannel];
                
                for (int channel = 0; channel < kNumChannels; ++channel) {
                    for (int sample = 0; sample < kSamplesPerChannel; ++sample) {
                        int index = (readIndex + sample) % kBufferSize;
                        buffer[channel][sample] = circularBuffer[channel][index];
                    }
                }
        
                udpClient.send(buffer, sizeof(buffer));
                readIndex = (readIndex + kSamplesPerChannel) % kBufferSize; // Move the read index to the next block
            }
            
            usleep(30);
        }
    }
    
    
    bool setup(BelaContext *context, void *userData) {
    	AuxiliaryTask serialCommsTask = Bela_createAuxiliaryTask(ioLoop, 0, "serial-thread", NULL);
    	Bela_scheduleAuxiliaryTask(serialCommsTask);
    	return true;
    }
    
    void render(BelaContext *context, void *userData)
    {
    	for(unsigned int n = 0; n < context->analogFrames; n++) {
    	for(unsigned int ar = 0; ar < kNumChannels; ar++) {
    	input = analogRead(context, n, ar); //read from ADC/context
    	circularBuffer[ar][writeIndex] = trunc(input*65535); //convert from float to int
        }
        writeIndex = (writeIndex + 1) % kBufferSize; // Increment write index in a circular manner
        }
    }
    
    void cleanup(BelaContext *context, void *userData) {}

    the code is for this instrument: www.instagram.com/p/CfhfSTBl-nB/

      violapwr the code is for this instrument: www.instagram.com/p/CfhfSTBl-nB/

      That's nice, I remember it struck me when it was announced last year: it seemed like it was doing all the right things.

      Now a question I've had for a while about what you are trying to do with Bela: why are you sending data over network from Bela to the host computer?

        giuliomoro ahh that's awesome to hear -- the instrument is coming along !! 🙂

        a few years back (when I first got the Bela) I ported the Pd code for the instrument from the Mac to the Bela. The problem was that Heavy doesn't support some objects; FFT was especially an issue (you had helped me to understand the scenario here: https://forum.bela.io/d/1698-optimizing-pd-patch-heavy-compatibility-other-ideas). Because I was caught up in hardware design stuff at that time, I decided best route was to keep the software running on the Mac, stick with the Arduino I was using for ADC, and later, down the road, work to re-code everything in C++ for the Bela. I'm at the point where a lot of the HW stuff is fixed and am now switching back to the Bela for the ADC (better conversion in comparison to Arduino). I'm still kicking the can down the road for the C++ port lol. Anyway, that's the backstory 🙂

          9 months later

          violapwr wanted to share an update on this in case useful to anyone; I finally got all the bugs out in this Bela analog data -> PD function I was building.
          -data is being transmitted with minimal errors and audio signals (over analog) actually sound good (as opposed to crunchy lol) when received on the PD end
          -uses OSC to send & sync sample rate, block size, and number of channels to PD

          below is the Bela code and here's link to the PD files:
          https://github.com/brianlindgren/Bela-to-PD-over-OSC

          #include <Bela.h>
          #include <cmath>
          #include <iostream>
          #include <libraries/UdpClient/UdpClient.h>
          #include <libraries/OscSender/OscSender.h>
          #include <libraries/OscReceiver/OscReceiver.h>
          #include <libraries/Pipe/Pipe.h>
          
          Pipe oscPipe;
          
          OscReceiver oscReceiver;
          OscSender oscSender;
          int localPort = 7562;
          int remotePort = 5000;
          const char* remoteIp = "192.168.7.1";
          
          constexpr int kBufferSize = 1024; // Total size of the circular buffer
          int kAnalogInChannels;
          int gAnalogFrames;
          int gAnalogSampleRate;
          
          float input; //from Bela->context
          uint16_t circularBuffer[kBufferSize]; // Circular buffer
          UdpClient udpClient(4567, "192.168.7.1");
          AuxiliaryTask serialCommsTask;
          
          int readIndex = 0;  // Current read index
          int writeIndex = 0; // Current write index
          
          
          void ioLoop(void* arg) {
              while (!Bela_stopRequested()) {
                  while (readIndex != writeIndex) { //send 1 block at time until caught up
                  
                      uint16_t buffer[kAnalogInChannels * gAnalogFrames]; // init transfer buffer
                      
                      //copy analog frames to transfer buffer
                      for (int frame = 0; frame < gAnalogFrames; ++frame) {
                      	//cycle through channels and copy content from circular buffer
                          for (int channel = 0; channel < kAnalogInChannels; ++channel) {
                              buffer[(frame * kAnalogInChannels) + channel] = circularBuffer[readIndex + ((frame * kAnalogInChannels) + channel)];
                          }
                      }
              
                      readIndex = (readIndex + (gAnalogFrames * kAnalogInChannels)); // Move the read index to the next block
                      
                      if (readIndex >= (kBufferSize - 1))  //wrap read pointer
              			readIndex = 0;
                      
          			udpClient.send(buffer, sizeof(buffer)); //send to PD
                  }
                  usleep(250);
              }
          }
          
          void on_receive(oscpkt::Message* msg, void*)
          {
          	// we make a copy of the incoming message and we send it down the pipe to the real-time thread
          	oscpkt::Message* incomingMsg = new oscpkt::Message(msg);
          	oscPipe.writeNonRt(incomingMsg);
          
          	// the real-time thread sends back to us the pointer once it is done with it
          	oscpkt::Message* returnedMsg;
          	while(oscPipe.readNonRt(returnedMsg) > 0)
          	{
          		delete returnedMsg;
          	}
          }
          
          bool setup(BelaContext *context, void *userData) {
          	/// from Bela example
          	oscPipe.setup("incomingOsc");
          	oscReceiver.setup(localPort, on_receive);
          	oscSender.setup(remotePort, remoteIp);
          
          	// the following code sends an OSC message to address /osc-setup
          	oscSender.newMessage("/osc-setup").send();
          
          	printf("Waiting for handshake ....\n");
          	// we want to stop our program and wait for a new message to come in.
          	// therefore, we set the pipe to blocking mode.
          	oscPipe.setBlockingNonRt(false);
          	oscPipe.setBlockingRt(true);
          	oscPipe.setTimeoutMsRt(1000);
          	oscpkt::Message* msg = nullptr;
          	int ret = oscPipe.readRt(msg);
          	bool ok = false;
          	if(ret > 0) {
          		if(msg && msg->match("/osc-setup-reply"))
          		{
          			printf("handshake received!\n");
          			ok = true;
          		}
          		delete msg;
          	}
          	if(!ok) {
          		fprintf(stderr, "No handshake received: %d\n", ret);
          		return false;
          	}
          	// in the remainder of the program, we will be calling readRt() from render(), and we want it
          	// to return immediately if there are no new messages available. We therefore set the
          	// pipe to non-blocking mode
          	oscPipe.setBlockingRt(false);
          	
          	AuxiliaryTask serialCommsTask = Bela_createAuxiliaryTask(ioLoop, 90, "serial-thread", NULL);
          	Bela_scheduleAuxiliaryTask(serialCommsTask);
          	printf("AnalogFrames: %d\n", context->analogFrames);
          	printf("AnalogInChannels: %d\n", context->analogInChannels);
          	printf("AnalogSampleRate: %d\n", (int)context->analogSampleRate);
          	gAnalogFrames = context->analogFrames; //# of analog frames
          	kAnalogInChannels = context->analogInChannels;
          	gAnalogSampleRate = (int)context->analogSampleRate;
          	oscSender.newMessage("/osc-settings").add(gAnalogSampleRate).add(gAnalogFrames).add(kAnalogInChannels).send();
          	oscPipe.writeRt(msg);
          	return true;
          }
          
          void render(BelaContext *context, void *userData)
          {
          	//write a message if read head couldn't catch up to write head last cycle
          	if (readIndex != writeIndex) {
                  rt_printf("udp catchup! >> readIndex: %d, writeIndex: %d\n", readIndex, writeIndex);
          	}
          
          	
          	//cycle through frames
          	for(unsigned int n = 0; n < gAnalogFrames; n++) {
          	
          		//cycle through channels and copy content to circular buffer
          		for(unsigned int ar = 0; ar < kAnalogInChannels; ar++) {
          			
          			input = analogRead(context, n, ar); //read from ADC/context
          			circularBuffer[writeIndex + ar] = input * 65535; //convert from float to int
          	    }
          	    // Increment write index every time a new frame is written and reset to 0 if too high
          	    writeIndex = (writeIndex + kAnalogInChannels); 
          	    
          	    if (writeIndex >= (kBufferSize - 1))
          	    	writeIndex = 0;
              }
          }
          
          void cleanup(BelaContext *context, void *userData) {}
          a year later

          very strange problem I'm having...

          for some reason the Bela all of a sudden had trouble communicating with PD over OSC. I wiped and reflashed the ssd but still not working (with the code above). I'm getting this error: no viable conversion from 'void (oscpkt::Message *, void *)' to 'std::function<void (oscpkt::Message *, const char *, void *)>' column: 31, line: 73

          if i run \example Communication/OSC-pipe/render.cpp the code runs & i'm able to receive 'osc-setup' in PD, but the 'osc-setup-reply' is not received on the Bela, with PD reporting this error send: No route to host (65)

          come to think of it, a few days ago, git (on Bela) was giving me a seg fault, so i tried to update/reinstall it using apt (which did not work)... hopefully I did not screw up anything .... 😳

          any ideas?

            Probably your code needs to be amended tobe compatible with the code on the board. There was an API change which is what makes the example work but your project doesn't

            It should be as easy as replacing

            void on_receive(oscpkt::Message* msg, void*)

            with

            void on_receive(oscpkt::Message* msg, const char*, void*)

            @giuliomoro that worked, the program now compiles! thanks!

            however, i'm still getting this error (both my program and the example project) when PD responds to Bela with osc-setup-reply: send: No route to host (65). when i ping 192.168.7.2 from my computer, the Bela responds, so the IP seems to be correct...

            any other ideas?

            Can you run ifconfig on the board (in the console at the bottom of the IDE) and show the results here? Also, run ping -c 1 192.168.7.1 in there and see what comes back

              giuliomoro OK, this is what was returned:

              root@bela ~/Bela# ifconfig
              bash: line 6: ifconfig: command not found

              and

              root@bela ~/Bela# ping -c 1 192.168.7.1
              PING 192.168.7.1 (192.168.7.1) 56(84) bytes of data.
              64 bytes from 192.168.7.1: icmp_seq=1 ttl=64 time=1.09 ms
              --- 192.168.7.1 ping statistics ---
              1 packets transmitted, 1 received, 0% packet loss, time 0ms
              rtt min/avg/max/mdev = 1.099/1.099/1.099/0.000 ms

              giuliomoro

              root@bela ~/Bela# ip a
              1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
                  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
                  inet 127.0.0.1/8 scope host lo
                     valid_lft forever preferred_lft forever
                  inet6 ::1/128 scope host 
                     valid_lft forever preferred_lft forever
              2: eth0:  mtu 1500 qdisc mq state DOWN group default qlen 1000
                  link/ether 4c:3f:d3:19:7a:91 brd ff:ff:ff:ff:ff:ff
              3: wlan0:  mtu 1500 qdisc noop state DOWN group default qlen 1000
                  link/ether 1c:bf:ce:65:1a:fd brd ff:ff:ff:ff:ff:ff
              4: usb0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
                  link/ether be:1a:d3:19:7a:93 brd ff:ff:ff:ff:ff:ff
                  inet 192.168.6.2/24 brd 192.168.6.255 scope global usb0
                     valid_lft forever preferred_lft forever
                  inet6 fe80::bc1a:d3ff:fe19:7a93/64 scope link 
                     valid_lft forever preferred_lft forever
              5: usb1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
                  link/ether be:1a:d3:19:7a:96 brd ff:ff:ff:ff:ff:ff
                  inet 192.168.7.2/24 brd 192.168.7.255 scope global usb1
                     valid_lft forever preferred_lft forever
                  inet6 fe80::bc1a:d3ff:fe19:7a96/64 scope link 
                     valid_lft forever preferred_lft forever

              violapwr if i run \example Communication/OSC-pipe/render.cpp the code runs & i'm able to receive 'osc-setup' in PD, but the 'osc-setup-reply' is not received on the Bela, with PD reporting this error send: No route to host (65)

              also can you show the Pd patch?

              @"giuliomoro"#p27136

              PD:

              this code is inside pd handshake (shown in the 2nd image):

              in pd can you use "find last error" and see where it points to?

                giuliomoro PD does not seem to be able to identify the error source, strangely:

                (in the back of my mind, I keep wondering if I messed up the system when trying to fix the git segmentation fault and need to flash the emmc (since flashing the SSD has not yet fixed it).... )

                I don't think it's a bela problem though ...
                can you try a simpler patch:

                [connect 192.168.7.2 7562(
                |
                | [1 2 3 4(
                |/
                [netsend -u -b]

                do you get the same "no route to host" error?

                  giuliomoro Yes, same error. I received a successful connect (1) when clicking connect 192.168.7.2 7562. Clicking 1 2 3 4 resulted in the same error and a disconnect (0)