waveformplot.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "waveformplot.h"
  2. #define BITS_DEPTH_16
  3. WaveformPlot::WaveformPlot(QAbstractPlot* parent)
  4. : QAbstractPlot(parent)
  5. {
  6. QCustomPlot* pCustomPlot = this->customPlot();
  7. m_pAudioWaveformGraph = pCustomPlot->addGraph();
  8. m_pAudioWaveformGraph->setPen(QPen(Qt::red));
  9. pCustomPlot->yAxis->setNumberFormat("gbc");
  10. pCustomPlot->yAxis->setNumberPrecision(1);
  11. pCustomPlot->yAxis->setRange(-1, 1);
  12. pCustomPlot->setBackground(QBrush(QColor("#cde8ff")));
  13. pCustomPlot->xAxis->setVisible(true);
  14. pCustomPlot->yAxis->setVisible(true);
  15. pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
  16. // pCustomPlot->axisRect()->setRangeZoomFactor(1.1, 1); // 设置缩放系数
  17. pCustomPlot->axisRect()->setRangeZoom(Qt::Horizontal); // 只有水平方向可以缩放
  18. pCustomPlot->axisRect()->setRangeDrag(Qt::Horizontal); // 只有水平方向可以拖动
  19. this->setOpenGL(true);
  20. }
  21. void WaveformPlot::paintWaveForm()
  22. {
  23. QCustomPlot* pCustomPlot = this->customPlot();
  24. QVector<double> xValue;
  25. QVector<double> yValue;
  26. QFile file(":/audio/audio1.pcm");
  27. if(!file.open(QIODevice::ReadOnly))
  28. {
  29. qDebug() << file.errorString();
  30. return;
  31. }
  32. int maxSize = file.size() / 2;
  33. // 除以2是多绘制一些点,使波形看起来更加连续
  34. int step = maxSize / this->width() / 2;
  35. if (step < 1)
  36. step = 1;
  37. else if (step > maxSize)
  38. step = maxSize;
  39. short cur_max = 0;
  40. short cur_min = 0;
  41. int index_max = 0;
  42. int index_min = 0;
  43. quint64 index = 0;
  44. #ifdef BITS_DEPTH_8
  45. char* buffer = new char[step];
  46. while(qint64 size = file.read(buffer, step))
  47. {
  48. cur_max = buffer[0];
  49. cur_min = buffer[0];
  50. index_max = 0;
  51. index_min = 0;
  52. for(int i=0;i<size;i++)
  53. {
  54. //遍历找这一段的最大最小值
  55. if (cur_max < buffer[i])
  56. {
  57. cur_max = buffer[i];
  58. index_max = i;
  59. }
  60. if (cur_min > buffer[i])
  61. {
  62. cur_min = buffer[i];
  63. index_min = i;
  64. }
  65. }
  66. //根据先后顺序存最大最小,相等就存一个
  67. if (index_max < index_min)
  68. {
  69. xValue.push_back(index);
  70. yValue.push_back(qreal(uchar(cur_max) - 128) / qreal(128));
  71. // m_vector.push_back(QPointF(index, qreal(uchar(cur_max) - 128) / qreal(128)));
  72. index++;
  73. }
  74. xValue.push_back(index);
  75. yValue.push_back(qreal(uchar(cur_min) - 128) / qreal(128));
  76. // m_vector.push_back(QPointF(index, qreal(uchar(cur_min) - 128) / qreal(128)));
  77. index++;
  78. if (index_max > index_min)
  79. {
  80. xValue.push_back(index);
  81. yValue.push_back(qreal(uchar(cur_max) - 128) / qreal(128));
  82. // m_vector.push_back(QPointF(index, qreal(uchar(cur_max) - 128) / qreal(128)));
  83. index++;
  84. }
  85. }
  86. delete[] buffer;
  87. #endif
  88. #ifdef BITS_DEPTH_16
  89. char* buffer = new char[step * 2];
  90. while(qint64 size = file.read(buffer, step * 2))
  91. {
  92. short* data = (short*)buffer;
  93. cur_max = data[0];
  94. cur_min = data[0];
  95. index_max = 0;
  96. index_min = 0;
  97. for(int i=0;i<size / 2;i++)
  98. {
  99. //遍历找这一段的最大最小值
  100. if (cur_max < data[i])
  101. {
  102. cur_max = data[i];
  103. index_max = i;
  104. }
  105. if (cur_min > data[i])
  106. {
  107. cur_min = data[i];
  108. index_min = i;
  109. }
  110. }
  111. //根据先后顺序存最大最小,相等就存一个
  112. if (index_max < index_min)
  113. {
  114. xValue.push_back(index);
  115. yValue.push_back(cur_max / 32768.0);
  116. // m_vector.push_back(QPointF(index, cur_max / 32768.0));
  117. index++;
  118. }
  119. xValue.push_back(index);
  120. yValue.push_back(cur_min / 32768.0);
  121. // m_vector.push_back(QPointF(index, cur_min / 32768.0));
  122. index++;
  123. if (index_max > index_min)
  124. {
  125. xValue.push_back(index);
  126. yValue.push_back(cur_max / 32768.0);
  127. // m_vector.push_back(QPointF(index, cur_max / 32768.0));
  128. index++;
  129. }
  130. }
  131. delete[] buffer;
  132. #endif
  133. file.close();
  134. m_pAudioWaveformGraph->data().clear();
  135. pCustomPlot->xAxis->setRange(0, xValue.size());
  136. m_pAudioWaveformGraph->setData(xValue, yValue);
  137. this->update();
  138. }