libmp3splt
src/conversions.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * libmp3splt -- library based on mp3splt,
00004  *               for mp3/ogg splitting without decoding
00005  *
00006  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
00007  * Copyright (c) 2005-2011 Alexandru Munteanu - io_fx@yahoo.fr
00008  *
00009  * http://mp3splt.sourceforge.net
00010  *
00011  *********************************************************/
00012 
00013 /**********************************************************
00014  *
00015  * This program is free software; you can redistribute it and/or
00016  * modify it under the terms of the GNU General Public License
00017  * as published by the Free Software Foundation; either version 2
00018  * of the License, or (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00028  * 02111-1307,
00029  * USA.
00030  *
00031  *********************************************************/
00032 
00038 #include <math.h>
00039 #include <string.h>
00040 
00041 #include "splt.h"
00042 
00044 long splt_co_convert_to_hundreths(const char *s)
00045 {
00046   long minutes = 0, seconds = 0, hundredths = 0, i = 0;
00047 
00048   for(i=0; i< strlen(s); i++)
00049   {
00050     if ((s[i]<0x30 || s[i] > 0x39) && (s[i]!=':'))
00051     {
00052       return -1;
00053     }
00054   }
00055 
00056   if (sscanf(s, "%ld:%ld:%ld", &minutes, &seconds, &hundredths) < 2)
00057   {
00058     return -1;
00059   }
00060 
00061   if ((minutes < 0) || (seconds < 0) || (hundredths < 0))
00062   {
00063     return -1;
00064   }
00065 
00066   if ((seconds > 59) || (hundredths > 99))
00067   {
00068     return -1;
00069   }
00070 
00071   if (s[strlen(s)-2]==':')
00072   {
00073     hundredths *= 10;
00074   }
00075 
00076   long hun = hundredths;
00077   hun += (minutes * 60 + seconds) * 100;
00078 
00079   return hun;
00080 }
00081 
00083 float splt_co_convert_to_dB(double input)
00084 {
00085   float level;
00086   if (input <= 0.0)
00087   {
00088     level = -96.0;
00089   }
00090   else 
00091   {
00092     level = 20 * log10(input);
00093   }
00094 
00095   return level;
00096 }
00097 
00099 double splt_co_convert_from_dB(float input)
00100 {
00101   double amp;
00102   if (input <- 96.0)
00103   {
00104     amp = 0.0;
00105   }
00106   else 
00107   {
00108     amp = pow(10.0, input / 20.0);
00109   }
00110 
00111   return amp;
00112 }
00113 
00115 void splt_co_get_mins_secs_hundr(long split_hundr, long *mins, long *secs, long *hundr)
00116 {
00117   long h = split_hundr % 100;
00118   long split_hundr_without_h = split_hundr / 100;
00119   long m = split_hundr_without_h / 60;
00120   long s = split_hundr_without_h % 60;
00121   if (mins)
00122   {
00123     *mins = m;
00124   }
00125   if (secs)
00126   {
00127     *secs = s;
00128   }
00129   if (hundr)
00130   {
00131     *hundr = h;
00132   }
00133 }
00134 
00136 long splt_co_time_to_long(double time)
00137 {
00138   return (long) (time * 100.0);
00139 }
00140 
00142 long splt_co_time_to_long_ceil(double time)
00143 {
00144   return (long) ceil(time * 100);
00145 }
00146