summaryrefslogtreecommitdiff
path: root/library.c
blob: cf947cd730c808b4aa4e288960376e31fda752d5 (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "config.h"
#include "library.h"
#include "plat.h"
#include <string.h>
#include <stdio.h>

#include "dr_flac.h"

static unsigned hash(const char* s) {
	const unsigned char* p = (const unsigned char*)s;
	unsigned h = 2166136261;
	while (*p)
		h = (h ^ *p++) * 16777619;
	return h;
}

Song* find_song(Library* l, const char* path) {
	int i = hash(path) % l->cap;
	int c;
	Song* s;
	for (c = 0; c < l->cap; c++) {
		s = &l->songs[i];
		if (!s->path[0] || !strcmp(path, s->path))
			return s;
		i++;
		i %= l->cap;
	}
	return 0;
}

static void cap_iter(
	void* uptr,
	const char* path
) {
	Library* lib = uptr;
	(void)path;
	lib->cap++;
}

static void adder_iter(
	void* uptr,
	const char* path
) {
	Song* s;
	Library* lib = uptr;
	s = find_song(lib, path);
	if (get_song_meta(path, s)) {
		strcpy(s->path, path);
		lib->indices[lib->cnt++] = s - lib->songs;
	}
}

void build_library(
	Arena* a,
	Library* lib,
	const char* path
) {
	int i;
	lib->cnt = lib->cap = 0;
	iter_dir(path, cap_iter, lib);
	lib->cap += 16;
	lib->songs = arena_alloc(
		a,
		lib->cap * sizeof *lib->songs
	);
	lib->indices = arena_alloc(
		a,
		lib->cap * sizeof *lib->indices
	);
	for (i = 0; i < lib->cap; i++)
		lib->songs[i].path[0] = 0;
	iter_dir(path, adder_iter, lib);
}

void parse_vorbis_comment(
	Song* s,
	const char* com,
	int len
) {
	/* this is not safe xDDDD idrc */
	if (len > 64) {
		len = 64;
	}
	if (!memcmp(com, "ARTIST=", 7)) {
		len -= 7;
		memcpy(s->artist, com + 7, len);
		s->artist[len] = 0;
	} else if (!memcmp(com, "TITLE=", 6)) {
		len -= 6;
		memcpy(s->name, com + 6, len);
		s->name[len] = 0;
	} else if (!memcmp(com, "ALBUM=", 6)) {
		len -= 6;
		memcpy(s->album, com + 6, len);
		s->album[len] = 0;
	}
}

void flac_meta(
	void* uptr,
	drflac_metadata* m
) {
	Song* s = uptr;
	drflac_vorbis_comment_iterator i;
	drflac_uint32 len;
	const char* c;
	switch (m->type) {
		case DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT:
			drflac_init_vorbis_comment_iterator(
				&i,
				m->data.vorbis_comment.commentCount,
				m->data.vorbis_comment.pComments
			);
			while ((c = drflac_next_vorbis_comment(
				&i,
				&len
			))) {
				parse_vorbis_comment(s, c, len);
			}
			break;
	}
}

int get_song_meta(const char* path, Song* s) {
	drflac* f;
	f = drflac_open_file_with_metadata(
		path,
		flac_meta,
		s,
		0
	);
	if (f) {
		drflac_close(f);
		return 1;
	}
	s->path[0] = 0;
	return 0;
}

int sound_mix(
	void* uptr,
	unsigned char* buf,
	int size
) {
	Player* p;
	drflac* f;
	int r;
	(void)size;
	p = uptr;
	f = p->f;
	r = drflac_read_pcm_frames_s32(
		f,
		size / p->channels / 4,
		(int*)buf
	);
	return r * p->channels * 4;
}

void init_player(Player* p) {
	p->seek = 0;
	p->song = 0;
	p->play = 0;
}

void play_song(Player* p, Song* song) {
	drflac* f;
	int sr, channels;
	f = drflac_open_file(
		song->path,
		0
	);
	/* todo errors and stuff */
	if (!f) return;
	channels = f->channels;
	sr = f->sampleRate;
	p->f = f;
	p->channels = channels;
	stop_audio();
	init_audio(
		p,
		sr,
		channels
	);
}

#define DR_FLAC_IMPLEMENTATION
#include "dr_flac.h"