| 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 = false; |
|---|
| 69 | m_ninstrument = 0; |
|---|
| 70 | |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | ExportSongDialog::~ExportSongDialog() |
|---|
| 76 | { |
|---|
| 77 | HydrogenApp::get_instance()->removeEventListener( this ); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /// \todo: memorizzare l'ultima directory usata |
|---|
| 83 | void ExportSongDialog::on_browseBtn_clicked() |
|---|
| 84 | { |
|---|
| 85 | static QString lastUsedDir = QDir::homePath(); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | std::auto_ptr<QFileDialog> fd( new QFileDialog ); |
|---|
| 89 | fd->setFileMode(QFileDialog::AnyFile); |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | if( templateCombo->currentIndex() <= 4 ) fd->setNameFilter("Microsoft WAV (*.wav *.WAV)"); |
|---|
| 93 | if( templateCombo->currentIndex() > 4 && templateCombo->currentIndex() < 8 ) fd->setNameFilter( "Apple AIFF (*.aiff *.AIFF)"); |
|---|
| 94 | if( templateCombo->currentIndex() == 8) fd->setNameFilter( "Lossless Flac (*.flac *.FLAC)"); |
|---|
| 95 | if( templateCombo->currentIndex() == 9) fd->setNameFilter( "Compressed Ogg (*.ogg *.OGG)"); |
|---|
| 96 | |
|---|
| 97 | fd->setDirectory( lastUsedDir ); |
|---|
| 98 | fd->setAcceptMode( QFileDialog::AcceptSave ); |
|---|
| 99 | fd->setWindowTitle( trUtf8( "Export song" ) ); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | QString defaultFilename = exportNameTxt->text(); |
|---|
| 103 | |
|---|
| 104 | fd->selectFile(defaultFilename); |
|---|
| 105 | |
|---|
| 106 | QString filename = ""; |
|---|
| 107 | if (fd->exec()) { |
|---|
| 108 | filename = fd->selectedFiles().first(); |
|---|
| 109 | b_QfileDialog = true; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | if ( ! filename.isEmpty() ) { |
|---|
| 113 | lastUsedDir = fd->directory().absolutePath(); |
|---|
| 114 | QString sNewFilename = filename; |
|---|
| 115 | exportNameTxt->setText(filename); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if( filename.endsWith( ".ogg" ) || filename.endsWith( ".OGG" ) ){ |
|---|
| 119 | sampleRateCombo->hide(); |
|---|
| 120 | sampleDepthCombo->hide(); |
|---|
| 121 | label->hide(); |
|---|
| 122 | label_2->hide(); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | void ExportSongDialog::on_okBtn_clicked() |
|---|
| 130 | { |
|---|
| 131 | if (m_bExporting) { |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | QString filename = exportNameTxt->text(); |
|---|
| 136 | if ( QFile( filename ).exists() == true && b_QfileDialog == false ) { |
|---|
| 137 | int res = QMessageBox::information( this, "Hydrogen", tr( "The file %1 exists. \nOverwrite the existing file?").arg(filename), tr("&Ok"), tr("&Cancel"), 0, 1 ); |
|---|
| 138 | if (res == 1 ) return; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | m_bExporting = tocheckBox->isChecked();//only for testing |
|---|
| 142 | if( m_bExporting ){ |
|---|
| 143 | m_bExportTrackouts = true; |
|---|
| 144 | } |
|---|
| 145 | Hydrogen::get_instance()->startExportSong( filename, sampleRateCombo->currentText().toInt(), sampleDepthCombo->currentText().toInt() ); |
|---|
| 146 | |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void ExportSongDialog::exportTracks() |
|---|
| 150 | { |
|---|
| 151 | Song *pSong = Hydrogen::get_instance()->getSong(); |
|---|
| 152 | if( m_ninstrument <= pSong->get_instrument_list()->get_size() -1 ){ |
|---|
| 153 | |
|---|
| 154 | bool instrumentexists = false; |
|---|
| 155 | //if a instrument contains no notes we jump to the next instrument |
|---|
| 156 | unsigned nPatterns = pSong->get_pattern_list()->get_size(); |
|---|
| 157 | for ( unsigned i = 0; i < nPatterns; i++ ) { |
|---|
| 158 | Pattern *pat = pSong->get_pattern_list()->get( i ); |
|---|
| 159 | |
|---|
| 160 | std::multimap <int, Note*>::iterator pos; |
|---|
| 161 | for ( pos = pat->note_map.begin(); pos != pat->note_map.end(); ++pos ) { |
|---|
| 162 | Note *pNote = pos->second; |
|---|
| 163 | assert( pNote ); |
|---|
| 164 | if( pNote->get_instrument()->get_name() == Hydrogen::get_instance()->getSong()->get_instrument_list()->get(m_ninstrument)->get_name() ){ |
|---|
| 165 | instrumentexists = true; |
|---|
| 166 | break; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | if( !instrumentexists ){ |
|---|
| 172 | if( m_ninstrument == Hydrogen::get_instance()->getSong()->get_instrument_list()->get_size() -1 ){ |
|---|
| 173 | m_bExportTrackouts = false;// |
|---|
| 174 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument );//solo instrument. this will disable all other instrument-solos |
|---|
| 175 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument );//unsolo this instrument because exporting is finished |
|---|
| 176 | m_ninstrument = 0; |
|---|
| 177 | return; |
|---|
| 178 | }else |
|---|
| 179 | { |
|---|
| 180 | m_ninstrument++; |
|---|
| 181 | exportTracks(); |
|---|
| 182 | return; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | QStringList filenamelist = exportNameTxt->text().split("."); |
|---|
| 187 | |
|---|
| 188 | QString firstitem = ""; |
|---|
| 189 | if( !filenamelist.isEmpty() ){ |
|---|
| 190 | firstitem = filenamelist.first(); |
|---|
| 191 | } |
|---|
| 192 | QString newitem = firstitem + "-" + Hydrogen::get_instance()->getSong()->get_instrument_list()->get(m_ninstrument)->get_name(); |
|---|
| 193 | int listsize = filenamelist.size(); |
|---|
| 194 | filenamelist.replace ( listsize -2, newitem ); |
|---|
| 195 | QString filename = filenamelist.join("."); |
|---|
| 196 | |
|---|
| 197 | if ( QFile( filename ).exists() == true && b_QfileDialog == false ) { |
|---|
| 198 | int res = QMessageBox::information( this, "Hydrogen", tr( "The file %1 exists. \nOverwrite the existing file?").arg(filename), tr("&Ok"), tr("&Cancel"), 0, 1 ); |
|---|
| 199 | if (res == 1 ) return; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | Hydrogen::get_instance()->stopExportSong(); |
|---|
| 203 | m_bExporting = false; |
|---|
| 204 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument ); |
|---|
| 205 | Hydrogen::get_instance()->startExportSong( filename, sampleRateCombo->currentText().toInt(), sampleDepthCombo->currentText().toInt() ); |
|---|
| 206 | if( m_ninstrument == Hydrogen::get_instance()->getSong()->get_instrument_list()->get_size() -1 ){ |
|---|
| 207 | m_bExportTrackouts = false;// |
|---|
| 208 | HydrogenApp::get_instance()->getMixer()->soloClicked( m_ninstrument ); |
|---|
| 209 | m_ninstrument = 0; |
|---|
| 210 | }else |
|---|
| 211 | { |
|---|
| 212 | m_ninstrument++; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | void ExportSongDialog::on_closeBtn_clicked() |
|---|
| 218 | { |
|---|
| 219 | Hydrogen::get_instance()->stopExportSong(); |
|---|
| 220 | m_bExporting = false; |
|---|
| 221 | accept(); |
|---|
| 222 | |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | void ExportSongDialog::on_templateCombo_currentIndexChanged(int index ) |
|---|
| 227 | { |
|---|
| 228 | /**index |
|---|
| 229 | * 0 = wav 44100 | 16 |
|---|
| 230 | * 1 = wav 48000 | 16 |
|---|
| 231 | * 2 = wav 48000 | 24 |
|---|
| 232 | * 3 = wav 22050 | 8 |
|---|
| 233 | * 4 = wav 96000 | 32 |
|---|
| 234 | * 5 = aiff 44100 | 16 |
|---|
| 235 | * 6 = aiff 48000 | 16 |
|---|
| 236 | * 7 = aiff 48000 | 24 |
|---|
| 237 | * 8 = flac 48000 |
|---|
| 238 | * 9 = ogg VBR , disable comboboxes |
|---|
| 239 | **/ |
|---|
| 240 | |
|---|
| 241 | QString filename; |
|---|
| 242 | QStringList splitty; |
|---|
| 243 | |
|---|
| 244 | filename = exportNameTxt->text(); |
|---|
| 245 | splitty = filename.split("."); |
|---|
| 246 | splitty.removeLast(); |
|---|
| 247 | filename = splitty.join( "." ); |
|---|
| 248 | |
|---|
| 249 | switch ( index ) { |
|---|
| 250 | case 0: |
|---|
| 251 | sampleRateCombo->show(); |
|---|
| 252 | sampleDepthCombo->show(); |
|---|
| 253 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 254 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 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 += ".wav"; |
|---|
| 263 | break; |
|---|
| 264 | case 2: |
|---|
| 265 | sampleRateCombo->show(); |
|---|
| 266 | sampleDepthCombo->show(); |
|---|
| 267 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 268 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 269 | filename += ".wav"; |
|---|
| 270 | break; |
|---|
| 271 | case 3: |
|---|
| 272 | sampleRateCombo->show(); |
|---|
| 273 | sampleDepthCombo->show(); |
|---|
| 274 | sampleRateCombo->setCurrentIndex ( 0 ); //22050hz |
|---|
| 275 | sampleDepthCombo->setCurrentIndex ( 0 ); //8bit |
|---|
| 276 | filename += ".wav"; |
|---|
| 277 | break; |
|---|
| 278 | case 4: |
|---|
| 279 | sampleRateCombo->show(); |
|---|
| 280 | sampleDepthCombo->show(); |
|---|
| 281 | sampleRateCombo->setCurrentIndex ( 3 ); //96000hz |
|---|
| 282 | sampleDepthCombo->setCurrentIndex ( 3 ); //32bit |
|---|
| 283 | filename += ".wav"; |
|---|
| 284 | break; |
|---|
| 285 | case 5: |
|---|
| 286 | sampleRateCombo->show(); |
|---|
| 287 | sampleDepthCombo->show(); |
|---|
| 288 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 289 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 290 | filename += ".aiff"; |
|---|
| 291 | break; |
|---|
| 292 | case 6: |
|---|
| 293 | sampleRateCombo->show(); |
|---|
| 294 | sampleDepthCombo->show(); |
|---|
| 295 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 296 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 297 | filename += ".aiff"; |
|---|
| 298 | break; |
|---|
| 299 | case 7: |
|---|
| 300 | sampleRateCombo->show(); |
|---|
| 301 | sampleDepthCombo->show(); |
|---|
| 302 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 303 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 304 | filename += ".aiff"; |
|---|
| 305 | break; |
|---|
| 306 | case 8: |
|---|
| 307 | sampleRateCombo->show(); |
|---|
| 308 | sampleDepthCombo->show(); |
|---|
| 309 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 310 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 311 | filename += ".flac"; |
|---|
| 312 | break; |
|---|
| 313 | case 9: |
|---|
| 314 | sampleRateCombo->hide(); |
|---|
| 315 | sampleDepthCombo->hide(); |
|---|
| 316 | label->hide(); |
|---|
| 317 | label_2->hide(); |
|---|
| 318 | filename += ".ogg"; |
|---|
| 319 | break; |
|---|
| 320 | |
|---|
| 321 | default: |
|---|
| 322 | sampleRateCombo->show(); |
|---|
| 323 | sampleDepthCombo->show(); |
|---|
| 324 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 325 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 326 | filename += ".wav"; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | exportNameTxt->setText(filename); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | void ExportSongDialog::on_exportNameTxt_textChanged( const QString& ) |
|---|
| 336 | { |
|---|
| 337 | QString filename = exportNameTxt->text(); |
|---|
| 338 | if ( ! filename.isEmpty() ) { |
|---|
| 339 | okBtn->setEnabled(true); |
|---|
| 340 | } |
|---|
| 341 | else { |
|---|
| 342 | okBtn->setEnabled(false); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | if( filename.endsWith( ".ogg" ) || filename.endsWith( ".OGG" ) ){ |
|---|
| 346 | templateCombo->setCurrentIndex( 9 );//ogg |
|---|
| 347 | } |
|---|
| 348 | else if( filename.endsWith( ".flac" ) || filename.endsWith( ".FLAC" ) ){ |
|---|
| 349 | label->show(); |
|---|
| 350 | label_2->show(); |
|---|
| 351 | templateCombo->setCurrentIndex( 8 );//flac |
|---|
| 352 | } |
|---|
| 353 | else if( filename.endsWith( ".aiff" ) || filename.endsWith( ".AIFF" ) ){ |
|---|
| 354 | label->show(); |
|---|
| 355 | label_2->show(); |
|---|
| 356 | templateCombo->setCurrentIndex( 5 );//aiff |
|---|
| 357 | } |
|---|
| 358 | else if( filename.endsWith( ".wav" ) || filename.endsWith( ".WAV" ) ){ |
|---|
| 359 | label->show(); |
|---|
| 360 | label_2->show(); |
|---|
| 361 | templateCombo->setCurrentIndex( 0 );//wav |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | void ExportSongDialog::progressEvent( int nValue ) |
|---|
| 367 | { |
|---|
| 368 | m_pProgressBar->setValue( nValue ); |
|---|
| 369 | if ( nValue == 100 ) { |
|---|
| 370 | m_bExporting = false; |
|---|
| 371 | QFile check( exportNameTxt->text() ); |
|---|
| 372 | if ( ! check.exists() ) { |
|---|
| 373 | QMessageBox::information( this, "Hydrogen", trUtf8("Export failed!") ); |
|---|
| 374 | } |
|---|
| 375 | if( m_bExportTrackouts ){ |
|---|
| 376 | exportTracks(); |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | } |
|---|