CLAM-Development  1.4.0
Segment.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "Segment.hxx"
23 
24 
25 
26 namespace CLAM
27 {
28 
30 //
31 // Segment
32 //
34 
36 {
37  pParent=NULL;
39  AddprHoldsData();
40  AddBeginTime();
41  AddEndTime();
42  AddSamplingRate();
43  AddChildren();//All segments may have children
44  UpdateData();
45  DefaultValues();
46 }
47 
48 void Segment::DefaultValues()
49 {
50  SetBeginTime(0);
51  SetEndTime(0);
52  SetSamplingRate(44100);
53  SetHoldsData( true );
54 
55 }
56 
57 void Segment::CopyInit(const Segment& prototype)
58 {
59  pParent=prototype.pParent;
60  mFramesSearch=prototype.mFramesSearch;
61  mFramesSearch.Set(GetFramesArray());
63 }
64 
65 
66 void Segment::CopyDataFromParent()
67 {
69  AddAudio();
70  UpdateData();
71  TTime beginTime=GetBeginTime();
72  TTime endTime=GetEndTime();
73  GetAudio().SetDuration(endTime-beginTime);
74  GetpParent()->GetAudio().GetAudioChunk(beginTime,endTime,GetAudio());
75 
76  /*TODO: right now frames with center after beginning of segment are added so
77  they may include samples outside of range: is this correct?*/
78  TIndex position=0;
79  if(beginTime>0)//if beginTime=0 it makes no sense searching and Find will return -1
80  position=GetpParent()->FindFrame(beginTime);
81  TIndex finalPosition=GetpParent()->FindFrame(endTime);
82  while(position<=finalPosition)
83  {
84  AddFrame(GetpParent()->GetFramesArray()[position]);
85  position++;
86  }
87 }
88 
89 
91 {
92  // Faster method when available
93  if(GetHoldsData())
94  return GetFramesArray().Size();
95 
96  /* TODO: if GetBeginTime()==0 Find in Search Array returns -1!!
97  but, will it happen the same with the last one*/
98  if (GetBeginTime()==0)
99  return FindFrame(GetEndTime());
100 
101  return FindFrame(GetEndTime())-FindFrame(GetBeginTime());
102 }
103 
104 
105 const Frame& Segment::GetFrame(TIndex pos) const
106 {
107  CLAM_ASSERT(GetHoldsData()||pParent,
108  "Segment::GetFrame: No available frames");
109 
110  if(GetHoldsData())
111  return GetFramesArray()[pos];
112 
113  return pParent->GetFrame(pos);
114 
115 }
116 
118 {
119  CLAM_ASSERT(GetHoldsData()||pParent,
120  "Segment::GetFrame:No available frames");
121 
122  if(GetHoldsData())
123  return GetFramesArray()[pos];
124 
125  return pParent->GetFrame(pos);
126 
127 }
128 
129 void Segment::AddFrame(Frame& newFrame)
130 {
131  CLAM_ASSERT(GetHoldsData()||pParent,
132  "Segment::AddFrame: No available frame array attribute");
133 
134  if(GetHoldsData())
135  GetFramesArray().AddElem(newFrame);
136  else
137  pParent->GetFramesArray().AddElem(newFrame);
138 
139  SetEndTime(newFrame.GetCenterTime());
140 
141 }
142 
144 {
145  CLAM_ASSERT(pos<=GetnFrames(),"Segment::DeleteFrame: Index out of bounds");
146  CLAM_ASSERT(GetHoldsData()||pParent,
147  "Segment::DeleteFrame: No available frame array attribute");
148 
149  if(GetHoldsData())
150  GetFramesArray().DeleteElem(pos);
151  else
152  pParent->GetFramesArray().DeleteElem(pos);
153 
154  if (GetnFrames()==0)
155  {
156  SetEndTime(0);
157  SetBeginTime(0);
158  }
159 
160  else
161  {
162  if(pos==GetnFrames())//it was the last frame
163  SetEndTime(GetFrame(GetnFrames()-1).GetCenterTime());
164 
165  if(pos==0)//it was first frame
166  SetBeginTime(GetFrame(0).GetCenterTime());
167  }
168 }
169 
171 {
172  CLAM_ASSERT(GetHoldsData()||pParent,
173  "Segment::FindFrame:No available frame array attribute");
174 
175  if(!GetHoldsData())
176  return pParent->FindFrame(time);
177  Frame tmpFrame;
178  tmpFrame.SetCenterTime(time);
179  if (GetnFrames()<1) return -1;
180  return GetFramesSearch().Find(tmpFrame);
181 
182 }
183 
185 {
186  return GetFrame(FindFrame(time));
187 }
188 
189 const Frame& Segment::GetFrame(TTime time) const
190 {
191  return GetFrame(FindFrame(time));
192 }
193 
195 {
196  DeleteFrame(FindFrame(time));
197 }
198 
199 void Segment::SetHoldsData(bool holdsData)
200 {
201  SetprHoldsData(holdsData);
202  if(holdsData)
203  {
204  //Adding necessary attributes
205  AddFramesArray();
206  AddAudio();
207  UpdateData();
208 
209  //Initializing some atributes
210  GetFramesSearch().Set(GetFramesArray());
211  if(pParent!=NULL)
212  {
213  CopyDataFromParent();
214  pParent=NULL;
215  }
216  }
217  else
218  {
220  RemoveAudio();
221  UpdateData();
222  }
223 }
224 
225 } // namespace CLAM
226