From 23d7870102b2bad597ae7717821b8da805f0cddb Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Tue, 16 Feb 2021 20:52:39 +0000 Subject: [PATCH] Add context pointer to getsample callback --- src/apt.h | 7 ++++--- src/dsp.c | 16 ++++++++-------- src/main.c | 6 +++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/apt.h b/src/apt.h index 4a44037..b97d745 100644 --- a/src/apt.h +++ b/src/apt.h @@ -42,8 +42,9 @@ extern "C" { // Maximum height of an APT image in number of scanlines #define APT_MAX_HEIGHT 3000 -// apt_getpixelrow callback function to get audio samples -typedef int (*apt_getsample_t)(float *samples, int count); +// apt_getpixelrow callback function to get audio samples. +// context is the same as passed to apt_getpixelrow. +typedef int (*apt_getsample_t)(void *context, float *samples, int count); typedef struct { float *prow[APT_MAX_HEIGHT]; // Row buffers @@ -59,7 +60,7 @@ typedef struct { } apt_rgb_t; int APT_API apt_init(double sample_rate); -int APT_API apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsample_t getsample); +int APT_API apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsample_t getsample, void *context); void APT_API apt_histogramEqualise(float **prow, int nrow, int offset, int width); void APT_API apt_linearEnhance(float **prow, int nrow, int offset, int width); diff --git a/src/dsp.c b/src/dsp.c index 188f44c..e9becd7 100755 --- a/src/dsp.c +++ b/src/dsp.c @@ -131,7 +131,7 @@ static double pll(double I, double Q) { } // Convert samples into pixels -static int getamp(double *ampbuff, int count, apt_getsample_t getsample) { +static int getamp(double *ampbuff, int count, apt_getsample_t getsample, void *context) { static float inbuff[BLKIN]; static int idxin = 0; static int nin = 0; @@ -147,7 +147,7 @@ static int getamp(double *ampbuff, int count, apt_getsample_t getsample) { idxin = 0; // Read some samples - res = getsample(&(inbuff[nin]), BLKIN - nin); + res = getsample(context, &(inbuff[nin]), BLKIN - nin); nin += res; // Make sure there is enough samples to continue @@ -168,7 +168,7 @@ static int getamp(double *ampbuff, int count, apt_getsample_t getsample) { } // Sub-pixel offsetting + FIR compensation -int getpixelv(float *pvbuff, int count, apt_getsample_t getsample) { +int getpixelv(float *pvbuff, int count, apt_getsample_t getsample, void *context) { // Amplitude buffer static double ampbuff[BLKAMP]; static int nam = 0; @@ -187,7 +187,7 @@ int getpixelv(float *pvbuff, int count, apt_getsample_t getsample) { int res; memmove(ampbuff, &(ampbuff[idxam]), nam * sizeof(double)); idxam = 0; - res = getamp(&(ampbuff[nam]), BLKAMP - nam, getsample); + res = getamp(&(ampbuff[nam]), BLKAMP - nam, getsample, context); nam += res; if (nam < m) return n; @@ -207,7 +207,7 @@ int getpixelv(float *pvbuff, int count, apt_getsample_t getsample) { } // Get an entire row of pixels, aligned with sync markers -int apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsample_t getsample) { +int apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsample_t getsample, void *context) { static float pixels[PixelLine + SyncFilterLen]; static int npv; static int synced = 0; @@ -225,7 +225,7 @@ int apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsamp // Get the sync line if (npv < SyncFilterLen + 2) { - res = getpixelv(&(pixelv[npv]), SyncFilterLen + 2 - npv, getsample); + res = getpixelv(&(pixelv[npv]), SyncFilterLen + 2 - npv, getsample, context); npv += res; if (npv < SyncFilterLen + 2) return 0; @@ -256,7 +256,7 @@ int apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsamp static int lastmshift; if (npv < PixelLine + SyncFilterLen) { - res = getpixelv(&(pixelv[npv]), PixelLine + SyncFilterLen - npv, getsample); + res = getpixelv(&(pixelv[npv]), PixelLine + SyncFilterLen - npv, getsample, context); npv += res; if (npv < PixelLine + SyncFilterLen) return 0; @@ -294,7 +294,7 @@ int apt_getpixelrow(float *pixelv, int nrow, int *zenith, int reset, apt_getsamp // Get the rest of this row if (npv < PixelLine) { - res = getpixelv(&(pixelv[npv]), PixelLine - npv, getsample); + res = getpixelv(&(pixelv[npv]), PixelLine - npv, getsample, context); npv += res; if (npv < PixelLine) return 0; diff --git a/src/main.c b/src/main.c index aa6b424..46e5bdc 100644 --- a/src/main.c +++ b/src/main.c @@ -46,7 +46,7 @@ int channels = 1; // Function declarations static int initsnd(char *filename); -int getsample(float *sample, int nb); +int getsample(void *context, float *sample, int nb); static int processAudio(char *filename, options_t *opts); #ifdef _MSC_VER @@ -165,7 +165,7 @@ static int processAudio(char *filename, options_t *opts){ img.prow[img.nrow] = (float *) malloc(sizeof(float) * 2150); // Write into memory and break the loop when there are no more samples to read - if (apt_getpixelrow(img.prow[img.nrow], img.nrow, &img.zenith, (img.nrow == 0), getsample) == 0) + if (apt_getpixelrow(img.prow[img.nrow], img.nrow, &img.zenith, (img.nrow == 0), getsample, NULL) == 0) break; if(opts->realtime) pushRow(img.prow[img.nrow], IMG_WIDTH); @@ -299,7 +299,7 @@ static int initsnd(char *filename) { } // Read samples from the audio file -int getsample(float *sample, int nb) { +int getsample(void *context, float *sample, int nb) { if(channels == 1){ return (int)sf_read_float(audioFile, sample, nb); }else{