| 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 | |
|---|
| 24 | #include "AudioFileBrowser.h" |
|---|
| 25 | #include "../HydrogenApp.h" |
|---|
| 26 | #include "InstrumentEditor/InstrumentEditor.h" |
|---|
| 27 | #include "SampleWaveDisplay.h" |
|---|
| 28 | #include "../widgets/Button.h" |
|---|
| 29 | |
|---|
| 30 | #include <hydrogen/data_path.h> |
|---|
| 31 | #include <hydrogen/h2_exception.h> |
|---|
| 32 | #include <hydrogen/Preferences.h> |
|---|
| 33 | #include <hydrogen/sample.h> |
|---|
| 34 | #include <hydrogen/audio_engine.h> |
|---|
| 35 | |
|---|
| 36 | #include <QTreeWidget> |
|---|
| 37 | #include <QMessageBox> |
|---|
| 38 | |
|---|
| 39 | using namespace H2Core; |
|---|
| 40 | using namespace std; |
|---|
| 41 | |
|---|
| 42 | AudioFileBrowser::AudioFileBrowser ( QWidget* pParent ) |
|---|
| 43 | : QDialog ( pParent ) |
|---|
| 44 | , Object ( "AudioFileBrowser" ) |
|---|
| 45 | { |
|---|
| 46 | |
|---|
| 47 | setupUi ( this ); |
|---|
| 48 | INFOLOG ( "INIT" ); |
|---|
| 49 | setWindowTitle ( trUtf8 ( "Audio File Browser" ) ); |
|---|
| 50 | setFixedSize ( width(), height() ); |
|---|
| 51 | installEventFilter( this ); |
|---|
| 52 | |
|---|
| 53 | filters << "*.wav" << "*.WAV" << "*.flac"<< "*.FLAC" << "*.aiff" << "*.AIFF"<< "*.au" << "*.AU" ; |
|---|
| 54 | |
|---|
| 55 | model = new QDirModel(); |
|---|
| 56 | // model->setNameFilters(filters); |
|---|
| 57 | model->setFilter( QDir::AllEntries | QDir::NoDotAndDotDot ); |
|---|
| 58 | model->setSorting( QDir::DirsFirst |QDir::Name ); |
|---|
| 59 | __index = model->index( QDir::currentPath() ); |
|---|
| 60 | |
|---|
| 61 | m_pPlayBtn->setEnabled( false ); |
|---|
| 62 | openBTN->setEnabled( false ); |
|---|
| 63 | |
|---|
| 64 | tree = new QTreeView( treeView ); |
|---|
| 65 | tree->setModel( model ); |
|---|
| 66 | tree->resize( 799, 310 ); |
|---|
| 67 | tree->header()->resizeSection( 0, 405 ); |
|---|
| 68 | tree->setAlternatingRowColors( true ); |
|---|
| 69 | tree->setRootIndex( model->index( Preferences::getInstance()->__lastsampleDirectory ) ); |
|---|
| 70 | |
|---|
| 71 | pathLineEdit->setText( Preferences::getInstance()->__lastsampleDirectory ); |
|---|
| 72 | m_psamplefilename = ""; |
|---|
| 73 | m_pselectedFile = ""; |
|---|
| 74 | |
|---|
| 75 | sEmptySampleFilename = DataPath::get_data_path() + "/emptySample.wav"; |
|---|
| 76 | |
|---|
| 77 | m_pSampleWaveDisplay = new SampleWaveDisplay( waveformview ); |
|---|
| 78 | m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename ); |
|---|
| 79 | m_pSampleWaveDisplay->move( 3, 3 ); |
|---|
| 80 | |
|---|
| 81 | playSamplescheckBox->setChecked( Preferences::getInstance()->__playsamplesonclicking ); |
|---|
| 82 | |
|---|
| 83 | connect( tree, SIGNAL( clicked( const QModelIndex&) ), SLOT( clicked( const QModelIndex& ) ) ); |
|---|
| 84 | connect( pathLineEdit, SIGNAL( returnPressed() ), SLOT( updateModelIndex() ) ); |
|---|
| 85 | |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | AudioFileBrowser::~AudioFileBrowser() |
|---|
| 91 | { |
|---|
| 92 | INFOLOG ( "DESTROY" ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | void AudioFileBrowser::updateModelIndex() |
|---|
| 97 | { |
|---|
| 98 | QString toremove = ""; |
|---|
| 99 | QString newpath = pathLineEdit->text(); |
|---|
| 100 | |
|---|
| 101 | if( QDir( newpath ).exists() ){ |
|---|
| 102 | tree->setRootIndex( model->index( newpath ) ); |
|---|
| 103 | }else |
|---|
| 104 | { |
|---|
| 105 | toremove = newpath.section( '/', -1 ); |
|---|
| 106 | // QMessageBox::information ( this, "Hydrogen", newpath + toremove); |
|---|
| 107 | newpath.replace( toremove, "" ); |
|---|
| 108 | tree->setRootIndex( model->index( newpath ) ); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void AudioFileBrowser::clicked( const QModelIndex& index ) |
|---|
| 114 | { |
|---|
| 115 | |
|---|
| 116 | QString path = model->filePath( index ); |
|---|
| 117 | pathLineEdit->setText( path ); |
|---|
| 118 | filelineedit->setText( path ); |
|---|
| 119 | m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename ); |
|---|
| 120 | |
|---|
| 121 | updateModelIndex(); //with this you have a navigation like konqueror |
|---|
| 122 | |
|---|
| 123 | if ( model->isDir( index ) ) |
|---|
| 124 | return; |
|---|
| 125 | |
|---|
| 126 | QString name = path.section( '/', -1 ); |
|---|
| 127 | |
|---|
| 128 | QString path2 = path; |
|---|
| 129 | QString onlypath = path; |
|---|
| 130 | if ( name != "" ){ |
|---|
| 131 | onlypath = path.replace( name, "" ); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | name = name.left( '.' ); |
|---|
| 135 | |
|---|
| 136 | QString message = "Name: " + name; |
|---|
| 137 | filelineedit->setText( path2 ); |
|---|
| 138 | pathLineEdit->setText( onlypath ); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | if ( |
|---|
| 142 | ( path2.endsWith( ".wav" ) ) || |
|---|
| 143 | ( path2.endsWith( ".WAV" ) ) || |
|---|
| 144 | ( path2.endsWith( ".au" ) ) || |
|---|
| 145 | ( path2.endsWith( ".AU" ) ) || |
|---|
| 146 | ( path2.endsWith( ".aiff" ) ) || |
|---|
| 147 | ( path2.endsWith( ".AIFF" ) ) || |
|---|
| 148 | ( path2.endsWith( ".flac" ) ) || |
|---|
| 149 | ( path2.endsWith( ".FLAC" ) ) |
|---|
| 150 | ) { |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | Sample *pNewSample = Sample::load( path2 ); |
|---|
| 154 | if ( pNewSample ) { |
|---|
| 155 | m_pNBytesLable->setText( trUtf8( "Size: %1 bytes" ).arg( pNewSample->get_size() / 2 ) ); |
|---|
| 156 | m_pSamplerateLable->setText( trUtf8( "Samplerate: %1" ).arg( pNewSample->get_sample_rate() ) ); |
|---|
| 157 | float sec = ( float )( pNewSample->get_n_frames() / (float)pNewSample->get_sample_rate() ); |
|---|
| 158 | QString qsec = ""; |
|---|
| 159 | qsec.sprintf( "%2.2f", sec ); |
|---|
| 160 | m_pLengthLable->setText( trUtf8( "Samplelength: " ) + qsec + trUtf8( " s" ) ); |
|---|
| 161 | |
|---|
| 162 | delete pNewSample; |
|---|
| 163 | m_psamplefilename = path2; |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | if (playSamplescheckBox->isChecked()){ |
|---|
| 167 | if ( sec <= 600.00){ |
|---|
| 168 | on_m_pPlayBtn_clicked(); |
|---|
| 169 | }else |
|---|
| 170 | { |
|---|
| 171 | QMessageBox::information ( this, "Hydrogen", trUtf8( "No clicking audio preview for samples longer than 10 minutes!" ) ); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | m_pSampleWaveDisplay->updateDisplay( path2 ); |
|---|
| 176 | m_pPlayBtn->setEnabled( true ); |
|---|
| 177 | openBTN->setEnabled( true ); |
|---|
| 178 | |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | m_pNameLabel->setText( message ); |
|---|
| 182 | }else{ |
|---|
| 183 | m_pNameLabel->setText( trUtf8( "Name:")); |
|---|
| 184 | m_pNBytesLable->setText( trUtf8( "Size:" ) ); |
|---|
| 185 | m_pSamplerateLable->setText( trUtf8( "Samplerate:" ) ); |
|---|
| 186 | m_pLengthLable->setText( trUtf8( "Samplelength:" ) ); |
|---|
| 187 | m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename ); |
|---|
| 188 | m_pPlayBtn->setEnabled( false ); |
|---|
| 189 | openBTN->setEnabled( false ); |
|---|
| 190 | m_psamplefilename = ""; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | void AudioFileBrowser::on_m_pPlayBtn_clicked() |
|---|
| 197 | { |
|---|
| 198 | |
|---|
| 199 | if( QFile( m_psamplefilename ).exists() == false ) |
|---|
| 200 | return; |
|---|
| 201 | Sample *pNewSample = Sample::load( m_psamplefilename ); |
|---|
| 202 | if ( pNewSample ){ |
|---|
| 203 | int length = ( ( pNewSample->get_n_frames() / pNewSample->get_sample_rate() + 1) * 100 ); |
|---|
| 204 | AudioEngine::get_instance()->get_sampler()->preview_sample( pNewSample, length ); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | void AudioFileBrowser::on_cancelBTN_clicked() |
|---|
| 212 | { |
|---|
| 213 | Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text(); |
|---|
| 214 | m_pselectedFile = ""; |
|---|
| 215 | reject(); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | void AudioFileBrowser::on_openBTN_clicked() |
|---|
| 221 | { |
|---|
| 222 | if ( |
|---|
| 223 | ( ( filelineedit->text().endsWith( ".wav" ) ) || |
|---|
| 224 | ( filelineedit->text().endsWith( ".WAV" ) ) || |
|---|
| 225 | ( filelineedit->text().endsWith( ".au" ) ) || |
|---|
| 226 | ( filelineedit->text().endsWith( ".AU" ) ) || |
|---|
| 227 | ( filelineedit->text().endsWith( ".aiff" ) ) || |
|---|
| 228 | ( filelineedit->text().endsWith( ".AIFF" ) ) || |
|---|
| 229 | ( filelineedit->text().endsWith( ".flac" ) ) || |
|---|
| 230 | ( filelineedit->text().endsWith( ".FLAC" ) ) ) && |
|---|
| 231 | ( QFile( filelineedit->text() ).exists() == true ) |
|---|
| 232 | ) { |
|---|
| 233 | m_pselectedFile = filelineedit->text(); |
|---|
| 234 | |
|---|
| 235 | }else |
|---|
| 236 | { |
|---|
| 237 | m_pselectedFile = ""; |
|---|
| 238 | } |
|---|
| 239 | Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text(); |
|---|
| 240 | accept(); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | void AudioFileBrowser::on_playSamplescheckBox_clicked() |
|---|
| 246 | { |
|---|
| 247 | Preferences::getInstance()->__playsamplesonclicking = playSamplescheckBox->isChecked(); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | QString AudioFileBrowser::selectedFile() |
|---|
| 253 | { |
|---|
| 254 | if ( useNameCheckBox->isChecked() ){ |
|---|
| 255 | m_pselectedFile.append( "___trUe" ); |
|---|
| 256 | } |
|---|
| 257 | return m_pselectedFile; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | void AudioFileBrowser::on_m_pPathHometoolButton_clicked() |
|---|
| 263 | { |
|---|
| 264 | pathLineEdit->setText( QDir::homePath() ); |
|---|
| 265 | tree->setRootIndex( model->index( QDir::homePath() ) ); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | void AudioFileBrowser::on_m_pPathUptoolButton_clicked() |
|---|
| 271 | { |
|---|
| 272 | QString toremove = ""; |
|---|
| 273 | QString path = pathLineEdit->text(); |
|---|
| 274 | |
|---|
| 275 | if ( path.length() <=1 ) |
|---|
| 276 | return; |
|---|
| 277 | |
|---|
| 278 | if ( path.endsWith( '/') ) { |
|---|
| 279 | toremove = path.section( '/', -2 ); |
|---|
| 280 | }else |
|---|
| 281 | { |
|---|
| 282 | toremove = path.section( '/', -1 ); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | QString updir = path.replace( toremove, "" ); |
|---|
| 286 | pathLineEdit->setText( updir ); |
|---|
| 287 | tree->setRootIndex( model->index( updir ) ); |
|---|
| 288 | tree->collapse( model->index( updir ) ); |
|---|
| 289 | } |
|---|