Results 1 to 10 of 10

Thread: How can I prevent audio application to abort?

  1. #1
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How can I prevent audio application to abort?

    I am programming a lip-sync software for animation.
    I take a .wav file, and split its 44100 samples pr.sec into 25 or 30 equally big chunks, depending on chosen frames pr. sec.
    I have a QTableWidget, where I can enter characters for every frame. Every character is mapped with a picture of a mouth, and when I press SPACE I hear the audio, and see the pictures in sync with the audio. No problems

    When I want to search in the audio, frame by frame, I can do it with the up/down-arrow keys. It works if I press these keys two or three times pr. sec., but if I hold one of the keys down, the application crashes. Why?
    I would like to avoid hacks like sleep-functions, but what should I do?

    Here is the code that plays the active frame.
    Qt Code:
    1. mouthFrame = activeFrame;
    2. QFile audioFil(audioFileName);
    3. if (audioFil.open(QIODevice::ReadOnly)){
    4. int offset = dataOffset + 8 + ((activeFrame - 1) * samplesPerFrame * blockAlign);
    5. audioFil.seek(offset);
    6.  
    7. ba = audioFil.read(i * samplesPerFrame * blockAlign );
    8. audioFil.close();
    9. QBuffer* audioBuffer = new QBuffer(&ba);
    10. audioBuffer->open(QIODevice::ReadOnly);
    11.  
    12. output = new QAudioOutput(format,this);
    13. output->start(audioBuffer);
    14. output->setNotifyInterval(1000/fps);
    15.  
    16. QEventLoop loop;
    17. connect(output, SIGNAL(stateChanged(QAudio::State)), &loop, SLOT(quit()));
    18. connect(output, SIGNAL(notify()),this, SLOT(showMouth()));
    19. do {
    20. loop.exec();
    21. } while(output->state() == QAudio::ActiveState);
    22. output->stop();
    23. audioBuffer->close();
    24. delete output;
    To copy to clipboard, switch view to plain text mode 
    As I said, it works if you're patient...
    Could it be improved?

    David

  2. The following 3 users say thank you to davidlamhauge for this useful post:

    ebirdseystew (18th December 2013)

  3. #2
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I prevent audio application to abort?

    What code are you using to handle the arrow keys? Could block entry into that code until the previous arrow key has been processed?

  4. #3
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I prevent audio application to abort?

    I use keyPressEvent.
    Here is some of the code:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *e){
    2. if (!upDownRunning && framesTotal > 0){
    3. switch (e->key()) {
    4. case Qt::Key_Down:case Qt::Key_Right:{ // Key_Down or Key_Right = 1 frame forward
    5. if(activeFrame < framesTotal){
    6. upDownRunning = true;
    7. activeFrame += 1;
    8. showMouth(framePhonemeList[activeFrame-1]);
    9. playwav(searchPreRoll);
    10. }
    11. break;
    12. }
    To copy to clipboard, switch view to plain text mode 
    upDownRunning is the current "sleep"-function. It is a boolean that is set true when the key is pressed, and false when the audio is played. But it doesn't work.
    searchPreRoll is a value (1 or 2) that tells how many frames to play.

  5. #4
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I prevent audio application to abort?

    I suppose since upDownRunning isn't set to true IMMEDIATELY after its checked, it could be that the key press events are coming in so fast that the second one has passed the IF before the first has set it to true. I don't know how many instructions it takes to evaluate framesTotal > 0, switch(e->key()), and activeFrame < framesTotal. I'm probably wrong though.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I prevent audio application to abort?

    What does playwav() do? How does it work? If it is the code you posted at the beginning then your application crashes most likely because it runs out of stack. You are spawning an event processing loop which will trigger the next key to be processed which will trigger another event processing loop, etc. You have to correct this code to use signals and slots properly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I prevent audio application to abort?

    Quote Originally Posted by wysota View Post
    What does playwav() do? How does it work? If it is the code you posted at the beginning then your application crashes most likely because it runs out of stack. You are spawning an event processing loop which will trigger the next key to be processed which will trigger another event processing loop, etc. You have to correct this code to use signals and slots properly.
    Like you write, my application probably runs out of stack. Here is the header of the playWaw():
    Qt Code:
    1. void MainWindow::playwav(int i)
    2. {
    3.  
    4. mouthFrame = activeFrame;
    5. QFile audioFil(audioFileName);
    6. if (audioFil.open(QIODevice::ReadOnly)){
    7. int offset = dataOffset + 8 + ((activeFrame - 1) * samplesPerFrame * blockAlign);
    8. audioFil.seek(offset);
    To copy to clipboard, switch view to plain text mode 

    But... - since I use the keyPressEvent, and not signals and slots, how can I use signals and slots properly?

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I prevent audio application to abort?

    You need to come up with an algorithm, what your code is supposed to do, how it is supposed to behave. Currently you expect to have a linear flow in your code but this is not a good approach for an event driven framework such as Qt and even more for asynchronous operations such as sound playback. A possible approach is that you schedule sound playback, return to the event loop and when you are notified that the sound finished playing, start another note. You can queue a series of sounds and just feed them to the player when required.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    davidlamhauge (18th December 2013)

  10. #8
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I prevent audio application to abort?

    Quote Originally Posted by wysota View Post
    You need to come up with an algorithm, what your code is supposed to do, how it is supposed to behave. Currently you expect to have a linear flow in your code but this is not a good approach for an event driven framework such as Qt and even more for asynchronous operations such as sound playback. A possible approach is that you schedule sound playback, return to the event loop and when you are notified that the sound finished playing, start another note. You can queue a series of sounds and just feed them to the player when required.
    My problem is that my code works the way I want - except from the fact that it crashes the program...
    I don't know how to "schedule" my "sound playback", but maybe it is here that I should use signals and slots?
    And when you write "... start another note", is that a thread? or?
    How do I "queue a series of sounds"? I don't use phonon, but maybe it is a possibility anyway...
    A lot to think about.

    Thanks, David

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I prevent audio application to abort?

    Quote Originally Posted by davidlamhauge View Post
    My problem is that my code works the way I want - except from the fact that it crashes the program...
    So it doesn't work the way you want

    I don't know how to "schedule" my "sound playback", but maybe it is here that I should use signals and slots?
    The simplest way I know is to use QQueue or equivalent. The when you get signalled, check if the user currently presses the key to play the next sound and if so, take the next element from the queue.

    And when you write "... start another note", is that a thread? or?
    How do I "queue a series of sounds"? I don't use phonon, but maybe it is a possibility anyway...
    A lot to think about.
    Similar to:

    Qt Code:
    1. struct MouthEntry {
    2. const QString soundPath;
    3. const QString mouthPath;
    4. };
    5.  
    6. QQueue<MouthEntry> m_mouthQueue;
    7.  
    8. void enqueueWordToSay(const QString &word) {
    9. for(int i=0;i<word.size();++i) m_mouthQueue = createEntryFromSound(word.at(i)); // this is oversimplified of course
    10. }
    11.  
    12. connect(audioOutput, SIGNAL(stateChanged(...)), this, SLOT(maybePlayNextNote()));
    13.  
    14. void X::maybePlayNextNote() {
    15. if(!iuserWantsNextFrame) { m_playing = false; }
    16. if(m_mouthQueue.isEmpty()) { m_playing = false; }
    17. playNote(m_mouthQueue.dequeue());
    18. }
    19.  
    20. void X::playNote(MouthEntry entry) {
    21. showMouth(entry.mouthPath);
    22. play(entry.soundPath);
    23. m_playing = true;
    24. }
    25.  
    26. void X::keyPressEvent(QKeyEvent *ke) {
    27. if(ke->key() == Qt::Key_Right) { userWantsNextFrame = true; maybePlayNextNote(); return; }
    28. }
    29.  
    30. void X::keyReleaseEvent(QKeyEvent *ke) {
    31. if(ke->key() == Qt::Key_Right) { userWantsNextFrame = false; return; }
    32. }
    To copy to clipboard, switch view to plain text mode 

    etc.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    davidlamhauge (18th December 2013)

  13. #10
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I prevent audio application to abort?

    Thank a lot!
    I should have guessed that Qt had a QQueue, ready for me
    I'll loook into it, when I come home from work

    David

Similar Threads

  1. QFtp abort()
    By Talei in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2013, 01:48
  2. qt 4.6.3 coredump - sig abort - help!
    By FredB in forum Qt-based Software
    Replies: 0
    Last Post: 26th November 2012, 17:48
  3. How to prevent multiple executables from application
    By jshafferman in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2011, 19:44
  4. Replies: 1
    Last Post: 18th August 2010, 13:39
  5. Replies: 2
    Last Post: 17th April 2009, 22:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.