webpage (django) <-> midi event (alsa) in Linux - linux

I would like to create a web application that sends and receives ALSA MIDI messages on Linux. Only one web client is intended.
What kind of architecture / programs do I need for that?
I am familiar with django but can't find the missing link to ALSA (or any system with a gateway to ALSA on my Ubuntu machine). Also, I have the small program ttymidi (http://www.varal.org/ttymidi/), that sends messages from a serial port to ALSA.

You might be able to use Python's miscellaneous operating system interfaces, but web applications aren't often designed in this way. You may also have to worry about latency and buffering in your program.

The simplest way of doing what you want without any third-party library is to use pyALSA, which is the official python wrapper around the C ALSA library.
I recommend you dealing with the Sequencer API instead of the RawMIDI stuff, which is lower-level. Check out some of the test apps and the C API documentation, it will definetely help you to write your code.

Related

Hardware communication with Python

Is it possible to write an API with Python so you can connect a physical ON and OFF switch via USB to a PC and when user presses the switch to ON or OFF, the python program detects it and send a signal to a web app and shows ON or OFF message on the website?
I am sorry if what I am asking its not clear enough!
Yes, it is possible. Reading USB devices can be done with Python. In linux USB device inputs can be found in some files(e.g. /dev/ttyUSB0). By reading those files you can get the information that you need. Putting here link that will be helpful
similar post
Firstly, you can't write an API to interact with hardware in python. You would have to use the pre-existing windows API(or the API provided by the Operating system that you are using) in order to interact with hardware in such a high-level language.
If you want to interact with hardware in python, and detect switch presses, releases etc, I would recommend you used a microcontroller such as a raspberry pi(for python) or an arduino(for C++). The respberry pi provides a very easy way to interact with hardware in python. If you still want to interact with a USB stick in python(but not acting as a switch) you can use the pyusb library.

what is the difference between api and device files in linux?

Difference between an API and a device driver
From the above link i read that API is like a specification that describes what to do, while a driver is an implementation that describes how to do it.
Now, i couldn't find API in linux for display, audio etc.I have also read on internet that linux provides device files to interact with device drivers. we can communicate to devices by writing or reading in those files but as written above API is the specification that describes what to do and API layer is missing here.so, i don't know what commands to writes in those files to interact with devices. ex-rasterize a image on display with the help of these device files.
Device files are just a practical way to communicate between user space and the kernel. Some device files (most notably, block devices) have a uniform API to them, but that's kinda besides the point.
For most standard operations, you would not interact directly with a device file, but instead use a library, exposing a documented API, for doing what you want. So, if you want to play sound files, you'll use, e.g., libjack, or even a higher abstraction layer, such as gstreamer or libvlc.
It is possible, and even likely, that those library use a device file for their actual output. You need not deal with that, unless you want to.
In other cases, you do want to open the device file and interact with it. In those cases, you need to read the relevant documentation to see how to do that. Some device files merely accept read and write requests. Others, such as tty devices, have ioctl commands that modify how they work. The man page for the relevant device will tell you what you need to know.
In general, many treat device files as extension of the kernel's API. In fact, many call the ioctl command "user defined syscalls". In all cases, just read the documentation to see what you need to do.

Bluez server for bidirectional communication

I want to create on my Linux desktop a small server listening to requests using Bluetooth. Clients (such as mobile phones or tablets) will connect to this server and exchange data back and forth.
It should be straightforward, but I'm unable to find an up-to-date tutorial for Bluez's new DBUS-based API, and Bluez documentation is basically just a huge data dump.
Any suggestions on how I should proceed? (The language used does not really matter, since there are DBUS bindings for all major languages.)
These are some useful links to get started, it's not much but it's a start.
Textual (and up-to-date) description of the DBus interfaces exposed by bluez5 : https://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc
You can find example for gatt client/server in the prevous cgit at the following path : /tree/src/shared/
An overview of Bluez and it's dbus interfaces (conference given at the 2016 Open IoT Summit) : https://www.youtube.com/watch?v=tclS9arLFzk
If you need example for your dbus bindings, I suggest looking at their test files
As you said yourself, there are dbus bindings in many languages however the language does matter. For example, some old low-level C API are not advised (see for yourself the advices in the dbus tutorial on freedesktop.org)
I suggest the following steps to start (especially for LE) :
Read the adapter-api.txt (first link) description and try to build a proxy to interact the org.bluez.Adapter1 interface (when trying to build the proxy: name would be 'org.bluez' and object path '/org/bluez/hci0' as describded in adapter-api.txt). Call StartDiscovery and StopDiscovery
Once scanning is done, print your proxy introspection to find the devices discovered (you should see MAC addresses preceded by "dev_")
Build proxies to interact with the device (read the device-api.txt file description to find out what you need)
For LE, if you want to access the Services of a Device. Introspect your device proxy and you will find it'serices. Repeat the process to reach Characteristics and Descriptors.

The relationship between Firmata, Arduino and Node js

This is a basic question of understanding. I'm trying to follow this explanation http://www.barryvandam.com/node-js-communicating-with-arduino/ but something there sounds a bit off to me.
As far as I understood before, I only need to push the standard Firmata code into the Arduino and code via Node.js to call actions and information from the Arduino.
But in this link they point our that I need to upload a code to the Arduino, which will obviously delete the Firmata code that is now there. wouldn't it result in loosing connection to the Arduino?
How does it work?
many thanks!
The example above does not use Firmata, though Firmata makes things easier. If you want to implement your own serial control protocol, there nothing stopping you.
However, if you load StandardFirmata on your Arduino, and then use the Firmata.js package in node, then you have a full-featured, well established serial protocol and an api to access the pins from.
There are Firmata controller implementations for most languages out there.
On top of Firmata.js, you might choose to use an abstraction called Johnny-Five. Johnny-Five abstracts the pins as components, with an intuitive API. Instead of controlling a servo by setting a pin value, you create a servo, and call servo.to(angle). It is kind of like jQuery but for hardware. It abstracts the platforms and devices in such a way that the shape of the interface is the same, but the hardware might be completely different. It is a very comfortable place to develop from.
Firmata is a set of functions that you can pre-load onto your Arduino that libraries like Johnny-Five can call to perform certain tasks.
As previously said by the author of the accepted answer, you don't have to use it, you can write your own logic to send and receive custom message.
In Node.js you can use the node-serialport library to send the message:
myPort.write();
Then read the message from the Arduino with the built in Serial library:
while(radio.available()){
radio.read(chr, 1);
}
Firmata is one way to communicate with an Arduino, you do not have to use it.
You can "talk" to Arduindo directly using the SerialPort module - which is what the linked example is doing.

What is the best way to program MIDI for Windows?

I am looking to write a small program that receives input from an external device and then sends MIDI signals to any MIDI compatible software. What is the best way, from the MIDI perspective, to go about this? Are there any specific libraries I should look into?
Thanks.
PortMidi! http://portmedia.sourceforge.net/
It's easy to use, examples for Windows are provided.
MIDI protocol is quite simple, most MIDI APIs offer manipulation with MIDI events and their parameters. What differs is the way how MIDI devices are enumerated and opened.
Correct answer depends on your requirements.
What input from what external device will you use? Will it be another MIDI device, mouse, keyboard or something that will allow input event parsing? Or will you need some low level hardware access? This one may influence programming language selection, if Java, C++ or something different and therefore the library choice.
What programming language do you prefer? C++, Java... If you plan to develop in Java, you can do that with API that JDK provides.
Should the program be multiplatform? If it should and you plan to develop in C++, you should use multiplatform MIDI library, e.g http://portmedia.sourceforge.net/ mentioned by darasan or https://github.com/jdkoftinoff/jdksmidi Otherwise you could just stick with native platform API (Windows API, ALSA, not sure about Mac stuff).
Do you plan to use some specific MIDI device? Maybe there is library that provides easy access to some device functions via MIDI, that you would have to handle by yourself (e.g. some predefined SysEx data)
With more question details more libraries (or less libraries) can be recommended.

Resources