~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/tag.c -rw-r--r-- 3.3 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
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
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "log.h"
#include "misc.h"
#include "tag.h"


void tag_vorbis_comment_parse(const char *line, Tag *tags)
{
	const char *val = strchr(line, '=');
	const Gperf_match *tag = tag_match(line, val - line);
	if (!tag)
		return;

	if (!val)
	{
		fprintf(stderr, "%s tag is malformed (= not found)\n", tag->name);
		return;
	}
	if (*++val == '\0')
		fprintf(stderr, "%s tag is empty\n", tag->name);

	char *end;
	float ftmp;
	switch (tag->id)
	{
		case RG_TRACK_GAIN:
		case RG_ALBUM_GAIN:
			ftmp = strtof(val, &end);
			if (*end && strcasecmp(end, "db") && strcasecmp(end, " db"))
			{
				fprintf(stderr, "%s tag has trailing garbage: %s\n",
					tag->name, end);
				break;
			}
			if (errno == ERANGE)
			{
				fprintf(stderr, "%s tag float %s, value: %s\n", tag->name,
					ftmp == HUGE_VAL ? "overflow" : "underflow", val);
				break;
			}
			tags[tag->id].fval = ftmp;
			tags[tag->id].empty = false;
			break;

		case RG_TRACK_PEAK:
		case RG_ALBUM_PEAK:
			ftmp = strtof(val, &end);
			if (*end)
			{
				fprintf(stderr, "%s tag has trailing garbage: %s\n",
					tag->name, end);
				break;
			}
			if (errno == ERANGE)
			{
				fprintf(stderr, "%s tag float %s, value: %s\n", tag->name,
					ftmp == HUGE_VAL ? "overflow" : "underflow", val);
				break;
			}
			tags[tag->id].fval = ftmp;
			tags[tag->id].empty = false;
			break;

		default:
			;
	}
	switch (tag_type[tag->id])
	{
		case STRING:
			tags[tag->id].sval = xstrdup(val);
			tags[tag->id].empty = false;
			break;

		case FLOAT:
			;
	}
}

Tag * tags_init(void)
{
	Tag *ret = xmalloc(NUM_TAG * sizeof(Tag));
	for (size_t i = 0; i < NUM_TAG; ++i)
		ret[i].empty = true;
	return ret;
}

void tags_free(const Tag *tags)
{
	for (size_t i = 0; i < NUM_TAG; ++i)
	{
		if (!tags[i].empty && tag_type[i] == STRING)
			free(tags[i].sval);
	}
	free((void *)tags);
}

/* Reverse gperf matching for printing */
static const char *const tag_key[NUM_TAG] =
{
	"TITLE",
	"ARTIST",
	"ALBUMARTIST",
	"ALBUM",
	"DATE",
	"TRACKNUMBER",
	"TRACKTOTAL",
	"DISCNUMBER",
	"DISCTOTAL",
	"DISCSUBTITLE",
	"REPLAYGAIN_TRACK_GAIN",
	"REPLAYGAIN_TRACK_PEAK",
	"REPLAYGAIN_ALBUM_GAIN",
	"REPLAYGAIN_ALBUM_PEAK",
};

void tags_fprint(FILE *stream, const Tag *tags)
{
	for (size_t i = 0; i < NUM_TAG; ++i)
	{
		if (!tags[i].empty)
		{
			switch (tag_type[i])
			{
				case STRING:
					fprintf(stream, "tag %s %s\n",tag_key[i], tags[i].sval);
					break;

				case FLOAT:
					fprintf(stream, "tag %s %f\n", tag_key[i], tags[i].fval);
			}
		}
	}
}

float tag_compute_replay_gain(const Tag *tags, const ReplayGain_type rgtype)
{
	float gain, peak = 1.f;
	if (rgtype == ALBUM_GAIN && !tags[RG_ALBUM_GAIN].empty)
	{
		gain = tags[RG_ALBUM_GAIN].fval;
		if (tags[RG_ALBUM_PEAK].empty)
		{
			log_append(LOG_WARNING, "No RG_ALBUM_PEAK tag found, clipping "
				"could happend");
		}
		else
			peak = tags[RG_ALBUM_PEAK].fval;
	}
	else if (rgtype == TRACK_GAIN && !tags[RG_TRACK_GAIN].empty)
	{
		gain = tags[RG_TRACK_GAIN].fval;
		if (tags[RG_TRACK_PEAK].empty)
		{
			log_append(LOG_WARNING, "No RG_TRACK_PEAK tag found, clipping "
				"could happend");
		}
		else
			peak = tags[RG_TRACK_PEAK].fval;
	}
	else
		return 1.f;

	return fminf(powf(10, gain / 20), 1 / peak);
}