WIP miniaudio working, some sigsev while playing...
This commit is contained in:
parent
78695b7976
commit
7aea8f6cf1
23 changed files with 469 additions and 299 deletions
|
|
@ -1,31 +1,20 @@
|
|||
#include "audiolayerwidget.h"
|
||||
|
||||
#include<iostream>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QVBoxLayout>
|
||||
#include <QFile>
|
||||
#include <QTime>
|
||||
|
||||
AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
|
||||
QGroupBox(parent)
|
||||
, m_suspendResumeButton(0)
|
||||
, m_watchDMX(new QTimer(this))
|
||||
, m_currentMedia(" ")
|
||||
, m_running(false)
|
||||
, m_refreshGUI(new QTimer(this))
|
||||
, m_currentMedia("")
|
||||
, m_mediaLoaded(false)
|
||||
{
|
||||
|
||||
this->setTitle(name);
|
||||
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
|
||||
QHBoxLayout *status = new QHBoxLayout;
|
||||
m_statusLabel = new QLabel;
|
||||
m_statusLabel->setText(STATUS_LABEL);
|
||||
m_statusValue = new QLabel;
|
||||
// m_receiveDMX = new QCheckBox("Receiving DMX", this);
|
||||
// m_receiveDMX->setChecked(false);
|
||||
// status->addWidget(m_receiveDMX);
|
||||
status->addWidget(m_statusLabel);
|
||||
status->addWidget(m_statusValue);
|
||||
m_loopCheck = new QCheckBox();
|
||||
|
|
@ -58,8 +47,8 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
|
|||
m_volumeLabel = new QLabel;
|
||||
m_volumeLabel->setText(tr(VOLUME_LABEL));
|
||||
m_volumeSlider = new QSlider(Qt::Horizontal);
|
||||
m_volumeSlider->setMinimum(0);
|
||||
m_volumeSlider->setMaximum(90);
|
||||
m_volumeSlider->setMinimum(-100);
|
||||
m_volumeSlider->setMaximum(100);
|
||||
m_volumeSlider->setSingleStep(1);
|
||||
m_volumeIndicator = new QLabel;
|
||||
volumeBox->addWidget(m_volumeLabel, 0, 0);
|
||||
|
|
@ -111,13 +100,8 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
|
|||
progressTime->addWidget(m_totalTimeValue);
|
||||
layout->addLayout(progressTime);
|
||||
|
||||
QHBoxLayout *progressSlider = new QHBoxLayout;
|
||||
m_progressLabel = new QLabel;
|
||||
m_progressLabel->setText(PROGRESS_LABEL);
|
||||
m_progressSlider = new QSlider(Qt::Horizontal);
|
||||
progressSlider->addWidget(m_progressLabel);
|
||||
progressSlider->addWidget(m_progressSlider);
|
||||
layout->addLayout(progressSlider);
|
||||
layout->addWidget(m_progressSlider);
|
||||
|
||||
m_suspendResumeButton = new QPushButton(this);
|
||||
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
|
||||
|
|
@ -125,12 +109,8 @@ AudioLayerWidget::AudioLayerWidget(QWidget *parent, QString name):
|
|||
layout->addWidget(m_suspendResumeButton);
|
||||
|
||||
this->setLayout(layout);
|
||||
|
||||
connect(m_watchDMX, SIGNAL(timeout()),
|
||||
this, SLOT(refreshGUI()));
|
||||
m_watchDMX->start(100);
|
||||
|
||||
m_music.setAttenuation(0);
|
||||
connect(m_refreshGUI, SIGNAL(timeout()), this, SLOT(refreshGUI()));
|
||||
m_refreshGUI->start(100);
|
||||
}
|
||||
|
||||
AudioLayerWidget::~AudioLayerWidget()
|
||||
|
|
@ -140,36 +120,42 @@ AudioLayerWidget::~AudioLayerWidget()
|
|||
|
||||
void AudioLayerWidget::volumeChanged(int value)
|
||||
{
|
||||
m_music.setVolume(value);
|
||||
float result;
|
||||
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
result = ma_volume_linear_to_db(value);
|
||||
ma_sound_group_set_volume(¤tSound, result);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::panChanged(int value)
|
||||
{
|
||||
m_music.setRelativeToListener(true);
|
||||
sf::Vector3f pos = m_music.getPosition();
|
||||
//m_music.setSpati(0, 0, 0);
|
||||
qreal pan = (value - 128) / 64.0f;
|
||||
qWarning("change pan %f", pan);
|
||||
//m_music.setPosition(pan, 0.0, sqrtf(1.0 + pan*pan));
|
||||
m_music.setPosition(pan, 0.0f, -1.0f);
|
||||
//pos = m_music.getPosition();
|
||||
qWarning("%f %f %f", pos.x, pos.y, pos.z);
|
||||
qWarning("is rel %i", m_music.isRelativeToListener());
|
||||
float result;
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
result = (value / 128.0) - 128;
|
||||
ma_sound_group_set_pan(¤tSound, result);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::pitchChanged(int value)
|
||||
{
|
||||
m_music.setPitch(qreal(value / 128.0 ));
|
||||
float result;
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
result = (value / 128.0) - 128;
|
||||
ma_sound_group_set_pitch(¤tSound, result);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::loopChanged(int value)
|
||||
{
|
||||
m_music.setLoop(value);
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
ma_sound_set_looping(¤tSound, value);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::setVol(qreal vol)
|
||||
{
|
||||
m_music.setVolume(vol);
|
||||
this->volumeChanged(vol);
|
||||
m_volumeSlider->blockSignals(true);
|
||||
m_volumeSlider->setValue(vol);
|
||||
m_volumeIndicator->setText(QString::number(vol));
|
||||
|
|
@ -186,7 +172,7 @@ void AudioLayerWidget::setPan(qreal pan)
|
|||
|
||||
void AudioLayerWidget::setPitch(qreal pitch)
|
||||
{
|
||||
m_music.setPitch(float(pitch / 128 ));
|
||||
this->pitchChanged(pitch);
|
||||
m_pitchSlider->blockSignals(true);
|
||||
m_pitchSlider->setValue(pitch);
|
||||
m_pitchSlider->blockSignals(false);
|
||||
|
|
@ -194,6 +180,12 @@ void AudioLayerWidget::setPitch(qreal pitch)
|
|||
|
||||
void AudioLayerWidget::loadMedia(QString file)
|
||||
{
|
||||
ma_result result;
|
||||
ma_format *format = 0;
|
||||
ma_uint32 *channels = 0;
|
||||
ma_uint32 *sampleRate = 0;
|
||||
|
||||
|
||||
if (m_currentMedia.compare(file) == 0 ) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -201,21 +193,46 @@ void AudioLayerWidget::loadMedia(QString file)
|
|||
qWarning("Can not access to file %s", file.toLatin1().constData());
|
||||
return;
|
||||
}
|
||||
// Load an ogg music file
|
||||
if (!m_music.openFromFile(file.toStdString())) {
|
||||
qWarning("Can not open file %s", file.toLatin1().constData());
|
||||
ma_engine engine = AudioWidget::getInstance()->getEngine();
|
||||
if (currentSound.ownsDataSource == true)
|
||||
{
|
||||
ma_sound_uninit(¤tSound);
|
||||
}
|
||||
result = ma_sound_init_from_file(&engine, file.toLatin1(), MA_SOUND_FLAG_NO_SPATIALIZATION | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM, NULL, NULL, ¤tSound);
|
||||
if (result != MA_SUCCESS) {
|
||||
qWarning("WARNING: Failed to load sound %s", file.toLatin1().constData());
|
||||
return;
|
||||
}
|
||||
|
||||
m_currentMedia = file;
|
||||
durationChanged(m_music.getDuration().asMilliseconds());
|
||||
float pLength = this->getDuration();
|
||||
result = ma_sound_get_data_format(¤tSound, format, channels, sampleRate, NULL, 0);
|
||||
if (result != MA_SUCCESS) {
|
||||
qWarning("WARNING: Failed to get data format %s", file.toLatin1().constData());
|
||||
return;
|
||||
}
|
||||
m_mediaLoaded = true;
|
||||
durationChanged(pLength * 1000);
|
||||
fileLoaded(file);
|
||||
|
||||
// Display music informations
|
||||
std::cout << "File loaded: " << file.toLatin1().constData() << " : " << std::endl;
|
||||
std::cout << " " << m_music.getDuration().asSeconds() << " seconds";
|
||||
std::cout << " " << m_music.getSampleRate() << " samples / sec";
|
||||
std::cout << " " << m_music.getChannelCount() << " channels" << std::endl;
|
||||
std::cout << " " << pLength << " seconds";
|
||||
std::cout << " -- " << sampleRate << " samples/sec";
|
||||
std::cout << " -- " << format << " format";
|
||||
std::cout << " -- " << channels << " channels" << std::endl;
|
||||
}
|
||||
|
||||
float AudioLayerWidget::getDuration()
|
||||
{
|
||||
float pLength;
|
||||
|
||||
if (m_mediaLoaded == false)
|
||||
return 0;
|
||||
ma_result result = ma_sound_get_length_in_seconds(¤tSound, &pLength);
|
||||
if (result != MA_SUCCESS) {
|
||||
qWarning("WARNING: Failed to get total duration %s", m_currentMedia.toLatin1().constData());
|
||||
return 0;
|
||||
}
|
||||
return pLength;
|
||||
}
|
||||
|
||||
void AudioLayerWidget::fileLoaded(QString file)
|
||||
|
|
@ -236,56 +253,66 @@ void AudioLayerWidget::durationChanged(qint64 dur)
|
|||
|
||||
void AudioLayerWidget::play(bool loop)
|
||||
{
|
||||
m_music.play();
|
||||
m_music.setLoop(loop);
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
ma_sound_set_looping(¤tSound, loop);
|
||||
ma_sound_start(¤tSound);
|
||||
m_loopCheck->blockSignals(true);
|
||||
m_loopCheck->setChecked(m_music.getLoop());
|
||||
m_loopCheck->setChecked(loop);
|
||||
m_loopCheck->blockSignals(false);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::pause()
|
||||
{
|
||||
m_music.pause();
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
ma_sound_stop(¤tSound);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::stop()
|
||||
{
|
||||
m_music.stop();
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
ma_sound_stop(¤tSound);
|
||||
ma_sound_seek_to_pcm_frame(¤tSound, 0);
|
||||
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(0));
|
||||
m_statusValue->setText(STOP_LABEL);
|
||||
m_suspendResumeButton->setText(tr(STOP_LABEL));
|
||||
m_progressSlider->setValue(0);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::refreshGUI() {
|
||||
// m_receiveDMX->setChecked(false);
|
||||
int progress;
|
||||
switch (m_music.getStatus()) {
|
||||
case sf::SoundSource::Playing:
|
||||
progress = m_music.getPlayingOffset().asMilliseconds();
|
||||
float progress;
|
||||
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
if (currentSound.ownsDataSource == 0)
|
||||
return;
|
||||
switch (ma_sound_is_playing(¤tSound)) {
|
||||
case true:
|
||||
ma_sound_get_cursor_in_seconds(¤tSound, &progress);
|
||||
progress *= 1000;
|
||||
m_progressSlider->setValue(progress);
|
||||
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(progress));
|
||||
m_statusValue->setText(PLAY_LABEL);
|
||||
m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
|
||||
break;
|
||||
case sf::SoundSource::Paused:
|
||||
case false:
|
||||
m_statusValue->setText(PAUSE_LABEL);
|
||||
m_suspendResumeButton->setText(tr(RESUME_LABEL));
|
||||
break;
|
||||
case sf::SoundSource::Stopped:
|
||||
m_progressTime->setTime(QTime::fromMSecsSinceStartOfDay(0));
|
||||
m_statusValue->setText(STOP_LABEL);
|
||||
m_suspendResumeButton->setText(tr(RESUME_LABEL));
|
||||
m_progressSlider->setValue(0);
|
||||
break;
|
||||
}
|
||||
//m_loopCheck->blockSignals(true);
|
||||
//m_loopCheck->setChecked(m_music.getLoop());
|
||||
//m_loopCheck->blockSignals(false);
|
||||
}
|
||||
|
||||
void AudioLayerWidget::toggleSuspendResume()
|
||||
{
|
||||
if (m_music.getStatus() == sf::SoundSource::Playing) {
|
||||
m_music.pause();
|
||||
if (m_mediaLoaded == false)
|
||||
return;
|
||||
if (ma_sound_is_playing(¤tSound)) {
|
||||
this->pause();
|
||||
} else {
|
||||
m_music.play();
|
||||
if (ma_sound_at_end(¤tSound))
|
||||
ma_sound_seek_to_pcm_frame(¤tSound, 0);
|
||||
ma_sound_start(¤tSound);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue