| 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 <hydrogen/Song.h> |
|---|
| 32 | #include <hydrogen/hydrogen.h> |
|---|
| 33 | #include <hydrogen/IO/AudioOutput.h> |
|---|
| 34 | |
|---|
| 35 | #include <memory> |
|---|
| 36 | |
|---|
| 37 | using namespace H2Core; |
|---|
| 38 | |
|---|
| 39 | ExportSongDialog::ExportSongDialog(QWidget* parent) |
|---|
| 40 | : QDialog(parent) |
|---|
| 41 | , Object( "ExportSongDialog" ) |
|---|
| 42 | , m_bExporting( false ) |
|---|
| 43 | { |
|---|
| 44 | setupUi( this ); |
|---|
| 45 | setModal( true ); |
|---|
| 46 | setWindowTitle( trUtf8( "Export song" ) ); |
|---|
| 47 | // setIcon( QPixmap( Skin::getImagePath() + "/icon16.png" ) ); |
|---|
| 48 | |
|---|
| 49 | HydrogenApp::get_instance()->addEventListener( this ); |
|---|
| 50 | |
|---|
| 51 | m_pProgressBar->setValue( 0 ); |
|---|
| 52 | sampleRateCombo->setCurrentIndex(1); |
|---|
| 53 | sampleDepthCombo->setCurrentIndex(1); |
|---|
| 54 | |
|---|
| 55 | QString defaultFilename( Hydrogen::get_instance()->getSong()->get_filename() ); |
|---|
| 56 | if( Hydrogen::get_instance()->getSong()->get_filename().isEmpty() ) |
|---|
| 57 | defaultFilename = Hydrogen::get_instance()->getSong()->__name; |
|---|
| 58 | defaultFilename.replace( '*', "_" ); |
|---|
| 59 | defaultFilename.replace( ".h2song", "" ); |
|---|
| 60 | defaultFilename += ".wav"; |
|---|
| 61 | exportNameTxt->setText(defaultFilename); |
|---|
| 62 | b_QfileDialog = false; |
|---|
| 63 | |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | ExportSongDialog::~ExportSongDialog() |
|---|
| 69 | { |
|---|
| 70 | HydrogenApp::get_instance()->removeEventListener( this ); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /// \todo: memorizzare l'ultima directory usata |
|---|
| 76 | void ExportSongDialog::on_browseBtn_clicked() |
|---|
| 77 | { |
|---|
| 78 | // static QString lastUsedDir = ""; |
|---|
| 79 | static QString lastUsedDir = QDir::homePath(); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | std::auto_ptr<QFileDialog> fd( new QFileDialog ); |
|---|
| 83 | fd->setFileMode(QFileDialog::AnyFile); |
|---|
| 84 | //fd->setFilter( trUtf8("Wave file (*.wav)") ); |
|---|
| 85 | /*fd->setNameFilters( QStringList() << "Microsoft WAV (*.wav *.WAV)" |
|---|
| 86 | << "Apple AIFF (*.aiff *.AIFF)" |
|---|
| 87 | << "Lossless Flac (*.flac *.FLAC)" |
|---|
| 88 | << "Compressed Ogg (*.ogg *.OGG)" ); |
|---|
| 89 | // << "Any files (*)");*/ |
|---|
| 90 | |
|---|
| 91 | if( templateCombo->currentIndex() <= 4 ) fd->setNameFilter("Microsoft WAV (*.wav *.WAV)"); |
|---|
| 92 | if( templateCombo->currentIndex() > 4 && templateCombo->currentIndex() < 8 ) fd->setNameFilter( "Apple AIFF (*.aiff *.AIFF)"); |
|---|
| 93 | if( templateCombo->currentIndex() == 8) fd->setNameFilter( "Lossless Flac (*.flac *.FLAC)"); |
|---|
| 94 | if( templateCombo->currentIndex() == 9) fd->setNameFilter( "Compressed Ogg (*.ogg *.OGG)"); |
|---|
| 95 | |
|---|
| 96 | fd->setDirectory( lastUsedDir ); |
|---|
| 97 | fd->setAcceptMode( QFileDialog::AcceptSave ); |
|---|
| 98 | fd->setWindowTitle( trUtf8( "Export song" ) ); |
|---|
| 99 | // fd->setIcon( QPixmap( Skin::getImagePath() + "/icon16.png" ) ); |
|---|
| 100 | |
|---|
| 101 | QString defaultFilename = exportNameTxt->text(); |
|---|
| 102 | |
|---|
| 103 | fd->selectFile(defaultFilename); |
|---|
| 104 | |
|---|
| 105 | QString filename = ""; |
|---|
| 106 | if (fd->exec()) { |
|---|
| 107 | filename = fd->selectedFiles().first(); |
|---|
| 108 | b_QfileDialog = true; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if ( ! filename.isEmpty() ) { |
|---|
| 112 | lastUsedDir = fd->directory().absolutePath(); |
|---|
| 113 | QString sNewFilename = filename; |
|---|
| 114 | // if ( sNewFilename.endsWith( ".wav" ) == false ) { |
|---|
| 115 | // filename += ".wav"; |
|---|
| 116 | // } |
|---|
| 117 | |
|---|
| 118 | exportNameTxt->setText(filename); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | if( filename.endsWith( ".ogg" ) || filename.endsWith( ".OGG" ) ){ |
|---|
| 122 | sampleRateCombo->hide(); |
|---|
| 123 | sampleDepthCombo->hide(); |
|---|
| 124 | label->hide(); |
|---|
| 125 | label_2->hide(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | void ExportSongDialog::on_okBtn_clicked() |
|---|
| 133 | { |
|---|
| 134 | if (m_bExporting) { |
|---|
| 135 | return; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | m_pSamplerateLbl->setText( trUtf8( "Sample rate: %1" ).arg( sampleDepthCombo->currentText() ) ); |
|---|
| 139 | QString filename = exportNameTxt->text(); |
|---|
| 140 | if ( QFile( filename ).exists() == true && b_QfileDialog == false ) { |
|---|
| 141 | int res = QMessageBox::information( this, "Hydrogen", tr( "The file %1 exists. \nOverwrite the existing file?").arg(filename), tr("&Ok"), tr("&Cancel"), 0, 1 ); |
|---|
| 142 | if (res == 1 ) return; |
|---|
| 143 | } |
|---|
| 144 | m_bExporting = true; |
|---|
| 145 | |
|---|
| 146 | Hydrogen::get_instance()->startExportSong( filename, sampleRateCombo->currentText().toInt(), sampleDepthCombo->currentText().toInt() ); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | void ExportSongDialog::on_closeBtn_clicked() |
|---|
| 152 | { |
|---|
| 153 | Hydrogen::get_instance()->stopExportSong(); |
|---|
| 154 | m_bExporting = false; |
|---|
| 155 | accept(); |
|---|
| 156 | |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | void ExportSongDialog::on_templateCombo_currentIndexChanged(int index ) |
|---|
| 161 | { |
|---|
| 162 | /**index |
|---|
| 163 | * 0 = wav 44100 | 16 |
|---|
| 164 | * 1 = wav 48000 | 16 |
|---|
| 165 | * 2 = wav 48000 | 24 |
|---|
| 166 | * 3 = wav 22050 | 8 |
|---|
| 167 | * 4 = wav 96000 | 32 |
|---|
| 168 | * 5 = aiff 44100 | 16 |
|---|
| 169 | * 6 = aiff 48000 | 16 |
|---|
| 170 | * 7 = aiff 48000 | 24 |
|---|
| 171 | * 8 = flac 48000 |
|---|
| 172 | * 9 = ogg VBR , disable comboboxes |
|---|
| 173 | **/ |
|---|
| 174 | // QMessageBox::information( this, "Hydrogen", trUtf8("index %1").arg(index) ); |
|---|
| 175 | QString filename; |
|---|
| 176 | QStringList splitty; |
|---|
| 177 | switch ( index ) { |
|---|
| 178 | case 0: |
|---|
| 179 | sampleRateCombo->show(); |
|---|
| 180 | sampleDepthCombo->show(); |
|---|
| 181 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 182 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 183 | filename = exportNameTxt->text(); |
|---|
| 184 | splitty = filename.split("."); |
|---|
| 185 | splitty.removeLast(); |
|---|
| 186 | filename = splitty.join( "." ); |
|---|
| 187 | filename += ".wav"; |
|---|
| 188 | exportNameTxt->setText(filename); |
|---|
| 189 | break; |
|---|
| 190 | case 1: |
|---|
| 191 | sampleRateCombo->show(); |
|---|
| 192 | sampleDepthCombo->show(); |
|---|
| 193 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 194 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 195 | filename = exportNameTxt->text(); |
|---|
| 196 | splitty = filename.split("."); |
|---|
| 197 | splitty.removeLast(); |
|---|
| 198 | filename = splitty.join( "." ); |
|---|
| 199 | filename += ".wav"; |
|---|
| 200 | exportNameTxt->setText(filename); |
|---|
| 201 | break; |
|---|
| 202 | case 2: |
|---|
| 203 | sampleRateCombo->show(); |
|---|
| 204 | sampleDepthCombo->show(); |
|---|
| 205 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 206 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 207 | filename = exportNameTxt->text(); |
|---|
| 208 | splitty = filename.split("."); |
|---|
| 209 | splitty.removeLast(); |
|---|
| 210 | filename = splitty.join( "." ); |
|---|
| 211 | filename += ".wav"; |
|---|
| 212 | exportNameTxt->setText(filename); |
|---|
| 213 | break; |
|---|
| 214 | case 3: |
|---|
| 215 | sampleRateCombo->show(); |
|---|
| 216 | sampleDepthCombo->show(); |
|---|
| 217 | sampleRateCombo->setCurrentIndex ( 0 ); //22050hz |
|---|
| 218 | sampleDepthCombo->setCurrentIndex ( 0 ); //8bit |
|---|
| 219 | filename = exportNameTxt->text(); |
|---|
| 220 | splitty = filename.split("."); |
|---|
| 221 | splitty.removeLast(); |
|---|
| 222 | filename = splitty.join( "." ); |
|---|
| 223 | filename += ".wav"; |
|---|
| 224 | exportNameTxt->setText(filename); |
|---|
| 225 | break; |
|---|
| 226 | case 4: |
|---|
| 227 | sampleRateCombo->show(); |
|---|
| 228 | sampleDepthCombo->show(); |
|---|
| 229 | sampleRateCombo->setCurrentIndex ( 3 ); //96000hz |
|---|
| 230 | sampleDepthCombo->setCurrentIndex ( 3 ); //32bit |
|---|
| 231 | filename = exportNameTxt->text(); |
|---|
| 232 | splitty = filename.split("."); |
|---|
| 233 | splitty.removeLast(); |
|---|
| 234 | filename = splitty.join( "." ); |
|---|
| 235 | filename += ".wav"; |
|---|
| 236 | exportNameTxt->setText(filename); |
|---|
| 237 | break; |
|---|
| 238 | case 5: |
|---|
| 239 | sampleRateCombo->show(); |
|---|
| 240 | sampleDepthCombo->show(); |
|---|
| 241 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 242 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 243 | filename = exportNameTxt->text(); |
|---|
| 244 | splitty = filename.split("."); |
|---|
| 245 | splitty.removeLast(); |
|---|
| 246 | filename = splitty.join( "." ); |
|---|
| 247 | filename += ".aiff"; |
|---|
| 248 | exportNameTxt->setText(filename); |
|---|
| 249 | break; |
|---|
| 250 | case 6: |
|---|
| 251 | sampleRateCombo->show(); |
|---|
| 252 | sampleDepthCombo->show(); |
|---|
| 253 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 254 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 255 | filename = exportNameTxt->text(); |
|---|
| 256 | splitty = filename.split("."); |
|---|
| 257 | splitty.removeLast(); |
|---|
| 258 | filename = splitty.join( "." ); |
|---|
| 259 | filename += ".aiff"; |
|---|
| 260 | exportNameTxt->setText(filename); |
|---|
| 261 | break; |
|---|
| 262 | case 7: |
|---|
| 263 | sampleRateCombo->show(); |
|---|
| 264 | sampleDepthCombo->show(); |
|---|
| 265 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 266 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 267 | filename = exportNameTxt->text(); |
|---|
| 268 | splitty = filename.split("."); |
|---|
| 269 | splitty.removeLast(); |
|---|
| 270 | filename = splitty.join( "." ); |
|---|
| 271 | filename += ".aiff"; |
|---|
| 272 | exportNameTxt->setText(filename); |
|---|
| 273 | break; |
|---|
| 274 | case 8: |
|---|
| 275 | sampleRateCombo->show(); |
|---|
| 276 | sampleDepthCombo->show(); |
|---|
| 277 | sampleRateCombo->setCurrentIndex ( 2 ); //48000hz |
|---|
| 278 | sampleDepthCombo->setCurrentIndex ( 2 ); //24bit |
|---|
| 279 | filename = exportNameTxt->text(); |
|---|
| 280 | splitty = filename.split("."); |
|---|
| 281 | splitty.removeLast(); |
|---|
| 282 | filename = splitty.join( "." ); |
|---|
| 283 | filename += ".flac"; |
|---|
| 284 | exportNameTxt->setText(filename); |
|---|
| 285 | break; |
|---|
| 286 | case 9: |
|---|
| 287 | sampleRateCombo->hide(); |
|---|
| 288 | sampleDepthCombo->hide(); |
|---|
| 289 | label->hide(); |
|---|
| 290 | label_2->hide(); |
|---|
| 291 | filename = exportNameTxt->text(); |
|---|
| 292 | splitty = filename.split("."); |
|---|
| 293 | splitty.removeLast(); |
|---|
| 294 | filename = splitty.join( "." ); |
|---|
| 295 | filename += ".ogg"; |
|---|
| 296 | exportNameTxt->setText(filename); |
|---|
| 297 | break; |
|---|
| 298 | |
|---|
| 299 | default: |
|---|
| 300 | sampleRateCombo->show(); |
|---|
| 301 | sampleDepthCombo->show(); |
|---|
| 302 | sampleRateCombo->setCurrentIndex ( 1 ); //44100hz |
|---|
| 303 | sampleDepthCombo->setCurrentIndex ( 1 ); //16bit |
|---|
| 304 | filename = exportNameTxt->text(); |
|---|
| 305 | splitty = filename.split("."); |
|---|
| 306 | splitty.removeLast(); |
|---|
| 307 | filename = splitty.join( "." ); |
|---|
| 308 | filename += ".wav"; |
|---|
| 309 | exportNameTxt->setText(filename); |
|---|
| 310 | ; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | void ExportSongDialog::on_exportNameTxt_textChanged( const QString& ) |
|---|
| 318 | { |
|---|
| 319 | QString filename = exportNameTxt->text(); |
|---|
| 320 | if ( ! filename.isEmpty() ) { |
|---|
| 321 | okBtn->setEnabled(true); |
|---|
| 322 | } |
|---|
| 323 | else { |
|---|
| 324 | okBtn->setEnabled(false); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | if( filename.endsWith( ".ogg" ) || filename.endsWith( ".OGG" ) ){ |
|---|
| 328 | templateCombo->setCurrentIndex( 9 );//ogg |
|---|
| 329 | } |
|---|
| 330 | else if( filename.endsWith( ".flac" ) || filename.endsWith( ".FLAC" ) ){ |
|---|
| 331 | label->show(); |
|---|
| 332 | label_2->show(); |
|---|
| 333 | templateCombo->setCurrentIndex( 8 );//flac |
|---|
| 334 | } |
|---|
| 335 | else if( filename.endsWith( ".aiff" ) || filename.endsWith( ".AIFF" ) ){ |
|---|
| 336 | label->show(); |
|---|
| 337 | label_2->show(); |
|---|
| 338 | templateCombo->setCurrentIndex( 5 );//aiff |
|---|
| 339 | } |
|---|
| 340 | else if( filename.endsWith( ".wav" ) || filename.endsWith( ".WAV" ) ){ |
|---|
| 341 | label->show(); |
|---|
| 342 | label_2->show(); |
|---|
| 343 | templateCombo->setCurrentIndex( 0 );//wav |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | void ExportSongDialog::progressEvent( int nValue ) |
|---|
| 349 | { |
|---|
| 350 | m_pProgressBar->setValue( nValue ); |
|---|
| 351 | if ( nValue == 100 ) { |
|---|
| 352 | m_bExporting = false; |
|---|
| 353 | QFile check( exportNameTxt->text() ); |
|---|
| 354 | if ( ! check.exists() ) { |
|---|
| 355 | QMessageBox::information( this, "Hydrogen", trUtf8("Export failed!") ); |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | } |
|---|