aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Knight <q@1bpm.net>2022-12-30 20:43:17 +0000
committerRichard Knight <q@1bpm.net>2022-12-30 20:43:17 +0000
commit510d32100f523a901d960013c50d481bf63d21df (patch)
treec6a5a782b8b2c320e487776a02a7e70b221ad914
parent4f73db87c653ccffcb3cddddaf003b9d09546122 (diff)
downloadcsound-datacore-510d32100f523a901d960013c50d481bf63d21df.tar.gz
csound-datacore-510d32100f523a901d960013c50d481bf63d21df.tar.bz2
csound-datacore-510d32100f523a901d960013c50d481bf63d21df.zip
fix permissions on reading and mempsname
-rw-r--r--Plugin.cmake10
-rw-r--r--README.md4
-rw-r--r--examples/memson-fltk.csd2
-rw-r--r--examples/memson-fltk2.csd1
-rw-r--r--examples/memson-t2.csd4
-rw-r--r--examples/rawread.csd35
-rw-r--r--src/opcodes.cpp327
7 files changed, 310 insertions, 73 deletions
diff --git a/Plugin.cmake b/Plugin.cmake
index d314502..577ea55 100644
--- a/Plugin.cmake
+++ b/Plugin.cmake
@@ -1,14 +1,16 @@
-if(NOT LINUX)
- message(FATAL_ERROR "Only Linux is supported")
+set(PLUGIN_NAME datacore)
+set(CPPFILES src/opcodes.cpp src/maketable.cpp)
+
+if(LINUX)
+ set(CPPFILES ${CPPFILES} src/pmparser.cpp)
+ add_definitions("-DUSE_PROCMAPS")
endif()
set(INCLUDES ${CSOUND_INCLUDE_DIRS} "include")
-set(PLUGIN_NAME memread)
# Dependencies
# None
# Source files
-set(CPPFILES src/opcodes.cpp src/pmparser.cpp src/maketable.cpp)
make_plugin(${PLUGIN_NAME} "${CPPFILES}")
target_include_directories(${PLUGIN_NAME} PRIVATE ${INCLUDES})
diff --git a/README.md b/README.md
index d765943..97fe2a9 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
-# csound-memread : Memory audification/sonification for Csound on Linux
+# csound-datacore : Data audification/sonification for Csound on Linux
## Overview
-csound-memread provides a way to read data associated with a process ID as audio.
+csound-datacore provides a way to read data associated with a process ID as audio.
Only Linux is supported or an OS that supports userspace reading of /proc/.../mem
## Requirements
diff --git a/examples/memson-fltk.csd b/examples/memson-fltk.csd
index 44e80a8..301bdc8 100644
--- a/examples/memson-fltk.csd
+++ b/examples/memson-fltk.csd
@@ -18,7 +18,7 @@ FLrun
instr 1
idata[] memps
- ipid = idata[int(random(0, lenarray(idata)-1))]
+ ipid = 5945
Sname = sprintf("%s (%d)", mempsname(ipid), ipid)
FLsetText Sname, gilabel
a1 memson ipid, gkx, gky, 44100, 1
diff --git a/examples/memson-fltk2.csd b/examples/memson-fltk2.csd
index a4beb8a..4dce09b 100644
--- a/examples/memson-fltk2.csd
+++ b/examples/memson-fltk2.csd
@@ -20,6 +20,7 @@ instr 1
idata[] memps
ipid = idata[int(random(0, lenarray(idata)-1))]
Sname = sprintf("%s (%d)", mempsname(ipid), ipid)
+prints Sname
FLsetText Sname, gilabel
ifn mem2tab ipid, 1
aindex lphasor 1
diff --git a/examples/memson-t2.csd b/examples/memson-t2.csd
index c732d1e..d62c37e 100644
--- a/examples/memson-t2.csd
+++ b/examples/memson-t2.csd
@@ -16,8 +16,8 @@ instr 1
print ipid
kr1 line 0, p3, 1
kr2 line 0, p3, 1
- ;a1 memson2 ipid, 0.4, 0.5; kr2
- a1 memson ipid, 0;kr1
+ a1 memson2 ipid, 0.4, 0.5; kr2
+ ;a1 memson ipid, 0;kr1
a1 dcblock a1*0.1
outs a1, a1
endin
diff --git a/examples/rawread.csd b/examples/rawread.csd
new file mode 100644
index 0000000..005a558
--- /dev/null
+++ b/examples/rawread.csd
@@ -0,0 +1,35 @@
+<CsoundSynthesizer>
+<CsOptions>
+-odac
+</CsOptions>
+<CsInstruments>
+sr = 44100
+kr = 4410
+nchnls = 2
+0dbfs = 1
+seed 0
+
+
+instr 1
+ a1 rawread "/usr/bin/zip"
+ a1 *= 0.1
+ outs a1, a1
+endin
+
+instr 2
+ aL, aR rawread "/usr/bin/zip", 1
+ outs aL*0.1, aR*0.1
+endin
+
+instr 3
+ ifn rawreadtable "/usr/bin/zip"
+ a1 loscil 0.1, 1, ifn, 1
+
+ outs a1, a1
+endin
+
+</CsInstruments>
+<CsScore>
+i3 0 60
+</CsScore>
+</CsoundSynthesizer>
diff --git a/src/opcodes.cpp b/src/opcodes.cpp
index fa252e2..460aee0 100644
--- a/src/opcodes.cpp
+++ b/src/opcodes.cpp
@@ -1,4 +1,9 @@
+#ifdef USE_PROCMAPS
#include "pmparser.h"
+#include <sys/mman.h>
+#define PROC_DIR "/proc/"
+#endif
+
#include "maketable.hpp"
#include <stdio.h>
#include <stdlib.h>
@@ -10,7 +15,6 @@
#include <plugin.h>
#include <functional>
#include <vector>
-#include <sys/mman.h>
#ifdef USE_X11
#include <X11/Xlib.h>
@@ -22,23 +26,29 @@
#include <sys/stat.h>
#include <dirent.h>
-#define PROC_DIR "/proc/"
+
+#ifdef USE_PROCMAPS
+
struct mempsname : csnd::Plugin<1, 1> {
static constexpr char const *otypes = "S";
static constexpr char const *itypes = "i";
int init() {
int pid = inargs[0];
char* path = (char*) csound->malloc(sizeof(char) * PATH_MAX);
- sprintf(path, "%s%d/comm", PROC_DIR, pid);
+ sprintf(path, "%s%d/cmdline", PROC_DIR, pid);
FILE* f = fopen(path, "r");
char* buffer = (char*) csound->malloc(sizeof(char) * 1024);
size_t size = fread(buffer, sizeof(char), 1024, f);
+ if (size > 0) {
+ if ('\n' == buffer[size - 1]) {
+ buffer[size - 1] = '\0';
+ }
+ }
-
fclose(f);
STRINGDAT &opath = outargs.str_data(0);
opath.size = strlen(buffer);
@@ -94,7 +104,7 @@ struct memps : csnd::Plugin<1, 0> {
struct stat fileInfo;
strcpy(path, PROC_DIR);
strcat(path, de->d_name);
- if (stat(path, &fileInfo) == 0) {
+ if (stat(strcat(path, "/maps"), &fileInfo) == 0) {
if (fileInfo.st_uid == thisUser) {
(*processnum)++;
*processes = (int)atoi(de->d_name);
@@ -111,64 +121,6 @@ struct memps : csnd::Plugin<1, 0> {
};
-#ifdef USE_X11
-struct winson : csnd::Plugin<1, 1> {
- static constexpr char const *otypes = "a";
- static constexpr char const *itypes = "i";
- Display* display;
- Window root;
- int width;
- int height;
- MYFLT* buffer;
- int buffer_size;
- int buffer_read_position;
- int buffer_write_position;
-
- int init() {
- display = XOpenDisplay(NULL);
- //root = DefaultRootWindow(display);
- root = 0x2200004;
- XWindowAttributes gwa;
- XGetWindowAttributes(display, root, &gwa);
-
- width = gwa.width;
- height = gwa.height;
- buffer_size = width * height;
- buffer = (MYFLT*) csound->malloc(sizeof(MYFLT) * buffer_size);
- buffer_read_position = 0;
- buffer_write_position = 0;
- refill_buffer();
- return OK;
- }
-
- int refill_buffer() {
- buffer_write_position = 0;
- XImage* image = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap);
- for (int x = 0; x < width; x++) {
- for (int y=0; y < height; y++) {
- buffer[buffer_write_position] = ((MYFLT) XGetPixel(image,x,y)) / 16777215;
- buffer_write_position++;
- }
- }
- return OK;
- }
-
- int aperf() {
- for (int i = 0; i < nsmps; i++) {
- outargs(0)[i] = buffer[buffer_read_position];
- if (LIKELY(buffer_read_position + 1 < buffer_size)) {
- buffer_read_position++;
- } else {
- refill_buffer();
- buffer_read_position = 0;
- }
- }
-
- return OK;
-
- }
-};
-#endif
@@ -370,18 +322,265 @@ struct memson : csnd::Plugin<1, 5> {
}
+};
+
+#endif
+// end with procmaps
+
+
+// ifn rawreadtable Sfile, ichannels, istart, iskip
+struct rawreadtable : csnd::Plugin<1, 4> {
+ static constexpr char const *otypes = "i";
+ static constexpr char const *itypes = "Sjjj";
+
+ int init() {
+ FILE* infile;
+ STRINGDAT &path = inargs.str_data(0);
+ FUNC *table;
+ int channels = (inargs[1] == 2) ? 2 : 1;
+ int start = (inargs[2] < 0) ? 0 : (int) inargs[2];
+ int skip = (inargs[3] < 1) ? 1 : (int) inargs[3];
+
+ int buffsize = 16384;
+
+ char* buffer = (char*) csound->malloc(sizeof(char) * buffsize);
+ size_t buffused;
+ int readpos = 0;
+ long int writepos = 0;
+
+ infile = fopen(path.data, "rb");
+ fseek(infile, 0L, SEEK_END);
+ int length = ftell(infile) / skip;
+ fseek(infile, start, SEEK_SET);
+
+ if ((maketable(csound, (int)length, &table, channels)) != OK) {
+ return csound->init_error("Cannot create ftable");
+
+ }
+ buffused = fread(buffer, sizeof(char), buffsize, infile);
+
+ while (buffused != 0) {
+ while (readpos < buffused) {
+ table->ftable[writepos] = ((MYFLT) buffer[readpos] / CHAR_MAX);
+ writepos ++;
+ readpos += skip;
+ }
+ readpos = 0;
+ buffused = fread(buffer, sizeof(char), buffsize, infile);
+
+ }
+ csound->free(buffer);
+ outargs[0] = table->fno;
+ return OK;
+ }
+};
+
+// aout rawread Sfile, [iloop = 0]
+struct rawread : csnd::Plugin<1, 2> {
+ static constexpr char const *otypes = "a";
+ static constexpr char const *itypes = "So";
+
+ FILE* infile;
+ char* buffer;
+ int buffsize;
+ size_t buffused;
+ int readpos;
+ bool doloop;
+ bool dooutput;
+
+ int init() {
+ buffsize = 16384;
+ readpos = 0;
+ doloop = (inargs[1] >= 1) ? true : false;
+ buffer = (char*) csound->malloc(sizeof(char) * buffsize);
+ STRINGDAT &path = inargs.str_data(0);
+ infile = fopen(path.data, "rb");
+ if (infile == NULL) {
+ return csound->init_error("Cannot open file");
+ }
+ dooutput = true;
+ fillbuffer();
+ csound->plugin_deinit(this);
+ return OK;
+ }
+
+ int deinit() {
+ if (infile != NULL) {
+ fclose(infile);
+ }
+ return OK;
+ }
+
+ void fillbuffer() {
+ buffused = fread(buffer, sizeof(char), buffsize, infile);
+ }
+
+ int aperf() {
+ if (!dooutput) {
+ return OK;
+ }
+ for (int i = 0; i < nsmps; i++) {
+ outargs(0)[i] = ((MYFLT) buffer[readpos] / CHAR_MAX);
+ if (readpos + 1 < buffused) {
+ readpos ++;
+ } else {
+ readpos = 0;
+ fillbuffer();
+ if (buffused == 0) {
+ if (doloop) {
+ fseek(infile, 0, SEEK_SET);
+ fillbuffer();
+ } else {
+ dooutput = false;
+ }
+ }
+ }
+ }
+ return OK;
+ }
+};
+
+// aL, aR rawread Sfile, [iloop = 0]
+struct rawreadstereo : csnd::Plugin<2, 2> {
+ static constexpr char const *otypes = "aa";
+ static constexpr char const *itypes = "So";
+
+ FILE* infile;
+ char* buffer;
+ int buffsize;
+ size_t buffused;
+ int readpos;
+ bool doloop;
+ bool dooutput;
+
+ int init() {
+ buffsize = 16384;
+ readpos = 0;
+ doloop = (inargs[1] >= 1) ? true : false;
+ buffer = (char*) csound->malloc(sizeof(char) * buffsize);
+ STRINGDAT &path = inargs.str_data(0);
+ infile = fopen(path.data, "rb");
+ if (infile == NULL) {
+ return csound->init_error("Cannot open file");
+ }
+ dooutput = true;
+ fillbuffer();
+ csound->plugin_deinit(this);
+ return OK;
+ }
+
+ int deinit() {
+ if (infile != NULL) {
+ fclose(infile);
+ }
+ return OK;
+ }
+
+ void fillbuffer() {
+ buffused = fread(buffer, sizeof(char), buffsize, infile);
+ }
+
+ int aperf() {
+ if (!dooutput) {
+ return OK;
+ }
+ for (int i = 0; i < nsmps; i++) {
+ outargs(0)[i] = ((MYFLT) buffer[readpos] / CHAR_MAX); // - 0.5 ;
+ outargs(1)[i] = ((MYFLT) buffer[readpos+1] / CHAR_MAX);
+ if (readpos + 2 < buffused) {
+ readpos = readpos + 2;
+ } else {
+ readpos = 0;
+ fillbuffer();
+ if (buffused == 0) {
+ if (doloop) {
+ fseek(infile, 0, SEEK_SET);
+ fillbuffer();
+ } else {
+ dooutput = false;
+ }
+ }
+ }
+ }
+ return OK;
+ }
+};
+
+
+
+#ifdef USE_X11
+struct winson : csnd::Plugin<1, 1> {
+ static constexpr char const *otypes = "a";
+ static constexpr char const *itypes = "i";
+ Display* display;
+ Window root;
+ int width;
+ int height;
+ MYFLT* buffer;
+ int buffer_size;
+ int buffer_read_position;
+ int buffer_write_position;
+
+ int init() {
+ display = XOpenDisplay(NULL);
+ //root = DefaultRootWindow(display);
+ root = 0x2200004;
+ XWindowAttributes gwa;
+ XGetWindowAttributes(display, root, &gwa);
+
+ width = gwa.width;
+ height = gwa.height;
+ buffer_size = width * height;
+ buffer = (MYFLT*) csound->malloc(sizeof(MYFLT) * buffer_size);
+ buffer_read_position = 0;
+ buffer_write_position = 0;
+ refill_buffer();
+ return OK;
+ }
+
+ int refill_buffer() {
+ buffer_write_position = 0;
+ XImage* image = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap);
+ for (int x = 0; x < width; x++) {
+ for (int y=0; y < height; y++) {
+ buffer[buffer_write_position] = ((MYFLT) XGetPixel(image,x,y)) / 16777215;
+ buffer_write_position++;
+ }
+ }
+ return OK;
+ }
+ int aperf() {
+ for (int i = 0; i < nsmps; i++) {
+ outargs(0)[i] = buffer[buffer_read_position];
+ if (LIKELY(buffer_read_position + 1 < buffer_size)) {
+ buffer_read_position++;
+ } else {
+ refill_buffer();
+ buffer_read_position = 0;
+ }
+ }
+
+ return OK;
+
+ }
};
+#endif
#include <modload.h>
void csnd::on_load(csnd::Csound *csound) {
+ csnd::plugin<rawread>(csound, "rawread", csnd::thread::ia);
+ csnd::plugin<rawreadstereo>(csound, "rawread.s", csnd::thread::ia);
+ csnd::plugin<rawreadtable>(csound, "rawreadtable", csnd::thread::i);
+
+#ifdef USE_PROCMAPS
csnd::plugin<memson>(csound, "memson", csnd::thread::ia);
csnd::plugin<memps>(csound, "memps", csnd::thread::i);
csnd::plugin<mempsname>(csound, "mempsname", csnd::thread::i);
csnd::plugin<mem2tab>(csound, "mem2tab", csnd::thread::i);
-
+#endif
#ifdef USE_X11
csnd::plugin<winson>(csound, "winson", csnd::thread::ia);