Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Lua API example using shared memory #10

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Integration testing
on:
workflow_dispatch:
pull_request:
branches: [ main ]
jobs:
test:
strategy:
Expand Down
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