GithubHelp home page GithubHelp logo

Comments (2)

projectgus avatar projectgus commented on August 16, 2024

Hi @bwinterstine,

Wow, that's a lot of latency! I couldn't say for sure, but my guess is that Bluetooth (probably the HC-05 but maybe also the macOS end of the Bluetooth connection) is adding latency. Not because of the baud rate, though (38400bps is still almost 5 bytes per millisecond). The baud rate only applies to the wired serial side of the HC-05's communications, not anything from the Bluetooth side onwards.

(The other reason I'm fairly sure this isn't a Hairless problem is that I don't think there have been any other bug reports of such high latency when using wired serial connections like USB.)

As a protocol, Bluetooth is optimized for low power not low latency. If the connection seems to be idle, the master device (your Mac) will put the slave device (the HC-05) from the "active" state into the "sniff" state, where it will minimize its radio usage (for lowest power), and only wake up to communicate at fixed intervals - can easily be 100ms or more. There's a good overview of Bluetooth Classic modes here. If the device stays idle it can even go into lower power states, but it seems like "sniff" is the most likely mode we're in here.

On the other hand, both MIDI and plain serial are byte-oriented protocols. So the HC-05 will receive MIDI serial bytes one at a time over the plain serial link, but it will buffer these values and only send a BT packet (with all the bytes) when the connection becomes available (in "sniff" mode it can't send the first packet immediately, because the other end of the connection isn't even listening - it has to wait for the interval to complete).

A quick search for "HC-05 latency" turns up a ton of complaints about bad latency, although most of them don't have a lot of info. This post seems particularly good. They talk about the HC-05 going into a low power mode if the connection is idle for >2-3 seconds, and at this point the latency is >40ms. But I think there are multiple different firmware floating around on these HC-05s, so the default idle timeout and the delay to go into sniff mode might vary. Also, the sniff time will be set by agreement between the macOS and the HC-05 end of the connection.

My suggestions would be:

  • It looks like HC-05 modules support AT commands including some which can disable the "sniff" mode, or set the sniff sleep mode parameters. They don't seem well documented, but there's some discussion here that seems to link most of the essentials. Note that in that thread the poster is trying to minimize power consumption by going to sniff mode often, you want the opposite (or maybe to turn sniff mode off and keep the connection permanently in the active mode.)
  • If you really want to do dig into this, apparently Xcode has a "packet logger" app for Bluetooth and you can then analyze the packet data with tools like Wireshark to see what the connection parameters are, and how often the HC-05 is transmitting.
  • There might be some other tools in MacOS that you can use to tune the Bluetooth connection from the Mac's side, I don't know much about Macs.
  • Switch to an RF technology that has better latency characteristics.

If you try all of this and you're really sure it's something in Hairless let me know, but I'd bet money on Bluetooth at this point.

from hairless-midiserial.

bwinterstine avatar bwinterstine commented on August 16, 2024

I agree, this latency is not due to the HC-05 or Bluetooth, alone. I had not considered "sniff" mode and need to evaluate this as you suggested. In the example above, if it was the first time I struck the key (after setting up the recorder) it could have very well been in some low power mode because it wasn't active. BUT, for these tests, I would strike the keys at least 5 times and I would expect Sniff to deactivate after the first key strike. All 5 key strikes had similar bad latency values, they didn't improve with time. Each key strike was about 2-3 seconds apart though...maybe... it was continuously entering sniff mode between key strikes?

I connected a Midi Keyboard with a USB interface to my MAC and repeated the tests. The latency is still between 60-85ms. The USB Midi keyboard does not use Hairless so this latency is on the Macbook Pro. The keyboard has zero measurable lag when connected to a Korg Kronos (via a midi DIN connector).

I am leaning toward the Mac USB interface and Mac Bluetooth interface both have high latency for some reason. I may need to look at using Wireshark.

I will close this issue because I have to agree, it is not a Hairless issue unless it is something recent related to MacOS Catalina (64 bit latest OS).

Thank you for your response and support!

from hairless-midiserial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.