Beatmup
fragment.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "fragment.h"
20 
21 using namespace Beatmup::Fragments;
22 
23 
24 Fragment::Fragment() : referenceCount(0), sampleCount(0) {};
25 
26 
28  std::lock_guard<std::mutex> lock(access);
29  if (referenceCount > 1) {
31  Fragment* copy = clone();
32  copy->referenceCount = 1;
33  return copy;
34  }
35  return this;
36 }
37 
38 
40  std::lock_guard<std::mutex> lock(access);
42  return this;
43 }
44 
45 
47  std::unique_lock<std::mutex> lock(access);
48  if (referenceCount <= 1) {
49  lock.unlock();
50  delete this;
51  return;
52  }
54  lock.unlock();
55 }
56 
57 
59  if (!isNull())
60  fragment->drop();
61  if (src.isNull())
62  nullify();
63  else {
64  fragment = src.fragment->use();
65  offset = src.offset;
66  length = src.length;
67  }
68 }
69 
70 
72  fragment = NULL;
73  offset = length = 0;
74 }
75 
76 
77 
78 FragmentPtr::FragmentPtr(Fragment& fragment, int offset, int length):
79  fragment(&fragment), offset(offset), length(length)
80 {
81  fragment.use();
82 }
83 
84 
86  if (src.isNull()) {
87  fragment = NULL;
88  offset = length = 0;
89  }
90  else {
91  fragment = src.fragment->use();
92  offset = src.offset;
93  length = src.length;
94  }
95 }
96 
97 
99  fragment = src.fragment;
100  offset = src.offset;
101  length = src.length;
102  // nullifying source
103  src.fragment = NULL;
104  src.offset = src.length = 0;
105 }
106 
107 
109  if (!isNull())
110  fragment->drop();
111 }
112 
113 
115  fragment = fragment->edit();
116  // No need to check if the fragment is cloned or not and drop the original if it is.
117  // If it is cloned, there is at least one reference to the original fragment.
118  // So no dropping, just decrease ref counter in fragment->edit().
119 }
120 
121 
123  if (!isNull())
124  fragment->drop();
125  fragment = NULL;
126  offset = length = 0;
127 }
Represents a continuous set of data samples.
Definition: fragment.h:31
void drop()
Dereferences the frame when it is not used any more.
Definition: fragment.cpp:46
std::mutex access
exclusive access
Definition: fragment.h:35
virtual Fragment * clone() const =0
unsigned int referenceCount
number of occurrences of this frame in sequences
Definition: fragment.h:36
Fragment * edit()
Enables editing of the current frame.
Definition: fragment.cpp:27
Fragment * use()
References the frame when it is used one more time.
Definition: fragment.cpp:39
Abstract fragmented signals representation.
Definition: fragment.h:27
Pointer to a Fragment.
Definition: fragment.h:67
void operator=(const FragmentPtr &)
Definition: fragment.cpp:58
int offset
offset in samples inside the fragment since its beginning
Definition: fragment.h:70
int length
number of samples to use from the fragment
Definition: fragment.h:71
FragmentPtr()
Initialize to null.
Definition: fragment.cpp:71
JNIEnv jobject jint jint jint jfloat fragment
Beatmup::AbstractBitmap * copy
return(jlong) new Beatmup jlong jstring src