| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY, without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <QFileDialog> |
|---|
| 24 | #include <QProgressBar> |
|---|
| 25 | #include <QLabel> |
|---|
| 26 | #include <QPixmap> |
|---|
| 27 | |
|---|
| 28 | #include "ExportSongDialog.h" |
|---|
| 29 | #include "Skin.h" |
|---|
| 30 | #include "HydrogenApp.h" |
|---|
| 31 | #include "Mixer/Mixer.h" |
|---|
| 32 | |
|---|
| 33 | #include <hydrogen/note.h> |
|---|
| 34 | #include <hydrogen/Pattern.h> |
|---|
| 35 | #include <hydrogen/instrument.h> |
|---|
| 36 | #include <hydrogen/Song.h> |
|---|
| 37 | #include <hydrogen/hydrogen.h> |
|---|
| 38 | #include <hydrogen/IO/AudioOutput.h> |
|---|
| 39 | |
|---|
| 40 | #include <memory> |
|---|
| 41 | |
|---|
| 42 | using namespace H2Core; |
|---|
| 43 | |
|---|
| 44 | ExportSongDialog::ExportSongDialog(QWidget* parent) |
|---|
| 45 | : QDialog(parent) |
|---|
| 46 | , Object( "ExportSongDialog" ) |
|---|
| 47 | , m_bExporting( false ) |
|---|
| 48 | { |
|---|
| 49 | setupUi( this ); |
|---|
| 50 | setModal( true ); |
|---|
| 51 | setWindowTitle( trUtf8( "Export song" ) ); |
|---|
| 52 | // setIcon( QPixmap( Skin::getImagePath() + "/icon16.png" ) ); |
|---|
| 53 | |
|---|
| 54 | HydrogenApp::get_instance()->addEventListener( this ); |
|---|
| 55 | |
|---|
| 56 | m_pProgressBar->setValue( 0 ); |
|---|
| 57 | sampleRateCombo->setCurrentIndex(1); |
|---|
| 58 | sampleDepthCombo->setCurrentIndex(1); |
|---|
| 59 | |
|---|
| 60 | QString defaultFilename( Hydrogen::get_instance()->getSong()->get_filename() ); |
|---|
| 61 | if( Hydrogen::get_instance()->getSong()->get_filename().isEmpty() ) |
|---|
| 62 | defaultFilename = Hydrogen::get_instance()->getSong()->__name; |
|---|
| 63 | defaultFilename.replace( '*', "_" ); |
|---|
| 64 | defaultFilename.replace( ".h2song", "" ); |
|---|
| 65 | defaultFilename += ".wav"; |
|---|
| 66 | exportNameTxt->setText(defaultFilename); |
|---|
| 67 | b_QfileDialog = false; |
|---|
| 68 | m_bExportTrackOuts = true; |
|---|
| 69 | m_bTrackoutIsExporting = false; |
|---|
| 70 | m_ninstrument = 0; |
|---|
| 71 | |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | ExportSongDialog::~ExportSongDialog() |
|---|
| 77 | { |
|---|
| 78 | HydrogenApp::get_instance()->removeEventListener( this ); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /// \todo: memorizzare l'ultima directory usata |
|---|
| 84 | void ExportSongDialog::on_browseBtn_clicked() |
|---|
| 85 | { |
|---|
| 86 | static QString lastUsedDir = QDir::homePath(); |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | std::auto_ptr<QFileDialog> fd( new QFileDialog ); |
|---|
| 90 | fd->setFileMode(QFileDialog::AnyFile); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | if( templateCombo->currentIndex() <= 4 ) fd->setNameFilter("Microsoft WAV (*.wav *.WAV)"); |
|---|
| 94 | if( templateCombo->currentIndex() > 4 && templateCombo->currentIndex() < 8 ) fd->setNameFilter( "Apple AIFF (*.aiff *.AIFF)"); |
|---|
| 95 | if( templateCombo->currentIndex() == 8) fd->setNameFilter( "Lossless Flac (*.flac *.FLAC)"); |
|---|
| 96 | if( templateCombo->currentIndex() == 9) fd->setNameFilter( "Compressed Ogg (*.ogg *.OGG)"); |
|---|
| 97 | |
|---|
| 98 | fd->setDirectory( lastUsedDir ); |
|---|
| 99 | fd->setAcceptMode( QFileDialog::AcceptSave ); |
|---|
| 100 | fd->setWindowTitle( trUtf8( "Export song" ) ); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | QString defaultFilename = exportNameTxt->text(); |
|---|
| 104 | |
|---|
| 105 | fd->selectFile(defaultFilename); |
|---|
| 106 | |
|---|
| 107 | QString filename = ""; |
|---|
| 108 | if (fd->exec()) { |
|---|
| 109 | filename = fd->selectedFiles().first(); |
|---|
| 110 | b_QfileDialog = true; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if ( ! filename.isEmpty() ) { |
|---|
| 114 | lastUsedDir = fd->directory().absolutePath(); |
|---|
| 115 | QString sNewFilename = filename; |
|---|
| 116 | exportNameTxt->setText(filename); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if( filename.endsWith( ".ogg" ) || filename.endsWith( ".OGG" ) ){ |
|---|
| 120 | sampleRateCombo->hide(); |
|---|
| 121 | sampleDepthCombo->hide(); |
|---|
| 122 | label->hide(); |
|---|
| 123 | label_2->hide(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | void ExportSongDialog::on_okBtn_clicked() |
|---|
| 131 | { |
|---|
| 132 | if (m_bExporting) { |
|---|
| 133 | return; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | QString filename = exportNameTxt->text(); |
|---|
| 137 | if ( QFile( filename ).exists() == true && b_QfileDialog == false ) { |
|---|
| 138 | int res = QMessageBox::information( this, "Hydrogen", tr( "The file %1 exists. \nOverwrite the existing file?").arg(filename), tr("&Ok"), tr("&Cancel"), 0, 1 ); |
|---|
| 139 | if (res == 1 ) return; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | m_bExporting = tocheckBox->isChecked();//only for testing |
|---|
| 143 | if( m_bExporting ){ |
|---|
| 144 | m_bTrackoutIsExporting = true; |
|---|
| 145 | } |
|---|
| 146 | Hydrogen::get_instance()->startExportSong( filename, sampleRateCombo->currentText().toInt(), sampleDepthCombo->currentText().toInt() ); |
|---|
| 147 | |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void ExportSongDialog::exportTracks() |
|---|
| 151 | { |
|---|
| 152 | Song *pSong = Hydrogen::get_instance()->getSong(); |
|---|
| 153 | if( m_ninstrument <= pSong->get_instrument_list()->get_size() -1 ){ |
|---|
| 154 | |
|---|
| 155 | bool instrumentexists = false; |
|---|
| 156 | //if a instrument contains no notes we jump to the next instrument |
|---|
| 157 | unsigned nPatterns = pSong->get_pattern_list()->get_size(); |
|---|
| 158 | for ( unsigned i = 0; i < nPatterns; i++ ) { |
|---|
| 159 | Pattern *pat = pSong->get_pattern_list()->get( i ); |
|---|
| 160 | |
|---|
| 161 | std::multimap <int, Note*>::iterator pos; |
|---|
| 162 | for ( pos = pat->note_map.begin(); pos != pat->note_map.end(); ++pos ) { |
|---|
| 163 | Note *pNote = pos->second; |
|---|
| 164 | assert( pNote ); |
|---|
| 165 | if( pNote->get_instrument()->get_name() == Hydrogen::get_instance()->getSong()->get_instrument_list()->get(m_ninstrument)->get_name() ){ |
|---|
| 166 | instrumentexists = true; |
|---|
| 167 | break; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if( !instrumentexists ){ |
|---|
| 173 | if( m_ninstrument == Hydrogen::get_instance()->getSong()->get_instrument_list()->get_size() -1 ){ |
|---|
| 174 | m_bTrackoutIsExporting = false;// |
|---|
| 175 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument );//solo instrument. this will disable all other instrument-solos |
|---|
| 176 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument );//unsolo this instrument because exporting is finished |
|---|
| 177 | m_ninstrument = 0; |
|---|
| 178 | return; |
|---|
| 179 | }else |
|---|
| 180 | { |
|---|
| 181 | m_ninstrument++; |
|---|
| 182 | exportTracks(); |
|---|
| 183 | return; |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | QStringList filenamelist = exportNameTxt->text().split("."); |
|---|
| 188 | |
|---|
| 189 | QString firstitem = ""; |
|---|
| 190 | if( !filenamelist.isEmpty() ){ |
|---|
| 191 | firstitem = filenamelist.first(); |
|---|
| 192 | } |
|---|
| 193 | QString newitem = firstitem + "-" + Hydrogen::get_instance()->getSong()->get_instrument_list()->get(m_ninstrument)->get_name(); |
|---|
| 194 | int listsize = filenamelist.size(); |
|---|
| 195 | filenamelist.replace ( listsize -2, newitem ); |
|---|
| 196 | QString filename = filenamelist.join("."); |
|---|
| 197 | |
|---|
| 198 | if ( QFile( filename ).exists() == true && b_QfileDialog == false ) { |
|---|
| 199 | int res = QMessageBox::information( this, "Hydrogen", tr( "The file %1 exists. \nOverwrite the existing file?").arg(filename), tr("&Ok"), tr("&Cancel"), 0, 1 ); |
|---|
| 200 | if (res == 1 ) return; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | Hydrogen::get_instance()->stopExportSong(); |
|---|
| 204 | m_bExporting = false; |
|---|
| 205 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument ); |
|---|
| 206 | Hydrogen::get_instance()->startExportSong( filename, sampleRateCombo->currentText().toInt(), sampleDepthCombo->currentText().toInt() ); |
|---|
| 207 | if( m_ninstrument == Hydrogen::get_instance()->getSong()->get_instrument_list()->get_size() -1 ){ |
|---|
| 208 | m_bTrackoutIsExporting = false;// |
|---|
| 209 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument ); |
|---|
| 210 | m_ninstrument = 0; |
|---|
| 211 | }else |
|---|
| 212 | { |
|---|
| 213 | m_ninstrument++; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | void ExportSongDialog::on_closeBtn_clicked() |
|---|
| 219 | { |
|---|
| 220 | Hydrogen::get_instance()->stopExportSong(); |
|---|
| 221 | m_bExporting = false; |
|---|
| 222 | accept(); |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | void ExportSongDialog::on_templateCombo_currentIndexChanged(int index ) |
|---|
| 228 | { |
|---|
| 229 | /**index |
|---|
| 230 | * 0 = wav 44100 | 16 |
|---|
| 231 | * 1 = wav 48000 | 16 |
|---|
| 232 | * 2 = wav 48000 | 24 |
|---|
| 233 | * 3 = wav 22050 | 8 |
|---|
| 234 | * 4 = wav 96000 | 32 |
|---|
| 235 | * 5 = aiff 44100 | 16 |
|---|
| 236 | * 6 = aiff 48000 | 16 |
|---|
| 237 | * 7 = aiff 48000 | 24 |
|---|
| 238 | * 8 = flac 48000 |
|---|
| 239 | * 9 = ogg VBR , disable comboboxes |
|---|
| 240 | **/ |
|---|
| 241 | |
|---|
| 242 | QString filename; |
|---|
| 243 | QStringList splitty; |
|---|
| 244 | |
|---|
| 245 | switch ( index ) { |
|---|
| 246 | case 0: |
|---|
| 247 | sampleRateCombo->show(); |
|---|
| 248 | sampleDepthCombo->show(); |
|---|
| 249 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 250 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 251 | filename = exportNameTxt->text(); |
|---|
| 252 | splitty = filename.split("."); |
|---|
| 253 | splitty.removeLast(); |
|---|
| 254 | filename = splitty.join( "." ); |
|---|
| 255 | filename += ".wav"; |
|---|
| 256 | break; |
|---|
| 257 | case 1: |
|---|
| 258 | sampleRateCombo->show(); |
|---|
| 259 | sampleDepthCombo->show(); |
|---|
| 260 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 261 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 262 | filename = exportNameTxt->text(); |
|---|
| 263 | splitty = filename.split("."); |
|---|
| 264 | splitty.removeLast(); |
|---|
| 265 | filename = splitty.join( "." ); |
|---|
| 266 | filename += ".wav"; |
|---|
| 267 | break; |
|---|
| 268 | case 2: |
|---|
| 269 | sampleRateCombo->show(); |
|---|
| 270 | sampleDepthCombo->show(); |
|---|
| 271 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 272 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 273 | filename = exportNameTxt->text(); |
|---|
| 274 | splitty = filename.split("."); |
|---|
| 275 | splitty.removeLast(); |
|---|
| 276 | filename = splitty.join( "." ); |
|---|
| 277 | filename += ".wav"; |
|---|
| 278 | break; |
|---|
| 279 | case 3: |
|---|
| 280 | sampleRateCombo->show(); |
|---|
| 281 | sampleDepthCombo->show(); |
|---|
| 282 | sampleRateCombo->setCurrentIndex ( 0 ); //22050hz |
|---|
| 283 | sampleDepthCombo->setCurrentIndex ( 0 ); //8bit |
|---|
| 284 | filename = exportNameTxt->text(); |
|---|
| 285 | splitty = filename.split("."); |
|---|
| 286 | splitty.removeLast(); |
|---|
| 287 | filename = splitty.join( "." ); |
|---|
| 288 | filename += ".wav"; |
|---|
| 289 | break; |
|---|
| 290 | case 4: |
|---|
| 291 | sampleRateCombo->show(); |
|---|
| 292 | sampleDepthCombo->show(); |
|---|
| 293 | sampleRateCombo->setCurrentIndex ( 3 ); //96000hz |
|---|
| 294 | sampleDepthCombo->setCurrentIndex ( 3 ); //32bit |
|---|
| 295 | filename = exportNameTxt->text(); |
|---|
| 296 | splitty = filename.split("."); |
|---|
| 297 | splitty.removeLast(); |
|---|
| 298 | filename = splitty.join( "." ); |
|---|
| 299 | filename += ".wav"; |
|---|
| 300 | break; |
|---|
| 301 | case 5: |
|---|
| 302 | sampleRateCombo->show(); |
|---|
| 303 | sampleDepthCombo->show(); |
|---|
| 304 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 305 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 306 | filename = exportNameTxt->text(); |
|---|
| 307 | splitty = filename.split("."); |
|---|
| 308 | splitty.removeLast(); |
|---|
| 309 | filename = splitty.join( "." ); |
|---|
| 310 | filename += ".aiff"; |
|---|
| 311 | break; |
|---|
| 312 | case 6: |
|---|
| 313 | sampleRateCombo->show(); |
|---|
| 314 | sampleDepthCombo->show(); |
|---|
| 315 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 316 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 317 | filename = exportNameTxt->text(); |
|---|
| 318 | splitty = filename.split("."); |
|---|
| 319 | splitty.removeLast(); |
|---|
| 320 | filename = splitty.join( "." ); |
|---|
| 321 | filename += ".aiff"; |
|---|
| 322 | break; |
|---|
| 323 | case 7: |
|---|
| 324 | sampleRateCombo->show(); |
|---|
| 325 | sampleDepthCombo->show(); |
|---|
| 326 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 327 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 328 | filename = exportNameTxt->text(); |
|---|
| 329 | splitty = filename.split("."); |
|---|
| 330 | splitty.removeLast(); |
|---|
| 331 | filename = splitty.join( "." ); |
|---|
| 332 | filename += ".aiff"; |
|---|
| 333 | break; |
|---|
| 334 | case 8: |
|---|
| 335 | sampleRateCombo->show(); |
|---|
| 336 | sampleDepthCombo->show(); |
|---|
| 337 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 338 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 339 | filename = exportNameTxt->text(); |
|---|
| 340 | splitty = filename.split("."); |
|---|
| 341 | splitty.removeLast(); |
|---|
| 342 | filename = splitty.join( "." ); |
|---|
| 343 | filename += ".flac"; |
|---|
| 344 | break; |
|---|
| 345 | case 9: |
|---|
| 346 | sampleRateCombo->hide(); |
|---|
| 347 | sampleDepthCombo->hide(); |
|---|
| 348 | label->hide(); |
|---|
| 349 | label_2->hide(); |
|---|
| 350 | filename = exportNameTxt->text(); |
|---|
| 351 | splitty = filename.split("."); |
|---|
| 352 | splitty.removeLast(); |
|---|
| 353 | filename = splitty.join( "." ); |
|---|
| 354 | filename += ".ogg"; |
|---|
| 355 | break; |
|---|
| 356 | |
|---|
| 357 | default: |
|---|
| 358 | sampleRateCombo->show(); |
|---|
| 359 | sampleDepthCombo->show(); |
|---|
| 360 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 361 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 362 | filename = exportNameTxt->text(); |
|---|
| 363 | splitty = filename.split("."); |
|---|
| 364 | splitty.removeLast(); |
|---|
| 365 | filename = splitty.join( "." ); |
|---|
| 366 | filename += ".wav"; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | exportNameTxt->setText(filename); |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | void ExportSongDialog::on_exportNameTxt_textChanged( const QString& ) |
|---|
| 376 | { |
|---|
| 377 | QString filename = exportNameTxt->text(); |
|---|
| 378 | if ( ! filename.isEmpty() ) { |
|---|
| 379 | okBtn->setEnabled(true); |
|---|
| 380 | } |
|---|
| 381 | else { |
|---|
| 382 | okBtn->setEnabled(false); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | if( filename.endsWith( ".ogg" ) || filename.endsWith( ".OGG" ) ){ |
|---|
| 386 | templateCombo->setCurrentIndex( 9 );//ogg |
|---|
| 387 | } |
|---|
| 388 | else if( filename.endsWith( ".flac" ) || filename.endsWith( ".FLAC" ) ){ |
|---|
| 389 | label->show(); |
|---|
| 390 | label_2->show(); |
|---|
| 391 | templateCombo->setCurrentIndex( 8 );//flac |
|---|
| 392 | } |
|---|
| 393 | else if( filename.endsWith( ".aiff" ) || filename.endsWith( ".AIFF" ) ){ |
|---|
| 394 | label->show(); |
|---|
| 395 | label_2->show(); |
|---|
| 396 | templateCombo->setCurrentIndex( 5 );//aiff |
|---|
| 397 | } |
|---|
| 398 | else if( filename.endsWith( ".wav" ) || filename.endsWith( ".WAV" ) ){ |
|---|
| 399 | label->show(); |
|---|
| 400 | label_2->show(); |
|---|
| 401 | templateCombo->setCurrentIndex( 0 );//wav |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | void ExportSongDialog::progressEvent( int nValue ) |
|---|
| 407 | { |
|---|
| 408 | m_pProgressBar->setValue( nValue ); |
|---|
| 409 | if ( nValue == 100 ) { |
|---|
| 410 | m_bExporting = false; |
|---|
| 411 | QFile check( exportNameTxt->text() ); |
|---|
| 412 | if ( ! check.exists() ) { |
|---|
| 413 | QMessageBox::information( this, "Hydrogen", trUtf8("Export failed!") ); |
|---|
| 414 | } |
|---|
| 415 | if( m_bExportTrackOuts && m_bTrackoutIsExporting ){ |
|---|
| 416 | exportTracks(); |
|---|
| 417 | } |
|---|
| 418 | } |
|---|
| 419 | } |
|---|