Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Play midi file .. problem 4.8.6
Forum Updated to NodeBB v4.3 + New Features

Play midi file .. problem 4.8.6

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        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
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SherifOmran
                wrote on last edited by
                #7

                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;;
                

                }

                @

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SherifOmran
                  wrote on last edited by
                  #8

                  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();
                  

                  }

                  @

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SherifOmran
                    wrote on last edited by
                    #9

                    any help

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved