Skip to content

Commit

Permalink
Merge pull request #10 from RobertBendun/feature/simple-api
Browse files Browse the repository at this point in the history
Simple Lua API example using shared memory
  • Loading branch information
RobertBendun authored May 31, 2024
2 parents 92b6099 + dec9098 commit fa102d8
Show file tree
Hide file tree
Showing 12 changed files with 786 additions and 197 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- New shared memory API
- Example of shared memory API usage in Lua using C library

### Changed

- Renamed "MIDI Sources" to "Blocks" to account for new forms of musical actions

## [0.1.2] - 2024-04-26

### Added
Expand Down
92 changes: 90 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ futures = "0.3.30"
rust-embed = "8.3.0"
mime_guess = "2.0.4"
tracing-appender = "0.2.3"
shared_memory = "0.12.4"

[dependencies.rusty_link]
git = "https://github.com/RobertBendun/rusty_link.git"
Expand Down
2 changes: 2 additions & 0 deletions examples/lua-shared-memory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
harmonia.so
clock
6 changes: 6 additions & 0 deletions examples/lua-shared-memory/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all: harmonia.so clock

harmonia.so: harmonia.c
gcc $< -shared -o $@ -fPIC -llua -lrtmidi -Wall -Wextra

LDLIBS=-lm -lrt
13 changes: 13 additions & 0 deletions examples/lua-shared-memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Example: simple Lua integration using unix sockets

This example creates a simple musical embeded language inside Lua, using coroutines.
It provides mechanism that is similar in UX to the very simplified SonicPI.

Tested only on Linux. Requires [RtMidi](https://github.com/thestk/rtmidi), [Lua 5.4](https://www.lua.org/) and C compiler.

## Usage

```console
$ make
$ lua5.4 demo.lua
```
46 changes: 46 additions & 0 deletions examples/lua-shared-memory/clock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <math.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>

static int fd;
char const* path = "/harmonia-block";

void clean(int)
{
shm_unlink(path);
exit(1);
}

int main()
{
signal(SIGINT, clean);

if ((fd = shm_open(path, O_RDWR|O_CREAT, 0600)) < 0) {
perror("shm_open");
return 1;
}

ftruncate(fd, sizeof(double));

double* p = mmap(NULL, sizeof(double), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

for (;;) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
*p = (double)ts.tv_sec + ts.tv_nsec/1000000000.0;

float f = 0.005;
nanosleep(&(struct timespec) {
.tv_sec = floor(f),
.tv_nsec = (f - floor(f)) * 1000000000,
}, NULL);
}
}
33 changes: 33 additions & 0 deletions examples/lua-shared-memory/demo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
harmonia = require "harmonia"

-- https://computermusicresource.com/midikeys.html
c2 = 48
e2 = 52
g2 = 55
c4 = 72
e4 = 76
g4 = 79

function play(note, duration)
coroutine.yield("play", note, duration)
end

function sleep(duration)
coroutine.yield("sleep", duration)
end

harmonia.bind_block("/harmonia-test", function ()
local prog = {c4, e4, g4}

for i = 1,8 do
for j = 1,i do
local time = 2 ^ (1 - i)
play(prog[i//#prog + 1], time)
sleep(time)
end
end

play(c2, 1)
play(e2, 1)
play(g2, 1)
end)
Loading

0 comments on commit fa102d8

Please sign in to comment.