2 #include "bcdisplayinfo.h"
4 #include "cdripwindow.h"
6 #include "mainprogress.h"
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
15 #define _(String) gettext(String)
16 #define gettext_noop(String) String
17 #define N_(String) gettext_noop (String)
19 PluginClient* new_plugin(PluginServer *server)
21 return new CDRipMain(server);
25 CDRipMain::CDRipMain(PluginServer *server)
26 : PluginAClient(server)
31 CDRipMain::~CDRipMain()
37 char* CDRipMain::plugin_title() { return _("CD Ripper"); }
39 int CDRipMain::is_realtime() { return 0; }
41 int CDRipMain::is_multichannel() { return 1; }
43 int CDRipMain::load_defaults()
45 // set the default directory
47 sprintf(directory, "%scdripper.rc", BCASTDIR);
50 defaults = new Defaults(directory);
53 track1 = defaults->get("TRACK1", 1);
54 min1 = defaults->get("MIN1", 0);
55 sec1 = defaults->get("SEC1", 0);
56 track2 = defaults->get("TRACK2", 2);
57 min2 = defaults->get("MIN2", 0);
58 sec2 = defaults->get("SEC2", 0);
59 sprintf(device, "/dev/cdrom");
60 defaults->get("DEVICE", device);
61 startlba = defaults->get("STARTLBA", 0);
62 endlba = defaults->get("ENDLBA", 0);
66 int CDRipMain::save_defaults()
68 defaults->update("TRACK1", track1);
69 defaults->update("MIN1", min1);
70 defaults->update("SEC1", sec1);
71 defaults->update("TRACK2", track2);
72 defaults->update("MIN2", min2);
73 defaults->update("SEC2", sec2);
74 defaults->update("DEVICE", device);
75 defaults->update("STARTLBA", startlba);
76 defaults->update("ENDLBA", endlba);
81 int CDRipMain::get_parameters()
88 while(result2 && !result)
92 //printf("CDRipMain::get_parameters 1\n");
93 CDRipWindow window(this, info.get_abs_cursor_x(), info.get_abs_cursor_y());
94 //printf("CDRipMain::get_parameters 2\n");
95 window.create_objects();
96 //printf("CDRipMain::get_parameters 3\n");
97 result = window.run_window();
98 //printf("CDRipMain::get_parameters 4\n");
100 if(!result) result2 = get_toc();
101 //printf("CDRipMain::get_parameters 5 %d\n", result);
103 PluginClient::sample_rate = 44100;
107 int CDRipMain::open_drive()
109 if((cdrom = open(device, O_RDONLY)) < 0)
112 ErrorBox window(PROGRAM_NAME ": CD Ripper",
113 info.get_abs_cursor_x(),
114 info.get_abs_cursor_y());
115 window.create_objects(_("Can't open cdrom drive."));
120 ioctl(cdrom, CDROMSTART); // start motor
124 int CDRipMain::close_drive()
126 ioctl(cdrom, CDROMSTOP);
131 int CDRipMain::get_toc()
134 int result = 0, i, tracks;
135 struct cdrom_tochdr hdr;
136 struct cdrom_tocentry entry[100];
139 result = open_drive();
141 if(ioctl(cdrom, CDROMREADTOCHDR, &hdr) < 0)
144 ErrorBox window(PROGRAM_NAME ": CD Ripper",
145 info.get_abs_cursor_x(),
146 info.get_abs_cursor_y());
147 window.create_objects(_("Can't get total from table of contents."));
152 for(i = 0; i < hdr.cdth_trk1; i++)
154 entry[i].cdte_track = 1 + i;
155 entry[i].cdte_format = CDROM_LBA;
156 if(ioctl(cdrom, CDROMREADTOCENTRY, &entry[i]) < 0)
158 ioctl(cdrom, CDROMSTOP);
160 ErrorBox window(PROGRAM_NAME ": CD Ripper",
161 info.get_abs_cursor_x(),
162 info.get_abs_cursor_y());
163 window.create_objects(_("Can't get table of contents entry."));
170 entry[i].cdte_track = CDROM_LEADOUT;
171 entry[i].cdte_format = CDROM_LBA;
172 if(ioctl(cdrom, CDROMREADTOCENTRY, &entry[i]) < 0)
174 ioctl(cdrom, CDROMSTOP);
176 ErrorBox window(PROGRAM_NAME ": CD Ripper",
177 info.get_abs_cursor_x(),
178 info.get_abs_cursor_y());
179 window.create_objects(_("Can't get table of contents leadout."));
185 tracks = hdr.cdth_trk1+1;
187 if(track1 <= 0 || track1 > tracks)
189 ioctl(cdrom, CDROMSTOP);
191 ErrorBox window(PROGRAM_NAME ": CD Ripper",
192 info.get_abs_cursor_x(),
193 info.get_abs_cursor_y());
194 window.create_objects(_("Start track is out of range."));
199 if(track2 < track1 || track2 <= 0 || track2 > tracks)
201 ioctl(cdrom, CDROMSTOP);
203 ErrorBox window(PROGRAM_NAME ": CD Ripper",
204 info.get_abs_cursor_x(),
205 info.get_abs_cursor_y());
206 window.create_objects(_("End track is out of range."));
211 if(track1 == track2 && min2 == 0 && sec2 == 0)
213 ioctl(cdrom, CDROMSTOP);
215 ErrorBox window(PROGRAM_NAME ": CD Ripper",
216 info.get_abs_cursor_x(),
217 info.get_abs_cursor_y());
218 window.create_objects(_("End position is out of range."));
223 startlba = endlba = 0;
226 startlba = entry[track1 - 1].cdte_addr.lba;
227 startlba += (min1 * 44100 * 4 * 60 + sec1 * 44100 * 4) / FRAMESIZE;
229 endlba = entry[track2 - 1].cdte_addr.lba;
232 endlba += (min2 * 44100 * 4 * 60 + sec2 * 44100 * 4) / FRAMESIZE;
236 //printf("CDRipMain::get_toc %ld %ld\n", startlba, endlba);
241 int CDRipMain::start_loop()
246 //printf("CDRipMain::start_loop 1\n");
248 FRAME = 4; // 2 bytes 2 channels
249 previewing = 3; // defeat bug in hardware
250 fragment_length = PluginClient::in_buffer_size * FRAME;
251 fragment_length /= NFRAMES * FRAMESIZE;
252 fragment_length *= NFRAMES * FRAMESIZE;
253 total_length = (endlba - startlba) * FRAMESIZE / fragment_length + previewing + 1;
254 result = open_drive();
255 //printf("CDRipMain::start_loop 1 %d\n", interactive);
257 // thread out progress
260 char string[BCTEXTLEN];
261 sprintf(string, "%s...", plugin_title());
262 progress = start_progress(string, total_length);
264 //printf("CDRipMain::start_loop 1\n");
266 // get still more CD parameters
269 startlba_fragment = startlba - fragment_length * previewing / FRAMESIZE;
270 buffer = new char[fragment_length];
271 arg.addr.lba = startlba_fragment;
272 arg.addr_format = CDROM_LBA;
273 arg.nframes = NFRAMES;
274 //printf("CDRipMain::start_loop 2\n");
280 int CDRipMain::stop_loop()
284 progress->stop_progress();
293 int CDRipMain::process_loop(double **plugin_buffer, int64_t &write_length)
296 //printf("CDRipMain::process_loop 1\n");
299 if(arg.addr.lba < endlba && !endofselection)
301 if(arg.addr.lba + fragment_length / FRAMESIZE > endlba)
303 fragment_length = (endlba - arg.addr.lba) / NFRAMES;
304 fragment_length *= NFRAMES * FRAMESIZE;
307 //printf("CDRipMain::process_loop 2 %d %d\n", arg.addr.lba, endlba);
309 for(i = 0; i < fragment_length;
310 i += NFRAMES * FRAMESIZE,
311 arg.addr.lba += NFRAMES)
313 arg.buf = (unsigned char*)&buffer[i];
314 for(attempts = 0; attempts < 3; attempts++)
316 if(!(ioctl(cdrom, CDROMREADAUDIO, &arg)))
321 if(attempts == 2 && !previewing) printf("Can't read CD audio.\n");
324 //printf("CDRipMain::process_loop 3\n");
326 if(arg.addr.lba > startlba)
328 // convert to doubles
329 fragment_samples = fragment_length / FRAME;
330 for(j = 0; j < 2 && j < PluginClient::total_in_buffers; j++)
332 buffer_channel = (int16_t*)buffer + j;
333 output_buffer = plugin_buffer[j];
334 for(k = 0, l = 0; l < fragment_samples; k += 2, l++)
336 output_buffer[l] = buffer_channel[k];
337 output_buffer[l] /= 0x7fff;
341 write_length = fragment_samples;
343 //printf("CDRipMain::process_loop 5 %d\n", interactive);
348 if(!result) result = progress->update(currentlength);
350 //printf("CDRipMain::process_loop 6\n");
354 //printf("CDRipMain::process_loop 7\n");
359 //printf("CDRipMain::process_loop 8 %d %d\n", endofselection, result);
360 return endofselection || result;