Skip to content

Commit

Permalink
Add info outlet to sfload
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Jan 29, 2025
1 parent 84154a3 commit 4034dd0
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions Source/Control/sfload.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

static t_class *sfload_class;

typedef struct _sfload{
typedef struct _sfload {
t_object x_obj;
t_outlet *x_info_outlet;
AVCodecContext *x_stream_ctx;
int x_stream_idx;
AVPacket *x_pkt;
Expand All @@ -27,6 +28,7 @@ typedef struct _sfload{
_Atomic int x_result_ready;
t_clock *x_result_clock;
char x_path[MAXPDSTRING];
t_atom x_sfinfo[6];
}t_sfload;

void* sfload_read_audio(void *arg){ // read audio into array
Expand Down Expand Up @@ -85,6 +87,10 @@ void* sfload_read_audio(void *arg){ // read audio into array
pd_error(x, "[sfload]: Could not initialize the resampling context\n");
return (NULL);
}

AVDictionaryEntry *loop_start_entry = av_dict_get(x->x_ic->metadata, "loop_start", NULL, 0);
AVDictionaryEntry *loop_end_entry = av_dict_get(x->x_ic->metadata, "loop_end", NULL, 0);

t_sample* x_out = (t_sample*)av_mallocz(nch * FRAMES * sizeof(t_sample));
int output_index = 0;
while(av_read_frame(x->x_ic, x->x_pkt) >= 0) {
Expand All @@ -111,6 +117,14 @@ void* sfload_read_audio(void *arg){ // read audio into array
}
av_packet_unref(x->x_pkt);
}

SETFLOAT(x->x_sfinfo, x->x_stream_ctx->sample_rate);
SETFLOAT(x->x_sfinfo + 1, nch);
SETFLOAT(x->x_sfinfo + 2, av_get_bytes_per_sample(x->x_stream_ctx->sample_fmt) * 8);
SETFLOAT(x->x_sfinfo + 3, output_index);
SETFLOAT(x->x_sfinfo + 4, loop_start_entry ? atoi(loop_start_entry->value) : 0);
SETFLOAT(x->x_sfinfo + 5, loop_end_entry ? atoi(loop_end_entry->value) : 0);

x->x_result_ready = output_index;
av_free(x_out);
return (NULL);
Expand All @@ -127,18 +141,24 @@ void sfload_check_done(t_sfload* x){ // result clock
x->x_result_ready = 0;
free(x->x_all_out);
x->x_all_out = NULL;
outlet_list(x->x_info_outlet, &s_, 6, x->x_sfinfo);
}
else
clock_delay(x->x_result_clock, 20);
}

static void sfload_find_file(t_sfload *x, t_symbol* file, char* dir_out, char* filename_out){
const char *filename = file->s_name;
const char *dirname = canvas_getdir(x->x_canvas)->s_name;
char* fileout;
open_via_path(dirname, filename, "", dir_out, &fileout, MAXPDSTRING-1, 0);
memcpy(filename_out, fileout, strlen(fileout) + 1);
strcat(dir_out, "/");
static void sfload_find_file(t_sfload *x, t_symbol* file, char* dir_out){
static char fname[MAXPDSTRING];
char *bufptr;
int fd = canvas_open(x->x_canvas, file->s_name, "", fname, &bufptr, MAXPDSTRING, 1);
if(fd < 0){
post("[sfload] file '%s' not found", file->s_name);
return;
}
else {
if(bufptr > fname) bufptr[-1] = '/';
strcpy(dir_out, fname);
}
}

void sfload_load(t_sfload* x, t_symbol* s, int ac, t_atom* av){
Expand Down Expand Up @@ -202,6 +222,7 @@ static void *sfload_new(t_symbol *s, int ac, t_atom *av){
x->x_canvas = canvas_getcurrent();
x->x_result_ready = 0;
x->x_result_clock = clock_new(x, (t_method)sfload_check_done);
x->x_info_outlet = outlet_new(x, &s_list);
return(x);
}

Expand Down

0 comments on commit 4034dd0

Please sign in to comment.