Results 1 to 4 of 4

Thread: Increase QPainter performance

  1. #1
    Join Date
    Sep 2006
    Location
    the Netherlands
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Increase QPainter performance

    Hey,

    I decided to develop my own audio editor targeted at audio for music. I've spend like 40 to 50 hours on it yet and I've managed to accomplish a few milestones already. Here's a list of things I have implemented:

    - Everyting is written in C and C++. Some critical code is implemented in C for performance. I'm planning to port this to ASM some time.
    - I'm using Qt 4.5 for the graphical user interface and some if it's classes to simplify cross-platform compatibility. (it's running on Mac OS X and Windows. Linux should work, but is untested)
    - Reading of a few audio file formats is implemented. (at this moment: wav, aiff, snd, w64 and my own raw format)
    - Drawing of a nice waveform is implemented.
    - Drawing of a frequency spectrum over time is implemented (like in soundtrack pro)
    - Some usual user-interface interactions like selecting and scrolling.
    - Almost everything that has nothing to do with audio and DSP programming (dialog boxes like settings and things like session management, etc...)

    I'm really proud of what I've accomplished so far (I have no experience with DSP and audio programming yet).
    The main problem I'm facing right now is performance issues with drawing a waveform... As it is not possible to load an entire audio file in the memory (a 3 minute wave file already is like 50 megs...) I have to do some smart stuff with caching and loading individual samples from the wave file. I've implemented this by reading a fixed amount of samples (512 for instance) from the hard disk each time the application needs a sample. Those 512 samples are then cached for quick retrieval so the next 511 samples could be read from the memory instantly.
    When I'm using a profiler (in my case Apple Shark) I can see that the function used for retrieving samples is a lot faster indeed. Now my bottleneck seems to be QPainter!

    I'm drawing a line from the center of the waveform widget to a certain value in the positive and a certain value in the negative space. That are two QPainter->drawLine() calls per width-pixel (so when my waveformarea is 500 px wide, there are 1000 calls to QPainter->drawLine).
    These two calls are using more than 80% of the time in the update function (and there is a lot going on in that function, including waveform sample retrieval).
    I suppose there is a better way to implement this, but how? Maybe drawing the lines into a buffer and drawing that buffer in the QPainter in one call?

    Thanks in advance!

    Bas

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

    Default Re: Increase QPainter performance

    Can we see the code?
    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.


  3. #3
    Join Date
    Sep 2006
    Location
    the Netherlands
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Increase QPainter performance

    of course!

    Qt Code:
    1. int heighthalf = height / 2;
    2. int channels = this->audioFile->sfinfo.channels;
    3. int i, q = 0;
    4. int frames = this->audioFile->sfinfo.frames;
    5.  
    6. int resolution = (to - from) / width;
    7.  
    8. QPainter painter(canvas);
    9.  
    10. painter.setBrush(QBrush(QColor::fromRgb(230, 230, 230, 255), Qt::SolidPattern));
    11. painter.drawRect(-10, -10, width + 10, height + 10);
    12.  
    13. painter.setPen(QColor::fromRgb(69, 102, 99, 255));
    14. float pos = 0.0;
    15. float neg = 0.0;
    16. int left = 0;
    17. for (i = 0; left < width && from + i < frames; i++) {
    18. double t = this->audioFile->getFrame(from + i, 0);
    19. if (t <= 1.0 && t >= -1.0) {
    20. if (t > 0.0) {
    21. if (t > pos)
    22. pos = t;
    23. } else {
    24. if (t < neg)
    25. neg = t;
    26. }
    27. }
    28. if (q >= resolution) {
    29. neg = -neg;
    30. QPoint currentpos = QPoint(left, heighthalf - (pos * heighthalf));
    31. QPoint currentneg = QPoint(left, heighthalf + (neg * heighthalf));
    32. painter.drawLine(currentpos, QPoint(left, heighthalf)); // THESE TWO LINES ARE (WHAT I THINK) THE BOTTLENECK
    33. painter.drawLine(currentneg, QPoint(left, heighthalf));
    34. q = 0;
    35. pos = 0;
    36. neg = 0;
    37. left++;
    38. } else {
    39. q++;
    40. }
    41. }
    42.  
    43. painter.setPen(QPen(QColor::fromRgb(69, 102, 99, 60)));
    44. painter.drawLine(0, heighthalf + heighthalf / 2, width, heighthalf + heighthalf / 2);
    45. painter.drawLine(0, heighthalf / 2, width, heighthalf / 2);
    46. painter.setPen(QPen(QColor::fromRgb(56, 84, 90, 255)));
    47. painter.drawLine(0, heighthalf, width, heighthalf);
    To copy to clipboard, switch view to plain text mode 
    Last edited by travelan; 3rd July 2009 at 11:47.

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

    Default Re: Increase QPainter performance

    Try using QPainter::drawLines() instead of QPainter::drawLine(), it's much faster
    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.


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

    travelan (3rd July 2009)

Similar Threads

  1. Performance Issues in Qt-4.4.3 / Qwt-5.1.1
    By swamyonline in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2009, 17:50
  2. Qt4 poor printing performance
    By seneca in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2009, 14:23
  3. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  4. Replies: 7
    Last Post: 20th March 2006, 20:03
  5. Increasing performance from Qtextedit, listview, etc?
    By taylor34 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 10:20

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.