summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3baudiosessionreadingjob.cpp
blob: 1af528bfb0197bd95b19e1e889c76ef9ba17b955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* 
 *
 * $Id: k3baudiosessionreadingjob.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3baudiosessionreadingjob.h"

#include <k3bthread.h>
#include <k3btoc.h>
#include <k3bcdparanoialib.h>
#include <k3bwavefilewriter.h>
#include <k3bglobals.h>
#include <k3bdevice.h>
#include <k3bcore.h>

#include <kdebug.h>
#include <tdelocale.h>

#include <unistd.h>


class K3bAudioSessionReadingJob::WorkThread : public K3bThread
{
public:
  WorkThread();
  ~WorkThread();

  void init();
  void run();
  void cancel();

  bool canceled;

  int fd;
  K3bCdparanoiaLib* paranoia;
  K3bDevice::Device* device;
  K3bDevice::Toc toc;
  K3bWaveFileWriter* waveFileWriter;
  TQStringList filenames;
  int paranoiaMode;
  int retries;
  bool neverSkip;
};


K3bAudioSessionReadingJob::WorkThread::WorkThread()
  : K3bThread(),
    fd(-1),
    paranoia(0),
    waveFileWriter(0),
    paranoiaMode(0),
    retries(50),
    neverSkip(false)
{
}


K3bAudioSessionReadingJob::WorkThread::~WorkThread()
{
  delete waveFileWriter;
  delete paranoia;
}


void K3bAudioSessionReadingJob::WorkThread::init()
{
  canceled = false;
}


void K3bAudioSessionReadingJob::WorkThread::run()
{
  if( !paranoia )
    paranoia = K3bCdparanoiaLib::create();

  if( !paranoia ) {
    emitInfoMessage( i18n("Could not load libcdparanoia."), K3bJob::ERROR );
    emitFinished(false);
    return;
  }

  if( toc.isEmpty() )
    toc = device->readToc();

  if( !paranoia->initParanoia( device, toc ) ) {
    emitInfoMessage( i18n("Could not open device %1").arg(device->blockDeviceName()),
		     K3bJob::ERROR );
    emitFinished(false);
    return;
  }

  if( !paranoia->initReading() ) {
    emitInfoMessage( i18n("Error while initializing audio ripping."), K3bJob::ERROR );
    emitFinished(false);    
    return;
  }

  device->block( true );

  // init settings
  paranoia->setMaxRetries( retries );
  paranoia->setParanoiaMode( paranoiaMode );
  paranoia->setNeverSkip( neverSkip );

  bool writeError = false;
  unsigned int trackNum = 1;
  unsigned int currentTrack = 0;
  unsigned long trackRead = 0;
  unsigned long totalRead = 0;
  unsigned int lastTrackPercent = 0;
  unsigned int lastTotalPercent = 0;
  bool newTrack = true;
  int status = 0;
  char* buffer = 0;
  while( !canceled && (buffer = paranoia->read( &status, &trackNum, fd == -1 /*when writing to a wav be want little endian */ )) ) {

    if( currentTrack != trackNum ) {
      emitNextTrack( trackNum, paranoia->toc().count() );
      trackRead = 0;
      lastTrackPercent = 0;

      currentTrack = trackNum;
      newTrack = true;
    }

    if( fd > 0 ) {
      if( ::write( fd, buffer, CD_FRAMESIZE_RAW ) != CD_FRAMESIZE_RAW ) {
	kdDebug() << "(K3bAudioSessionCopyJob::WorkThread) error while writing to fd " << fd << endl;
	writeError = true;
	break;
      }
    }
    else {
      if( newTrack ) {
	newTrack = false;

	if( !waveFileWriter )
	  waveFileWriter = new K3bWaveFileWriter();

	if( filenames.count() < currentTrack ) {
	  kdDebug() << "(K3bAudioSessionCopyJob) not enough image filenames given: " << currentTrack << endl;
	  writeError = true;
	  break;
	}

	if( !waveFileWriter->open( filenames[currentTrack-1] ) ) {
	  emitInfoMessage( i18n("Unable to open '%1' for writing.").arg(filenames[currentTrack-1]), K3bJob::ERROR );
	  writeError = true;
	  break;
	}
      }

      waveFileWriter->write( buffer, 
			     CD_FRAMESIZE_RAW, 
			     K3bWaveFileWriter::LittleEndian );
    }

    trackRead++;
    totalRead++;

    unsigned int trackPercent = 100 * trackRead / toc[currentTrack-1].length().lba();
    if( trackPercent > lastTrackPercent ) {
      lastTrackPercent = trackPercent;
      emitSubPercent( lastTrackPercent );
    }
    unsigned int totalPercent = 100 * totalRead / paranoia->rippedDataLength();
    if( totalPercent > lastTotalPercent ) {
      lastTotalPercent = totalPercent;
      emitPercent( lastTotalPercent );
    }
  }

  if( waveFileWriter )
    waveFileWriter->close();

  paranoia->close();

  device->block( false );

  if( status != K3bCdparanoiaLib::S_OK ) {
    emitInfoMessage( i18n("Unrecoverable error while ripping track %1.").arg(trackNum), K3bJob::ERROR );
    emitFinished(false);
    return;
  }

  emitFinished( !writeError & !canceled );
}


void K3bAudioSessionReadingJob::WorkThread::cancel()
{
  canceled = true;
  // FIXME: add backup killing like in the audio ripping and make sure to close paranoia
}




K3bAudioSessionReadingJob::K3bAudioSessionReadingJob( K3bJobHandler* jh, TQObject* parent, const char* name )
  : K3bThreadJob( jh, parent, name )
{
  m_thread = new WorkThread();
  setThread( m_thread );
}


K3bAudioSessionReadingJob::~K3bAudioSessionReadingJob()
{
  delete m_thread;
}


void K3bAudioSessionReadingJob::setDevice( K3bDevice::Device* dev )
{
  m_thread->device = dev;
  m_thread->toc = K3bDevice::Toc();
}


void K3bAudioSessionReadingJob::setToc( const K3bDevice::Toc& toc )
{
  m_thread->toc = toc;
}


void K3bAudioSessionReadingJob::writeToFd( int fd )
{
  m_thread->fd = fd;
}

void K3bAudioSessionReadingJob::setImageNames( const TQStringList& l )
{
  m_thread->filenames = l;
  m_thread->fd = -1;
}


void K3bAudioSessionReadingJob::setParanoiaMode( int m )
{
  m_thread->paranoiaMode = m;
}


void K3bAudioSessionReadingJob::setReadRetries( int r )
{
  m_thread->retries = r;
}

void K3bAudioSessionReadingJob::setNeverSkip( bool b )
{
  m_thread->neverSkip = b;
}


void K3bAudioSessionReadingJob::start()
{
  k3bcore->blockDevice( m_thread->device );
  K3bThreadJob::start();
}


void K3bAudioSessionReadingJob::cleanupJob( bool success )
{
  Q_UNUSED( success );
  k3bcore->unblockDevice( m_thread->device );
}

#include "k3baudiosessionreadingjob.moc"