-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from RobertBendun/feature/simple-api
Simple Lua API example using shared memory
- Loading branch information
Showing
12 changed files
with
786 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
harmonia.so | ||
clock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.