File: gap_timeconv.c

package info (click to toggle)
gimp-gap 2.6.0%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,656 kB
  • ctags: 7,358
  • sloc: ansic: 119,801; sh: 3,890; makefile: 932; lisp: 97; pascal: 55
file content (157 lines) | stat: -rw-r--r-- 4,343 bytes parent folder | download | duplicates (7)
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
/*  gap_timeconv.c
 *
 *  This module handles conversions framenumber/Rate  --> timestring (mm:ss:msec)
 */
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* revision history:
 * version 1.3.19a; 2003/09/06  hof: added more converter procedures for audio
 * version 1.3.14c; 2003/06/14  hof: created
 */

#include "config.h"

#include <stdio.h>

#include "libgimp/gimp.h"
#include "gap_timeconv.h"
#include "gap-intl.h"

/* -------------------------------
 * gap_timeconv_msecs_to_timestr
 * -------------------------------
 * convert Input milliseconds value
 * into time string formated as mm:ss:msec
 * (minutes:seconds:milliseconds)
 *
 * the caller must provide a pointer to txt buffer
 * where the output is written into at txt_size.
 *
 * the txt buffer size should be at last 12 bytes
 */
void
gap_timeconv_msecs_to_timestr(gint32 tmsec, gchar *txt, gint txt_size)
{
  gint32   tms;
  gint32   tsec;
  gint32   tmin;

  tms = tmsec % 1000;
  tsec = (tmsec / 1000) % 60;
  tmin = tmsec / 60000;

  g_snprintf(txt, txt_size,"%02d:%02d:%03d"
            , (int)tmin, (int)tsec, (int)tms);
}  /* end gap_timeconv_msecs_to_timestr */

/* -------------------------------
 * gap_timeconv_framenr_to_timestr
 * -------------------------------
 * convert Input framenumber and framerate unit frames/sec)
 * into time string formated as mm:ss:msec
 * (minutes:seconds:milliseconds)
 *
 * the caller must provide a pointer to txt buffer
 * where the output is written into at txt_size.
 *
 * the txt buffer size should be at last 12 bytes
 */
void
gap_timeconv_framenr_to_timestr( gint32 framenr, gdouble framerate, gchar *txt, gint txt_size)
{
  gdouble  msec_per_frame;
  gint32   tmsec;

  if(framerate > 0)
  {
    msec_per_frame = 1000.0 / framerate;
  }
  else
  {
    msec_per_frame = 1000.0;
  } 

  if(framenr >= 0)
  {
    tmsec = framenr * msec_per_frame;
    gap_timeconv_msecs_to_timestr (tmsec, txt, txt_size);
  }
  else
  { g_snprintf(txt, txt_size, "??:??:???");
  }

}  /* end gap_timeconv_framenr_to_timestr */


/* -------------------------------
 * gap_timeconv_samples_to_timestr
 * -------------------------------
 * convert Input samples at given samplerate (unit is samples/sec)
 * into time string formated as mm:ss:msec
 * (minutes:seconds:milliseconds)
 *
 * the caller must provide a pointer to txt buffer
 * where the output is written into at txt_size.
 *
 * the txt buffer size should be at last 12 bytes
 */
void
gap_timeconv_samples_to_timestr( gint32 samples, gdouble samplerate, gchar *txt, gint txt_size)
{
  gdouble  tmsec;

  if(samplerate > 0)
  {
    tmsec = (1000.0 * (gdouble)samples) / samplerate;
  }
  else
  {
    tmsec = (1000.0 * (gdouble)samples) / 44100;
  } 

  gap_timeconv_msecs_to_timestr ((gint32)tmsec, txt, txt_size);
}  /* end gap_timeconv_samples_to_timestr */


/* -------------------------------
 * gap_timeconv_samples_to_frames
 * -------------------------------
 * convert Input samples at given samplerate (unit is samples/sec)
 *  and given framerate unit frames/sec)
 *  to audioframes. (== number of frames that have to be played at framerate
 *                      to get the same duration as samples played at samplerate)
 * returns the number of audioframes
 */
gdouble
gap_timeconv_samples_to_frames( gint32 samples, gdouble samplerate, gdouble framerate)
{
  gdouble  secs;
  gdouble  frames;

  if((samplerate < 1) || (framerate < 1))
  {
    return (0);
  }

  secs = (gdouble)samples / samplerate;
  frames = secs * framerate;
  
  return(frames);
}  /* end gap_timeconv_samples_to_frames */