Debian Codegraph @

tools/klrc.c

raw
1 /* Copyright (C) 2008 Vincent Penquerc'h.
2 This file is part of the Kate codec library.
3 Written by Vincent Penquerc'h.
4
5 Use, distribution and reproduction of this library is governed
6 by a BSD style source license included with this source in the
7 file 'COPYING'. Please read these terms before distributing. */
8
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <ogg/ogg.h>
16 #include "kate/kate.h"
17 #include "kutil.h"
18 #include "klrc.h"
19
20 void *new_lrc_data(void)
21 {
22 struct lrc_data *lrc_data=kate_malloc(sizeof(struct lrc_data));
23 if (lrc_data) {
24 lrc_data->last_event_end_time=-1;
25 }
26 return lrc_data;
27 }
28
29 void free_lrc_data(void *data)
30 {
31 kate_free(data);
32 }
33
34 static ogg_int64_t granule_to_centiseconds(const kate_info *ki,ogg_int64_t granpos)
35 {
36 return (100*granpos*ki->gps_denominator+ki->gps_numerator/2)/ki->gps_numerator;
37 }
38
39 static void centiseconds_to_time(ogg_int64_t cs,int *minutes,int *seconds,int *centiseconds)
40 {
41 *minutes=cs/(60*100);
42 *seconds=cs/100%60;
43 *centiseconds=cs%100;
44 }
45
46 void write_lrc_event(FILE *fout,void *data,const kate_event *ev,ogg_int64_t granpos)
47 {
48 struct lrc_data *lrc_data=(struct lrc_data*)data;
49 int m0,s0,cs0;
50 int m1,s1,cs1;
51
52 centiseconds_to_time(granule_to_centiseconds(ev->ki,ev->start),&m0,&s0,&cs0);
53 centiseconds_to_time(granule_to_centiseconds(ev->ki,ev->start+ev->duration),&m1,&s1,&cs1);
54
55 (void)granpos;
56 (void)data;
57 if (lrc_data->last_event_end_time<0) {
58 fprintf(fout,"[%d:%02d.%02d]",m0,s0,cs0);
59 }
60 else if (lrc_data->last_event_end_time<ev->start) {
61 fprintf(fout,"\n");
62 fprintf(fout,"[%d:%02d.%02d]",m0,s0,cs0);
63 }
64
65 fprintf(fout,"%s\n",ev->text);
66 fprintf(fout,"[%d:%02d.%02d]",m1,s1,cs1);
67
68 fflush(fout);
69
70 lrc_data->last_event_end_time=ev->start+ev->duration;
71 }
72
73 void write_lrc_end(FILE *fout)
74 {
75 fprintf(fout,"\n");
76 fflush(fout);
77 }
78