| 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 | connect( tree, SIGNAL( clicked( const QModelIndex&) ), SLOT( clicked( const QModelIndex& ) ) ); |
|---|
| 76 | connect( pathLineEdit, SIGNAL( returnPressed() ), SLOT( updateModelIndex() ) ); |
|---|
| 77 | |
|---|
| 78 | // QTimer *timer = new QTimer( this ); |
|---|
| 79 | // connect(timer, SIGNAL(timeout() ), this, SLOT( updateModelIndex() ) ); |
|---|
| 80 | // timer->start( 1000 ); |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | AudioFileBrowser::~AudioFileBrowser() |
|---|
| 87 | { |
|---|
| 88 | INFOLOG ( "DESTROY" ); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | void AudioFileBrowser::updateModelIndex() |
|---|
| 93 | { |
|---|
| 94 | tree->setRootIndex( model->index( pathLineEdit->text() ) ); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void AudioFileBrowser::clicked( const QModelIndex& index ) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | QString path = model->filePath( index ); |
|---|
| 101 | pathLineEdit->setText( path ); |
|---|
| 102 | filelineedit->setText( path ); |
|---|
| 103 | m_pSampleWaveDisplay->updateDisplay( NULL ); |
|---|
| 104 | |
|---|
| 105 | updateModelIndex(); //with this you have a navigation like konqueror |
|---|
| 106 | |
|---|
| 107 | if ( model->isDir( index ) ) |
|---|
| 108 | return; |
|---|
| 109 | |
|---|
| 110 | QString name = path.section( '/', -1 ); |
|---|
| 111 | |
|---|
| 112 | QString path2 = path; |
|---|
| 113 | QString onlypath = path; |
|---|
| 114 | if ( name != "" ){ |
|---|
| 115 | onlypath = path.replace( name, "" ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | name = name.left( '.' ); |
|---|
| 119 | |
|---|
| 120 | QString message = "Name: " + name; |
|---|
| 121 | filelineedit->setText( path2 ); |
|---|
| 122 | pathLineEdit->setText( onlypath ); |
|---|
| 123 | |
|---|
| 124 | updateModelIndex(); //with this you have a navigation like konqueror |
|---|
| 125 | |
|---|
| 126 | if ( |
|---|
| 127 | ( path2.endsWith( ".wav" ) ) || |
|---|
| 128 | ( path2.endsWith( ".WAV" ) ) || |
|---|
| 129 | ( path2.endsWith( ".au" ) ) || |
|---|
| 130 | ( path2.endsWith( ".AU" ) ) || |
|---|
| 131 | ( path2.endsWith( ".aiff" ) ) || |
|---|
| 132 | ( path2.endsWith( ".AIFF" ) ) || |
|---|
| 133 | ( path2.endsWith( ".flac" ) ) || |
|---|
| 134 | ( path2.endsWith( ".FLAC" ) ) |
|---|
| 135 | ) { |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | Sample *pNewSample = Sample::load( path2 ); |
|---|
| 139 | if ( pNewSample ) { |
|---|
| 140 | m_pNBytesLable->setText( trUtf8( "Size: %1 bytes" ).arg( pNewSample->get_size() / 2 ) ); |
|---|
| 141 | m_pSamplerateLable->setText( trUtf8( "Samplerate: %1" ).arg( pNewSample->get_sample_rate() ) ); |
|---|
| 142 | float sec = ( float )( pNewSample->get_n_frames() / (float)pNewSample->get_sample_rate() ); |
|---|
| 143 | QString qsec = ""; |
|---|
| 144 | qsec.sprintf( "%2.2f", sec ); |
|---|
| 145 | m_pLengthLable->setText( trUtf8( "Samplelength: " ) + qsec + trUtf8( " s" ) ); |
|---|
| 146 | |
|---|
| 147 | //second part of check prevent left sample objects into memory |
|---|
| 148 | if (playSamplescheckBox->isChecked() && AudioEngine::get_instance()->get_sampler()->get_playing_notes_number() == 0 ){ |
|---|
| 149 | AudioEngine::get_instance()->get_sampler()->preview_sample(pNewSample); |
|---|
| 150 | }else{ |
|---|
| 151 | delete pNewSample; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | m_pSampleWaveDisplay->updateDisplay( path2 ); |
|---|
| 155 | m_pPlayBtn->setEnabled( true ); |
|---|
| 156 | openBTN->setEnabled( true ); |
|---|
| 157 | m_psamplefilename = path2; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | m_pNameLabel->setText( message ); |
|---|
| 161 | }else{ |
|---|
| 162 | m_pNameLabel->setText( trUtf8( "Name:")); |
|---|
| 163 | m_pNBytesLable->setText( trUtf8( "Size:" ) ); |
|---|
| 164 | m_pSamplerateLable->setText( trUtf8( "Samplerate:" ) ); |
|---|
| 165 | m_pLengthLable->setText( trUtf8( "Samplelength:" ) ); |
|---|
| 166 | m_pSampleWaveDisplay->updateDisplay( NULL ); |
|---|
| 167 | m_pPlayBtn->setEnabled( false ); |
|---|
| 168 | openBTN->setEnabled( false ); |
|---|
| 169 | m_psamplefilename = ""; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | void AudioFileBrowser::on_m_pPlayBtn_clicked() |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | if( QFile( m_psamplefilename ).exists() == false ) |
|---|
| 179 | return; |
|---|
| 180 | Sample *pNewSample = Sample::load( m_psamplefilename ); |
|---|
| 181 | if ( pNewSample ){ |
|---|
| 182 | AudioEngine::get_instance()->get_sampler()->preview_sample( pNewSample ); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | void AudioFileBrowser::on_cancelBTN_clicked() |
|---|
| 189 | { |
|---|
| 190 | Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text(); |
|---|
| 191 | m_pselectedFile = ""; |
|---|
| 192 | reject(); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | void AudioFileBrowser::on_openBTN_clicked() |
|---|
| 198 | { |
|---|
| 199 | if ( |
|---|
| 200 | ( ( filelineedit->text().endsWith( ".wav" ) ) || |
|---|
| 201 | ( filelineedit->text().endsWith( ".WAV" ) ) || |
|---|
| 202 | ( filelineedit->text().endsWith( ".au" ) ) || |
|---|
| 203 | ( filelineedit->text().endsWith( ".AU" ) ) || |
|---|
| 204 | ( filelineedit->text().endsWith( ".aiff" ) ) || |
|---|
| 205 | ( filelineedit->text().endsWith( ".AIFF" ) ) || |
|---|
| 206 | ( filelineedit->text().endsWith( ".flac" ) ) || |
|---|
| 207 | ( filelineedit->text().endsWith( ".FLAC" ) ) ) && |
|---|
| 208 | ( QFile( filelineedit->text() ).exists() == true ) |
|---|
| 209 | ) { |
|---|
| 210 | m_pselectedFile = filelineedit->text(); |
|---|
| 211 | |
|---|
| 212 | }else |
|---|
| 213 | { |
|---|
| 214 | m_pselectedFile = ""; |
|---|
| 215 | } |
|---|
| 216 | Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text(); |
|---|
| 217 | accept(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | QString AudioFileBrowser::selectedFile() |
|---|
| 223 | { |
|---|
| 224 | return m_pselectedFile; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | void AudioFileBrowser::on_m_pPathHometoolButton_clicked() |
|---|
| 230 | { |
|---|
| 231 | pathLineEdit->setText( QDir::homePath() ); |
|---|
| 232 | tree->setRootIndex( model->index( QDir::homePath() ) ); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | void AudioFileBrowser::on_m_pPathUptoolButton_clicked() |
|---|
| 238 | { |
|---|
| 239 | QString toremove = ""; |
|---|
| 240 | QString path = pathLineEdit->text(); |
|---|
| 241 | |
|---|
| 242 | if ( path.length() <=1 ) |
|---|
| 243 | return; |
|---|
| 244 | |
|---|
| 245 | if ( path.endsWith( '/') ) { |
|---|
| 246 | toremove = path.section( '/', -2 ); |
|---|
| 247 | }else |
|---|
| 248 | { |
|---|
| 249 | toremove = path.section( '/', -1 ); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | QString updir = path.replace( toremove, "" ); |
|---|
| 253 | pathLineEdit->setText( updir ); |
|---|
| 254 | tree->setRootIndex( model->index( updir ) ); |
|---|
| 255 | } |
|---|