~hecanjog/pippi

076dfbce6e8c2cbcece2891034816643fd8446b0 — Erik Schoster 13 days ago 4e71407
Add astrid ipc helper and update program names
M astrid/Makefile => astrid/Makefile +2 -2
@@ 81,7 81,7 @@ else
	journalctl -xf --output=json | python -u python/colorize_logs.py
endif

build: astrid-dac astrid-adc astrid-renderer astrid-q astrid-seriallistener
build: astrid-dac astrid-adc astrid-renderer astrid-q astrid-seriallistener astrid-counter

macos: astrid-dac astrid-adc astrid-renderer-macos astrid-q astrid-seriallistener
macos: astrid-dac astrid-adc astrid-renderer-macos astrid-q astrid-seriallistener astrid-counter


M astrid/console.py => astrid/console.py +8 -8
@@ 63,7 63,7 @@ def midi_relay(device_name, stop_event):
                        params = 'note=%s velocity=%s' % (msg.note, msg.velocity)

                        try:
                            subprocess.run(['./build/qmessage', instrument_name, params])
                            subprocess.run(['./build/astrid-qmessage', instrument_name, params])
                        except Exception as e:
                            logger.exception('Could not invoke qmessage: %s' % e)



@@ 110,12 110,12 @@ class AstridConsole(cmd.Cmd):
        if cmd == 'on' and self.dac is None:
            print('Starting dac & adc...')
            if self.dac is None:
                self.dac = subprocess.Popen('./build/dac')
                self.dac = subprocess.Popen('./build/astrid-dac')
            else:
                print('dac is already running')

            if self.adc is None:
                self.adc = subprocess.Popen('./build/adc')
                self.adc = subprocess.Popen('./build/astrid-adc')
            else:
                print('adc is already running')



@@ 140,7 140,7 @@ class AstridConsole(cmd.Cmd):
        if cmd == 'on' and self.dac is None:
            print('Starting dac...')
            if self.dac is None:
                self.dac = subprocess.Popen('./build/dac')
                self.dac = subprocess.Popen('./build/astrid-dac')
            else:
                print('dac is already running')



@@ 157,7 157,7 @@ class AstridConsole(cmd.Cmd):
        if cmd == 'on' and self.adc is None:
            print('Starting adc...')
            if self.adc is None:
                self.adc = subprocess.Popen('./build/adc')
                self.adc = subprocess.Popen('./build/astrid-adc')
            else:
                print('adc is already running')



@@ 173,7 173,7 @@ class AstridConsole(cmd.Cmd):
    def do_l(self, instrument):
        if instrument not in self.instruments:
            try:
                rcmd = './build/renderer "orc/%s.py" "%s"' % (instrument, instrument)
                rcmd = './build/astrid-renderer "orc/%s.py" "%s"' % (instrument, instrument)
                print(rcmd)
                self.instruments[instrument] = subprocess.Popen(rcmd, shell=True)
            except Exception as e:


@@ 191,7 191,7 @@ class AstridConsole(cmd.Cmd):

        if instrument not in self.instruments:
            try:
                rcmd = './build/renderer "orc/%s.py" "%s"' % (instrument, instrument)
                rcmd = './build/astrid-renderer "orc/%s.py" "%s"' % (instrument, instrument)
                print(rcmd)
                self.instruments[instrument] = subprocess.Popen(rcmd, shell=True)
            except Exception as e:


@@ 201,7 201,7 @@ class AstridConsole(cmd.Cmd):

        try:
            logger.info('Sending play msg to %s renderer w/params:\n  %s' % (instrument, params))
            subprocess.run(['./build/qmessage', instrument, params])
            subprocess.run(['./build/astrid-qmessage', instrument, params])
        except Exception as e:
            print('Could not invoke qmessage: %s' % e)
            print(traceback.format_exc())

M astrid/src/astrid.c => astrid/src/astrid.c +38 -15
@@ 156,41 156,64 @@ int lpadc_create() {
    return 0;
}

int lpadc_getid() {
    int handle, shmid = 0;
    char * shmidp;
int lpipc_setid(char * path, int id) {
    FILE * handle;
    int count;

    handle = fopen(path, "w");
    if(handle == NULL) {
        perror("fopen");
        return -1;
    }

    count = fprintf(handle, "%d", id);
    if(count < 0) {
        perror("fprintf");
        fclose(handle);
        return -1;
    }

    return 0;
}

int lpipc_getid(char * path) {
    int handle, id = 0;
    char * idp;
    struct stat st;

    /* Read the identifier for the adcbuf shared memory 
     * from the well known file. Reading with mmap to speed 
     * up access as this might be called in a tight render loop */
    handle = open(LPADC_HANDLE, O_RDONLY);
    /* Read the identifier from the well known file. mmap speeds 
     * up access when ids need to be fetched when timing matters */
    handle = open(path, O_RDONLY);
    if(handle < 0) {
        perror("Could not open tmpfile to read adcbuf shmid");
        perror("lpipc_getid open");
        return -1;
    }

    /* Get the file size */
    if(fstat(handle, &st) == -1) {
        perror("Could not stat tmpfile");
        perror("lpipc_getid fstat");
        close(handle);
        return -1;
    }

    /* Map the file into memory */
    shmidp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, handle, 0);
    if(shmidp == MAP_FAILED) {
        perror("Could not mmap tmpfile");
    idp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, handle, 0);
    if(idp == MAP_FAILED) {
        perror("lpipc_getid mmap");
        close(handle);
        return -1;
    }

    /* Copy the identifier from the mmaped file & cleanup */
    sscanf(shmidp, "%d", &shmid);
    munmap(shmidp, st.st_size);
    sscanf(idp, "%d", &id);
    munmap(idp, st.st_size);
    close(handle);

    return shmid;
    return id;
}

int lpadc_getid() {
    return lpipc_getid(LPADC_HANDLE);
}

lpadcbuf_t * lpadc_open() {

M astrid/src/astrid.h => astrid/src/astrid.h +6 -0
@@ 39,6 39,9 @@
#define LPPLAYQ "/tmp/astridq"
#define LPMAXQNAME (12 + 1 + LPMAXNAME)

#define LPVOICE_ID_SHMID "/tmp/astrid_voice_id_shmid"
#define LPVOICE_ID_SEMID "/tmp/astrid_voice_id_semid"

/* This struct is required for historical reasons by POSIX to be defined 
 * for system V semaphores. Astrid uses them for voice ID assignment. */
union semun {


@@ 140,6 143,9 @@ lpadcbuf_t * lpadc_open();
void lpadc_write_block(lpadcbuf_t * adcbuf, float * block, size_t blocksize);
lpfloat_t lpadc_read_sample(lpadcbuf_t * adcbuf, size_t offset);

int lpipc_setid(char * path, int id); 
int lpipc_getid(char * path); 

void lptimeit_since(struct timespec * start);

#endif

M astrid/src/getvoiceid.c => astrid/src/getvoiceid.c +2 -2
@@ 4,8 4,8 @@ int main(int argc, char * argv[]) {
    lpcounter_t c;
    int voice_id;

    c.semid = atoi(argv[1]);
    c.shmid = atoi(argv[2]);
    c.semid = lpipc_getid(LPVOICE_ID_SEMID);
    c.shmid = lpipc_getid(LPVOICE_ID_SHMID);

    if((voice_id = lpcounter_read_and_increment(&c)) < 0) {
        perror("lpcounter_create");

M astrid/src/makecounter.c => astrid/src/makecounter.c +3 -0
@@ 12,5 12,8 @@ int main() {
    printf("shmid = %d\n", c.shmid);
    printf("semid = %d\n", c.semid);

    lpipc_setid(LPVOICE_ID_SHMID, c.shmid);
    lpipc_setid(LPVOICE_ID_SEMID, c.semid);

    return 0;
}