rework android-rmt display, add a few buttons
[goodguy/history.git] / cinelerra-5.0 / cinelerra / indexthread.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
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 "asset.h"
23 #include "bcsignals.h"
24 #include "clip.h"
25 #include "condition.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "filexml.h"
29 #include "indexfile.h"
30 #include "indexstate.h"
31 #include "indexthread.h"
32 #include "language.h"
33 #include "maxchannels.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "preferences.h"
37 #include "mainsession.h"
38 #include "samples.h"
39 #include <unistd.h>
40 #include "trackcanvas.h"
41 #include "tracks.h"
42
43 // Read data from buffers and calculate peaks
44
45 IndexThread::IndexThread(MWindow *mwindow, 
46         IndexFile *index_file,
47         char *index_filename,
48         int64_t buffer_size, 
49         int64_t length_source)
50  : Thread(1, 0, 0)
51 {
52         this->buffer_size = buffer_size;
53         this->length_source = length_source;
54         this->mwindow = mwindow;
55         this->index_filename = index_filename;
56         this->index_file = index_file;
57
58 // initialization is completed in run
59         for(int i = 0; i < TOTAL_INDEX_BUFFERS; i++)
60         {
61 // Must allocate MAX_CHANNELS for a nested EDL
62                 int min_channels = MAX(MAX_CHANNELS, index_file->source_channels);
63                 buffer_in[i] = new Samples*[min_channels];
64                 bzero(buffer_in[i], sizeof(Samples*) * min_channels);
65
66                 output_lock[i] = new Condition(0, "IndexThread::output_lock");
67                 input_lock[i] = new Condition(1, "IndexThread::input_lock");
68
69                 for(int j = 0; j < index_file->source_channels; j++)
70                 {
71                         buffer_in[i][j] = new Samples(buffer_size);
72                 }
73         }
74
75 //printf("IndexThread::IndexThread %d\n", __LINE__);
76 //index_state->dump();
77
78         interrupt_flag = 0;
79 }
80
81 IndexThread::~IndexThread()
82 {
83         for(int i = 0; i < TOTAL_INDEX_BUFFERS; i++)
84         {
85                 for(int j = 0; j < index_file->source_channels; j++)
86                 {
87                         delete buffer_in[i][j];
88                 }
89                 delete [] buffer_in[i];
90                 delete output_lock[i];
91                 delete input_lock[i];
92         }
93 }
94
95 int IndexThread::start_build()
96 {
97         interrupt_flag = 0;
98         current_buffer = 0;
99         for(int i = 0; i <  TOTAL_INDEX_BUFFERS; i++) last_buffer[i] = 0;
100         start();
101         return 0;
102 }
103
104 int IndexThread::stop_build()
105 {
106         join();
107         return 0;
108 }
109
110 void IndexThread::run()
111 {
112         int done = 0;
113         IndexState *index_state = index_file->get_state();
114         int index_channels = index_file->source_channels;
115         index_state->add_audio_stream(index_channels, index_file->source_length);
116         index_state->init_scan(mwindow->preferences->index_size);
117         index_state->index_status = INDEX_BUILDING;
118
119         while(!interrupt_flag && !done) {
120                 output_lock[current_buffer]->lock("IndexThread::run");
121
122                 if(last_buffer[current_buffer]) done = 1;
123                 if(!interrupt_flag && !done) {
124 // process buffer
125                         int64_t len = input_len[current_buffer];
126                         for( int ch=0; ch<index_channels; ++ch ) {
127                                 double *samples = buffer_in[current_buffer][ch]->get_data();
128                                 index_state->put_data(ch,samples,len);
129                         }
130
131 // draw simultaneously with build
132                         index_file->redraw_edits(0);
133                 }
134
135                 input_lock[current_buffer]->unlock();
136                 current_buffer++;
137                 if(current_buffer >= TOTAL_INDEX_BUFFERS) current_buffer = 0;
138         }
139
140         index_file->redraw_edits(1);
141
142 // write the index file to disk
143         Asset *asset = index_file->indexable->is_asset ? (Asset*)index_file->indexable : 0;
144         index_state->create_index(index_filename, asset);
145 }
146
147
148