-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathjfk_reader.c
More file actions
111 lines (97 loc) · 2.55 KB
/
jfk_reader.c
File metadata and controls
111 lines (97 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <ruby.h>
#include <ruby/memory_view.h>
#include <ruby/encoding.h>
typedef struct {
VALUE audio_path;
int n_samples;
const char *audio_path_str;
float *data;
short *samples;
} jfk_alloc_args;
static VALUE
jfk_reader_alloc_resources(VALUE arg)
{
jfk_alloc_args *a = (jfk_alloc_args *)arg;
a->audio_path_str = StringValueCStr(a->audio_path);
a->data = ALLOC_N(float, a->n_samples);
a->samples = ALLOC_N(short, a->n_samples);
return Qnil;
}
static VALUE
jfk_reader_initialize(VALUE self, VALUE audio_path)
{
rb_iv_set(self, "audio_path", audio_path);
return Qnil;
}
static bool
jfk_reader_get_memory_view(const VALUE obj, rb_memory_view_t *view, int flags)
{
VALUE audio_path = rb_iv_get(obj, "audio_path");
// n_samples is a fixed constant (not derived from user input).
const int n_samples = 176000;
jfk_alloc_args args = {
.audio_path = audio_path,
.n_samples = n_samples,
.audio_path_str = NULL,
.data = NULL,
.samples = NULL,
};
int state;
rb_protect(jfk_reader_alloc_resources, (VALUE)&args, &state);
if (state) {
if (args.samples) xfree(args.samples);
if (args.data) xfree(args.data);
return false;
}
FILE *file = fopen(args.audio_path_str, "rb");
if (file == NULL) {
xfree(args.samples);
xfree(args.data);
return false;
}
fseek(file, 78, SEEK_SET);
fread(args.samples, sizeof(short), n_samples, file);
fclose(file);
for (int i = 0; i < n_samples; i++) {
args.data[i] = args.samples[i] / 32768.0;
}
xfree(args.samples);
view->obj = obj;
view->data = (void *)args.data;
view->byte_size = sizeof(float) * n_samples;
view->readonly = true;
view->format = "f";
view->item_size = sizeof(float);
view->item_desc.components = NULL;
view->item_desc.length = 0;
view->ndim = 1;
view->shape = NULL;
view->sub_offsets = NULL;
view->private_data = NULL;
return true;
}
static bool
jfk_reader_release_memory_view(const VALUE obj, rb_memory_view_t *view)
{
if (view->data) {
xfree(view->data);
view->data = NULL;
}
return true;
}
static bool
jfk_reader_memory_view_available_p(const VALUE obj)
{
return true;
}
static const rb_memory_view_entry_t jfk_reader_view_entry = {
jfk_reader_get_memory_view,
jfk_reader_release_memory_view,
jfk_reader_memory_view_available_p
};
void Init_jfk_reader(void)
{
VALUE cJFKReader = rb_define_class("JFKReader", rb_cObject);
rb_memory_view_register(cJFKReader, &jfk_reader_view_entry);
rb_define_method(cJFKReader, "initialize", jfk_reader_initialize, 1);
}