| 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 | |
|---|
| 28 | #include <hydrogen/LocalFileMng.h> |
|---|
| 29 | #include <hydrogen/h2_exception.h> |
|---|
| 30 | #include <hydrogen/Preferences.h> |
|---|
| 31 | #include <hydrogen/hydrogen.h> |
|---|
| 32 | #include <hydrogen/sample.h> |
|---|
| 33 | //#include <hydrogen/sampler/Sampler.h> |
|---|
| 34 | #include <hydrogen/audio_engine.h> |
|---|
| 35 | |
|---|
| 36 | #include "../widgets/Button.h" |
|---|
| 37 | |
|---|
| 38 | #include <QTreeWidget> |
|---|
| 39 | #include <QDomDocument> |
|---|
| 40 | #include <QMessageBox> |
|---|
| 41 | #include <QHeaderView> |
|---|
| 42 | #include <QFileDialog> |
|---|
| 43 | #include <vector> |
|---|
| 44 | #include <cstdlib> |
|---|
| 45 | #include <iostream> |
|---|
| 46 | #include <fstream> |
|---|
| 47 | |
|---|
| 48 | using namespace H2Core; |
|---|
| 49 | using namespace std; |
|---|
| 50 | |
|---|
| 51 | AudioFileBrowser::AudioFileBrowser ( QWidget* pParent ) |
|---|
| 52 | : QDialog ( pParent ) |
|---|
| 53 | , Object ( "AudioFileBrowser" ) |
|---|
| 54 | { |
|---|
| 55 | |
|---|
| 56 | setupUi ( this ); |
|---|
| 57 | INFOLOG ( "INIT" ); |
|---|
| 58 | setWindowTitle ( trUtf8 ( "Audio File Browser" ) ); |
|---|
| 59 | setFixedSize ( width(), height() ); |
|---|
| 60 | installEventFilter(this); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | model = new QDirModel(); |
|---|
| 64 | model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot ); |
|---|
| 65 | model->setSorting(QDir::DirsFirst |QDir::Name); |
|---|
| 66 | QModelIndex index = model->index(QDir::currentPath()); |
|---|
| 67 | |
|---|
| 68 | m_pPlayBtn->setEnabled( false ); |
|---|
| 69 | openBTN->setEnabled( false ); |
|---|
| 70 | |
|---|
| 71 | // namelabel->setText(model->headerData()); |
|---|
| 72 | QTreeView *tree = new QTreeView(treeView); |
|---|
| 73 | tree->setModel(model); |
|---|
| 74 | tree->resize(799, 310); |
|---|
| 75 | tree->header()->resizeSection ( 0, 405 ); |
|---|
| 76 | tree->setRootIndex(model->index(QDir::homePath())); |
|---|
| 77 | |
|---|
| 78 | m_psamplefilename = ""; |
|---|
| 79 | m_pselectedFile = ""; |
|---|
| 80 | |
|---|
| 81 | connect(tree, SIGNAL(clicked(const QModelIndex&)), SLOT(clicked(const QModelIndex&))); |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | AudioFileBrowser::~AudioFileBrowser() |
|---|
| 88 | { |
|---|
| 89 | INFOLOG ( "DESTROY" ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | void AudioFileBrowser::clicked(const QModelIndex& index) |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | QString path = model->filePath(index); |
|---|
| 98 | filelineedit->setText(path); |
|---|
| 99 | if ( model->isDir(index)) |
|---|
| 100 | return; |
|---|
| 101 | |
|---|
| 102 | QString name = path.section( '/', -1 ); |
|---|
| 103 | name = name.left('.'); |
|---|
| 104 | |
|---|
| 105 | QString message = "Name: " + name; |
|---|
| 106 | filelineedit->setText(path); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | if ( |
|---|
| 110 | ( path.endsWith( ".wav" ) ) || |
|---|
| 111 | ( path.endsWith( ".WAV" ) ) || |
|---|
| 112 | ( path.endsWith( ".au" ) ) || |
|---|
| 113 | ( path.endsWith( ".AU" ) ) || |
|---|
| 114 | ( path.endsWith( ".aiff" ) ) || |
|---|
| 115 | ( path.endsWith( ".AIFF" ) ) || |
|---|
| 116 | ( path.endsWith( ".flac" ) ) || |
|---|
| 117 | ( path.endsWith( ".FLAC" ) ) |
|---|
| 118 | ) { |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | Sample *pNewSample = Sample::load( path ); |
|---|
| 122 | if (pNewSample) { |
|---|
| 123 | m_pNBytesLable->setText( trUtf8( "Size: %1 bytes" ).arg( pNewSample->get_size() ) ); |
|---|
| 124 | m_pSamplerateLable->setText( trUtf8( "Samplerate: %1" ).arg( pNewSample->get_sample_rate() ) ); |
|---|
| 125 | m_pLengthLable->setText( trUtf8( "Samplelength: %1 s" ).arg( pNewSample->get_n_frames() / pNewSample->get_sample_rate() ) ); |
|---|
| 126 | |
|---|
| 127 | //second part of check prevent left sample objects into memory |
|---|
| 128 | if (playSamplescheckBox->isChecked() && AudioEngine::get_instance()->get_sampler()->get_playing_notes_number() == 0 ){ |
|---|
| 129 | AudioEngine::get_instance()->get_sampler()->preview_sample(pNewSample); |
|---|
| 130 | }else{ |
|---|
| 131 | delete pNewSample; |
|---|
| 132 | } |
|---|
| 133 | m_pPlayBtn->setEnabled( true ); |
|---|
| 134 | openBTN->setEnabled( true ); |
|---|
| 135 | m_psamplefilename = path; |
|---|
| 136 | } |
|---|
| 137 | m_pNameLabel->setText(message); |
|---|
| 138 | }else{ |
|---|
| 139 | m_pNameLabel->setText( trUtf8( "Name:")); |
|---|
| 140 | m_pNBytesLable->setText( trUtf8( "Size:" ) ); |
|---|
| 141 | m_pSamplerateLable->setText( trUtf8( "Samplerate:" ) ); |
|---|
| 142 | m_pLengthLable->setText( trUtf8( "Samplelength:" ) ); |
|---|
| 143 | m_pPlayBtn->setEnabled( false ); |
|---|
| 144 | openBTN->setEnabled( false ); |
|---|
| 145 | m_psamplefilename = ""; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | void AudioFileBrowser::on_m_pPlayBtn_clicked() |
|---|
| 152 | { |
|---|
| 153 | |
|---|
| 154 | if( QFile( m_psamplefilename ).exists() == false ) |
|---|
| 155 | return; |
|---|
| 156 | Sample *pNewSample = Sample::load( m_psamplefilename ); |
|---|
| 157 | if ( pNewSample ){ |
|---|
| 158 | AudioEngine::get_instance()->get_sampler()->preview_sample(pNewSample); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | void AudioFileBrowser::on_cancelBTN_clicked() |
|---|
| 165 | { |
|---|
| 166 | m_pselectedFile = ""; |
|---|
| 167 | reject(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | void AudioFileBrowser::on_openBTN_clicked() |
|---|
| 173 | { |
|---|
| 174 | if ( |
|---|
| 175 | ( ( filelineedit->text().endsWith( ".wav" ) ) || |
|---|
| 176 | ( filelineedit->text().endsWith( ".WAV" ) ) || |
|---|
| 177 | ( filelineedit->text().endsWith( ".au" ) ) || |
|---|
| 178 | ( filelineedit->text().endsWith( ".AU" ) ) || |
|---|
| 179 | ( filelineedit->text().endsWith( ".aiff" ) ) || |
|---|
| 180 | ( filelineedit->text().endsWith( ".AIFF" ) ) || |
|---|
| 181 | ( filelineedit->text().endsWith( ".flac" ) ) || |
|---|
| 182 | ( filelineedit->text().endsWith( ".FLAC" ) ) ) && |
|---|
| 183 | ( QFile( filelineedit->text() ).exists() == true ) |
|---|
| 184 | ) { |
|---|
| 185 | m_pselectedFile = filelineedit->text(); |
|---|
| 186 | |
|---|
| 187 | }else |
|---|
| 188 | { |
|---|
| 189 | m_pselectedFile = ""; |
|---|
| 190 | } |
|---|
| 191 | accept(); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | QString AudioFileBrowser::selectedFile() |
|---|
| 197 | { |
|---|
| 198 | return m_pselectedFile; |
|---|
| 199 | } |
|---|