libmp3splt
src/silence_utils.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 
00037 #include "splt.h"
00038 
00039 int splt_siu_ssplit_new(struct splt_ssplit **silence_list, 
00040     float begin_position, float end_position, int len, int *error)
00041 {
00042   struct splt_ssplit *temp = NULL;
00043   struct splt_ssplit *s_new = NULL;
00044 
00045   if ((s_new = malloc(sizeof(struct splt_ssplit))) == NULL)
00046   {
00047     *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00048     return -1;
00049   }
00050 
00051   s_new->len = len;
00052   s_new->begin_position = begin_position;
00053   s_new->end_position = end_position;
00054   s_new->next = NULL;
00055 
00056   temp = *silence_list;
00057   if (temp == NULL)
00058   {
00059     *silence_list = s_new; // No elements
00060     return 0;
00061   }
00062 
00063   if (temp->len < len)
00064   {
00065     s_new->next = temp;
00066     *silence_list = s_new;
00067   }
00068   else
00069   {
00070     if (temp->next == NULL)
00071     {
00072       temp->next = s_new;
00073     }
00074     else 
00075     {
00076       while (temp != NULL) 
00077       {
00078         if (temp->next != NULL) 
00079         {
00080           if (temp->next->len < len) 
00081           {
00082             //build an ordered list by len to keep most probable silence points
00083             s_new->next = temp->next;
00084             temp->next = s_new;
00085             break;
00086           }
00087         }
00088         else 
00089         {
00090           temp->next = s_new;
00091           break;
00092         }
00093         temp = temp->next;
00094       }
00095     }
00096   }
00097 
00098   return 0;
00099 }
00100 
00101 void splt_siu_ssplit_free(struct splt_ssplit **silence_list)
00102 {
00103   struct splt_ssplit *temp = NULL, *saved = NULL;
00104 
00105   if (silence_list)
00106   {
00107     temp = *silence_list;
00108 
00109     while (temp != NULL)
00110     {
00111       saved = temp->next;
00112       free(temp);
00113       temp = saved;
00114     }
00115 
00116     *silence_list = NULL;
00117   }
00118 }
00119 
00120 float splt_siu_silence_position(struct splt_ssplit *temp, float off)
00121 {
00122   float length_of_silence = (temp->end_position - temp->begin_position);
00123   return temp->begin_position + (length_of_silence * off);
00124 }
00125 
00126 int splt_siu_parse_ssplit_file(splt_state *state, FILE *log_file, int *error)
00127 {
00128   char *line = NULL; 
00129   int found = 0;
00130 
00131   while ((line = splt_io_readline(log_file, error)) != NULL)
00132   {
00133     if (*error < 0) { break; }
00134 
00135     int len = 0;
00136     float begin_position = 0, end_position = 0;
00137     if (sscanf(line, "%f\t%f\t%d", &begin_position, &end_position, &len) == 3)
00138     {
00139       splt_siu_ssplit_new(&state->silence_list, begin_position, end_position, len, error);
00140       if (*error < 0)
00141       {
00142         break;
00143       }
00144 
00145       found++;
00146     }
00147 
00148     if (line)
00149     {
00150       free(line);
00151       line = NULL;
00152     }
00153   }
00154 
00155   if (line)
00156   {
00157     free(line);
00158     line = NULL;
00159   }
00160 
00161   return found;
00162 }
00163