Skip to content

Commit 6ae44af

Browse files
committed
first version that prints implemented
1 parent 35b82e3 commit 6ae44af

File tree

5 files changed

+230
-26
lines changed

5 files changed

+230
-26
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vscode
2+
a.out.data
3+
a.out.js
4+
a.out.wasm

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ webasembly combilable version of the libassp
66

77
- make sure emsdk is activated: `source ../emsdk/emsdk_env.sh` (change path to ur install)
88

9-
- `emcc -O3 -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' \
9+
- compile `emcc -O3 -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' \
1010
-I assp \
1111
performAssp.c \
12-
assp/*.c``
12+
assp/*.c \
13+
--preload-file resources/msajc003.wav \
14+
--emrun`
1315

14-
- view `emrun --no_browser --port 8080 .`
16+
- view `emrun --no_browser --port 8080 test.html`

performAssp.c

+210-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include <filter.h>
1616
#include <ksv.h>
1717
#include <ctype.h> /* tolower() */
18-
18+
#include <string.h>
19+
#include <stdio.h>
1920

2021
/*
2122
* This list is used to map gender option values from R to the appropriate
@@ -487,6 +488,148 @@ void destroy_buffer(uint8_t* p) {
487488
free(p);
488489
}
489490

491+
// code taken from dobj2AsspDataObj(DOBJ * data)
492+
// & getDObjTrackData()
493+
int dobj2AsspDataObj(DOBJ * data){
494+
495+
DDESC *desc = NULL;
496+
TSSFF_Generic *genVar = NULL;
497+
int i,
498+
n;
499+
/*
500+
* count tracks
501+
*/
502+
for (n = 0, desc = &(data->ddl); desc != NULL; desc = desc->next) {
503+
n++;
504+
}
505+
506+
/*
507+
* create result, a list with a matrix for each track
508+
*/
509+
// PROTECT(ans = allocVector(VECSXP, n));
510+
/*
511+
* create list of tracks and formats
512+
*/
513+
// PROTECT(tracks = allocVector(STRSXP, n));
514+
// PROTECT(trackFormats = allocVector(STRSXP, n));
515+
for (i = 0, desc = &(data->ddl); desc != NULL; desc = desc->next, i++) {
516+
// SET_STRING_ELT(tracks, i, mkChar(desc->ident));
517+
// SET_STRING_ELT(trackFormats, i,
518+
// mkChar(asspDF2ssffString(desc->format)));
519+
/*
520+
* fill tracks with data
521+
*/
522+
printf ("Printing track %s.\n", desc->ident);
523+
524+
// from getDObjTrackData:
525+
void *tempBuffer,
526+
*bufPtr;
527+
int i,
528+
m,
529+
n;
530+
tempBuffer = malloc((size_t) data->recordSize);
531+
// SET_VECTOR_ELT(ans, i, getDObjTrackData(data, desc));
532+
533+
/*
534+
* various pointers for variuos data sizes
535+
*/
536+
uint8_t *u8Ptr;
537+
int8_t *i8Ptr;
538+
uint16_t *u16Ptr;
539+
int16_t *i16Ptr;
540+
uint32_t *u32Ptr;
541+
int32_t *i32Ptr;
542+
float *f32Ptr;
543+
double *f64Ptr;
544+
545+
double *Rans;
546+
int *Ians;
547+
uint8_t *bPtr;
548+
bPtr = (uint8_t *) tempBuffer;
549+
i = 0; /* initial index in buffer */
550+
551+
552+
for (m = 0; m < data->bufNumRecs; m++) {
553+
printf("\n");
554+
bufPtr = (void *)((char *)data->dataBuffer + m * data->recordSize);
555+
memcpy(tempBuffer, bufPtr, (size_t) data->recordSize);
556+
switch (desc->format) {
557+
case DF_UINT8:
558+
{
559+
u8Ptr = &bPtr[desc->offset];
560+
for (n = 0; n < desc->numFields; n++) {
561+
printf("%i\n", (unsigned int) u8Ptr[n]);
562+
}
563+
}
564+
break;
565+
case DF_INT8:
566+
{
567+
i8Ptr = (int8_t *) & bPtr[desc->offset];
568+
for (n = 0; n < desc->numFields; n++) {
569+
printf("%i\t", (int) u8Ptr[n]);
570+
}
571+
}
572+
break;
573+
case DF_UINT16:
574+
{
575+
u16Ptr = (uint16_t *) & bPtr[desc->offset];
576+
for (n = 0; n < desc->numFields; n++) {
577+
printf("%i\t", (unsigned int) u16Ptr[n]);
578+
}
579+
}
580+
break;
581+
case DF_INT16:
582+
{
583+
i16Ptr = (int16_t *) & bPtr[desc->offset];
584+
for (n = 0; n < desc->numFields; n++) {
585+
printf("%i\t", (int) i16Ptr[n]);
586+
}
587+
}
588+
break;
589+
case DF_UINT32:
590+
{
591+
u32Ptr = (uint32_t *) & bPtr[desc->offset];
592+
for (n = 0; n < desc->numFields; n++) {
593+
printf("%lu\t", (unsigned long) u32Ptr[n]);
594+
}
595+
}
596+
break;
597+
case DF_INT32:
598+
{
599+
i32Ptr = (int32_t *) & bPtr[desc->offset];
600+
for (n = 0; n < desc->numFields; n++) {
601+
printf("%li\t", (long) i32Ptr[n]);
602+
}
603+
}
604+
break;
605+
case DF_REAL32:
606+
{
607+
f32Ptr = (float *) &bPtr[desc->offset];
608+
for (n = 0; n < desc->numFields; n++) {
609+
printf("%f\t", (double) f32Ptr[n]);
610+
}
611+
}
612+
break;
613+
case DF_REAL64:
614+
{
615+
f64Ptr = (double *) &bPtr[desc->offset];
616+
for (n = 0; n < desc->numFields; n++) {
617+
printf("%f\t", (double) f64Ptr[n]);
618+
}
619+
}
620+
break;
621+
default:
622+
fprintf(stderr, "Hi, I just landed in the default of a switch in dataobj.c."
623+
"I am sorry, I should not be here and I don't know what to do.");
624+
break;
625+
}
626+
}
627+
free(tempBuffer);
628+
}
629+
630+
return 0;
631+
}
632+
490633

491634
/*
492635
* This function performs an ASSP analysis routine
@@ -495,11 +638,73 @@ void destroy_buffer(uint8_t* p) {
495638
* default options are available)
496639
*/
497640
EMSCRIPTEN_KEEPALIVE
498-
int performAssp(uint8_t* audio_in, int function_id) {
499-
// this is where the magic SHOULD happen :-)
500-
return 10;
501-
}
641+
int performAssp(const char* audio_path, const char* function_name) {
642+
643+
printf("%s\n", "### starting performAssp()");
644+
AOPTS OPTS;
645+
AOPTS *opt = &OPTS;
646+
A_F_LIST *anaFunc = funclist;
502647

648+
DOBJ *inPtr,
649+
*outPtr;
650+
651+
/*
652+
* Second parameter must be ASSP function name
653+
* check for validity and pick the right function descriptor
654+
*/
655+
while (anaFunc->funcNum != AF_NONE) {
656+
if (strcmp(function_name, anaFunc->fName) == 0){
657+
printf("Performing: %s\n", anaFunc->fName);
658+
break;
659+
}
660+
anaFunc++;
661+
}
662+
if (anaFunc->funcNum == AF_NONE){
663+
fprintf(stderr, "Invalid analysis function in performAssp.c");
664+
return 1;
665+
}
666+
667+
/*
668+
* generate the default settings for the analysis function
669+
*/
670+
if ((anaFunc->setFunc) (opt) == -1){
671+
fprintf(stderr, "%d\t$%s\n", asspMsgNum, getAsspMsg(asspMsgNum));
672+
return 1;
673+
}
674+
675+
/*
676+
* open
677+
*/
678+
inPtr = asspFOpen(strdup(audio_path), AFO_READ, (DOBJ *) NULL);
679+
if (inPtr == NULL){
680+
fprintf(stderr, "%s (%s)", getAsspMsg(asspMsgNum), strdup(audio_path));
681+
}
682+
683+
/*
684+
* run the function (as pointed to in the descriptor) to generate
685+
* the output object
686+
*/
687+
outPtr = (anaFunc->compProc) (inPtr, opt, (DOBJ *) NULL);
688+
if (outPtr == NULL) {
689+
asspFClose(inPtr, AFC_FREE);
690+
fprintf(stderr, "%s (%s)", getAsspMsg(asspMsgNum), strdup(audio_path));
691+
}
692+
693+
/*
694+
* input data object no longer needed
695+
*/
696+
asspFClose(inPtr, AFC_FREE);
697+
698+
/*
699+
* TODO write to file
700+
*/
701+
int tmp = dobj2AsspDataObj(outPtr);
702+
703+
asspFClose(outPtr, AFC_FREE);
704+
705+
printf("%s\n", "done!");
706+
return 0;
707+
}
503708

504709

505710
/*

resources/msajc003.wav

113 KB
Binary file not shown.

test.html

+11-18
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,24 @@
1212
<script>
1313

1414
async function loadAudio(src) {
15-
// Load image
16-
const imgBlob = await fetch(src).then(resp => resp.blob());
17-
// const img = await createImageBitmap(imgBlob);
18-
// // Make canvas same size as image
19-
// const canvas = document.createElement('canvas');
20-
// canvas.width = img.width;
21-
// canvas.height = img.height;
22-
// // Draw image onto canvas
23-
// const ctx = canvas.getContext('2d');
24-
// ctx.drawImage(img, 0, 0);
25-
return ctx.getImageData(0, 0, img.width, img.height);
15+
// Load audio
16+
const audio_ab = await fetch(src).then(resp => resp.arrayBuffer());
17+
return audio_ab;
2618
}
2719

2820
Module.onRuntimeInitialized = async _ => {
2921
const api = {
30-
performAssp: Module.cwrap('performAssp', 'number', []),
22+
performAssp: Module.cwrap('performAssp', 'number', ['string', 'string']),
3123
create_buffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
3224
destroy_buffer: Module.cwrap('destroy_buffer', '', ['number'])
3325
};
34-
const image = await loadAudio('/image.jpg');
35-
const p = api.create_buffer(image.width, image.height);
36-
Module.HEAP8.set(image.data, p);
37-
// ... performAssp
38-
console.log(api.performAssp(2));
39-
api.destroy_buffer(p);
26+
// const audioFileBuffer = await loadAudio('/resources/msajc003.wav');
27+
// const p = api.create_buffer(image.width, image.height);
28+
// Module.HEAP8.set(image.data, p);
29+
30+
// save arraybufferview to FS -> so fopen() in wasmassp works
31+
// FS.writeFile('audiofile.wav', new DataView(audioFileBuffer));
32+
console.log(api.performAssp("resources/msajc003.wav", "forest"));
4033

4134
};
4235
</script>

0 commit comments

Comments
 (0)