~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/duration.c -rw-r--r-- 1.1 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
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

#include "duration.h"
#include "misc.h"


Duration * duration_init(uint64_t nb_samples, uint64_t sample_rate)
{
	Duration *ret = xmalloc(sizeof(Duration));
	ret->nb_samples = nb_samples;
	ret->sample_rate = sample_rate;
	duration_invalid_formatted_cache(ret);
	return ret;
}

void duration_free(const Duration *dur)
{
	if (!dur)
		return;

	free((Duration *)dur);
}

void duration_invalid_formatted_cache(Duration *dur)
{
	dur->formatted[0] = '\0';
}

/* Convert a duration in seconds to a HH:MM:SS timestamp and returns it. The
 * returned string is allocated via malloc and should be freed. */
static void format_seconds(uint64_t secs, char *buf)
{
	uint64_t mins, hours;

	hours = secs / 3600;
	secs %= 3600;
	mins = secs / 60;
	secs %= 60;

	/* Max possible duration: "5124095576030431:xx:xx", length 22 */
	snprintf(buf, MAX_FORMATTED_DURATION_LENGTH + 1,
		"%02"PRIu64":%02"PRIu64":%02"PRIu64, hours, mins, secs);
}

const char * duration_get_formatted(Duration *dur)
{
	if (dur->formatted[0] == '\0')
		format_seconds(dur->nb_samples / dur->sample_rate, dur->formatted);
	return dur->formatted;
}