Play midi file .. problem 4.8.6



  • Hello guys,

    i had a program that was programmed under mac and it worked fine but now i am testing it under windows
    my problem is that i need to play a midi file .. I use the following way, when i emit the startbackgroundsound signal

    @
    qDebug() << "background sound = " << bgsound;
    // ouput = background sound = "C:\Users\Sherif\AppData\Local\login\spacegamebackground.mid"

    QPointer &lt;QSound> backgroundsound = new QSound(bgsound);
    QObject::connect(this,SIGNAL(StartBackGroundSoundSignal()),backgroundsound.data(),SLOT(play()));
    
    emit(StartBackGroundSoundSignal());
    

    @

    what happens is that it beeps and does not play it ..
    Could it be that there is a missing file? I am running from inside QT .. !!

    please advice ..

    thanks


  • Lifetime Qt Champion

    Hi,

    Are you really passing

    @ "C:\Users\Sherif\AppData\Local\login\spacegamebackground.mid"@

    as path ?

    If so you must either escape the backslashes or even better, since you are using Qt, use the forward slash *nix notation.

    Hope it helps



  • I could not solve the issue .. any help? I tested again under MAC but does not play any more

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPointer>
    #include <QSound>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QPointer <QSound> backgroundsound = new QSound("/Volumes/DATA/Win/bp.mid");
    // connect background signal
    backgroundsound->play();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QWidget>
    #include <QtCore>
    #include <QtGui>

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QPointer <QSound> backgroundsound ;

    private:
    Ui::MainWindow *ui;

    };

    #endif // MAINWINDOW_H
    @



  • The "description":http://qt-project.org/doc/qt-4.8/qsound.html#details says that on Windows QSound supports only WAV format.
    And on MacOSX it supports whatever NSSound supports, which I guess, includes MIDI.



  • thanks for the info, but do you know how can i play a midi file?



  • You can use libraries like RTMIDI to read the actual midi, but you will still have to find a way to produce actual musical notes from the midi data, look for some synthesizer libraries.



  • I found the following code online. I need to convert this form into a normal .h file so that i can include it as a class in any c file. I don't know this syntax and when i made a header file out of it, it did not work .. Could any body help? thank ..

    @
    /*

    • QtPlaySMF
    • Part of QtMidi (http://github.com/waddlesplash/qtmidi).
    • Copyright (c) 2003-2012 by David G. Slomin
    • Copyright (c) 2012 WaddleSplash
    • Permission is hereby granted, free of charge, to any person obtaining
    • a copy of this software and associated documentation files (the "Software"),
    • to deal in the Software without restriction, including without limitation
    • the rights to use, copy, modify, merge, publish, distribute, sublicense,
    • and/or sell copies of the Software, and to permit persons to whom the Software
    • is furnished to do so, subject to the following conditions:
    • The above copyright notice and this permission notice shall
    • be included in all copies or substantial portions of the Software.
    • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    • EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    • OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    • IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    • DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    • ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    • DEALINGS IN THE SOFTWARE.

    */

    #include <stdio.h>
    #include <QThread>
    #include <QElapsedTimer>
    #include <QCoreApplication>
    #include <QMidiOut.h>
    #include <QMidiFile.h>

    class MidiPlayer : public QThread
    {
    Q_OBJECT
    public:
    MidiPlayer(QMidiFile* file, QMidiOut* out)
    { midi_file = file; midi_out = out; }
    //MidiPlayer();

    void play();
    

    private:
    QMidiEvent* midi_file_event;
    QMidiFile* midi_file;
    QMidiOut* midi_out;

    protected:
    void run()
    {
    QElapsedTimer t;
    t.start();
    QList<QMidiEvent*>* events = midi_file->events();
    for(int i = 0; i < events->count(); i++)
    {
    midi_file_event = events->at(i);
    if (midi_file_event->type() != QMidiEvent::Meta)
    {
    qint64 event_time = midi_file->timeFromTick(midi_file_event->tick()) * 1000;

                qint32 waitTime = event_time - t.elapsed();
                if(waitTime > 0) {
                    msleep(waitTime);
                }
                handleEvent();
            }
        }
    
        midi_out->disconnect();
    }
    

    private slots:
    void handleEvent()
    {
    if (midi_file_event->type() == QMidiEvent::SysEx)
    { // TODO: sysex
    }
    else
    {
    qint32 message = midi_file_event->message();
    midi_out->sendMsg(message);
    }
    }
    };

    void MidiPlayer::play()
    {
    QString filename = "D:/Release/Win/bg.mid";
    QString midiOutName = "";
    QMidiFile* midi_file = new QMidiFile();
    midi_file->load(filename);

    QMidiOut* midi_out = new QMidiOut();
    midi_out->connect(midiOutName);
    MidiPlayer* p = new MidiPlayer(midi_file,midi_out);
    p->start();
    

    }
    #include "playsmf.moc"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc,argv);

    MidiPlayer *aa;
    aa->play();
    
    //QObject::connect(p,SIGNAL(finished()),&a,SLOT(quit()));
    
    return a.exec(&#41;;
    

    }

    @



  • Here is my header file and c file

    problem: prototype does not match

    @

    #ifndef MIDICLASS_H
    #define MIDICLASS_H

    #include <stdio.h>
    #include <QThread>
    #include <QElapsedTimer>
    #include <QCoreApplication>
    #include <QMidiOut.h>
    #include <QMidiFile.h>

    class midiclass : public QThread
    {
    Q_OBJECT
    public:
    explicit midiclass(QMidiFile* file, QMidiOut* out)
    { midi_file = file; midi_out = out; }
    //MidiPlayer();

    void play();
    

    private:
    QMidiEvent* midi_file_event;
    QMidiFile* midi_file;
    QMidiOut* midi_out;

    signals:
    protected:
    void run()
    {
    QElapsedTimer t;
    t.start();
    QList<QMidiEvent*>* events = midi_file->events();
    for(int i = 0; i < events->count(); i++)
    {
    midi_file_event = events->at(i);
    if (midi_file_event->type() != QMidiEvent::Meta)
    {
    qint64 event_time = midi_file->timeFromTick(midi_file_event->tick()) * 1000;

                qint32 waitTime = event_time - t.elapsed();
                if(waitTime > 0) {
                    msleep(waitTime);
                }
                handleEvent();
            }
        }
    
        midi_out->disconnect();
    }
    

    private slots:
    void handleEvent()
    {
    if (midi_file_event->type() == QMidiEvent::SysEx)
    { // TODO: sysex
    }
    else
    {
    qint32 message = midi_file_event->message();
    midi_out->sendMsg(message);
    }
    }
    };

    #endif // MIDICLASS_H

    @

    here is my .c file

    @
    #include "midiclass.h"

    midiclass::midiclass(QThread *parent) :
    QObject(parent)
    {

    }

    void midiclass::play()
    {
    QString filename = "D:/Activate Software/typing tutor MAC/login/Release/Win/bg.mid";
    QString midiOutName = "";
    QMidiFile* midi_file = new QMidiFile();
    midi_file->load(filename);

    QMidiOut* midi_out = new QMidiOut();
    midi_out->connect(midiOutName);
    MidiPlayer* p = new MidiPlayer(midi_file,midi_out);
    p->start();
    

    }

    @



  • any help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.