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