audiowaveform.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "audiowaveform.h"
  2. #include <QFile>
  3. #define BITS_DEPTH_16
  4. AudioWaveform::AudioWaveform(QQuickItem* parent)
  5. : QQuickPaintedItem{parent}
  6. {
  7. m_pCustomPlot = new QCustomPlot;
  8. m_pAudioWaveformGraph = m_pCustomPlot->addGraph();
  9. m_pAudioWaveformGraph->setPen(QPen(Qt::red));
  10. m_pCustomPlot->yAxis->setNumberFormat("gbc");
  11. m_pCustomPlot->yAxis->setNumberPrecision(1);
  12. m_pCustomPlot->yAxis->setRange(-1, 1);
  13. m_pCustomPlot->setBackground(QBrush(QColor("#cde8ff")));
  14. // m_pCustomPlot->setBackground(QBrush(QColor(Qt::lightGray)));
  15. m_pCustomPlot->xAxis->setVisible(false);
  16. m_pCustomPlot->yAxis->setVisible(false);
  17. m_pCustomPlot->setOpenGl(true);
  18. }
  19. AudioWaveform::~AudioWaveform()
  20. {
  21. m_pAudioWaveformGraph->data().clear();
  22. m_pCustomPlot->clearGraphs();
  23. delete m_pCustomPlot;
  24. }
  25. void AudioWaveform::paint(QPainter *painter)
  26. {
  27. m_pCustomPlot->setGeometry(0,0,this->width(),this->height());
  28. painter->drawPixmap(0,0,this->width(),this->height(), m_pCustomPlot->toPixmap());
  29. }
  30. void AudioWaveform::refresh()
  31. {
  32. update(QRect(0,0,width(), height()));
  33. }
  34. void AudioWaveform::paintWaveForm()
  35. {
  36. QVector<double> xValue;
  37. QVector<double> yValue;
  38. QFile file(":/audio/audio1.pcm");
  39. if(!file.open(QIODevice::ReadOnly))
  40. {
  41. qDebug() << file.errorString();
  42. return;
  43. }
  44. int maxSize = file.size() / 2;
  45. // 除以2是多绘制一些点,使波形看起来更加连续
  46. int step = maxSize / this->width() / 2;
  47. if (step < 1)
  48. step = 1;
  49. else if (step > maxSize)
  50. step = maxSize;
  51. short cur_max = 0;
  52. short cur_min = 0;
  53. int index_max = 0;
  54. int index_min = 0;
  55. quint64 index = 0;
  56. #ifdef BITS_DEPTH_8
  57. char* buffer = new char[step];
  58. while(qint64 size = file.read(buffer, step))
  59. {
  60. cur_max = buffer[0];
  61. cur_min = buffer[0];
  62. index_max = 0;
  63. index_min = 0;
  64. for(int i=0;i<size;i++)
  65. {
  66. //遍历找这一段的最大最小值
  67. if (cur_max < buffer[i])
  68. {
  69. cur_max = buffer[i];
  70. index_max = i;
  71. }
  72. if (cur_min > buffer[i])
  73. {
  74. cur_min = buffer[i];
  75. index_min = i;
  76. }
  77. }
  78. //根据先后顺序存最大最小,相等就存一个
  79. if (index_max < index_min)
  80. {
  81. xValue.push_back(index);
  82. yValue.push_back(qreal(uchar(cur_max) - 128) / qreal(128));
  83. // m_vector.push_back(QPointF(index, qreal(uchar(cur_max) - 128) / qreal(128)));
  84. index++;
  85. }
  86. xValue.push_back(index);
  87. yValue.push_back(qreal(uchar(cur_min) - 128) / qreal(128));
  88. // m_vector.push_back(QPointF(index, qreal(uchar(cur_min) - 128) / qreal(128)));
  89. index++;
  90. if (index_max > index_min)
  91. {
  92. xValue.push_back(index);
  93. yValue.push_back(qreal(uchar(cur_max) - 128) / qreal(128));
  94. // m_vector.push_back(QPointF(index, qreal(uchar(cur_max) - 128) / qreal(128)));
  95. index++;
  96. }
  97. }
  98. delete[] buffer;
  99. #endif
  100. #ifdef BITS_DEPTH_16
  101. char* buffer = new char[step * 2];
  102. while(qint64 size = file.read(buffer, step * 2))
  103. {
  104. short* data = (short*)buffer;
  105. cur_max = data[0];
  106. cur_min = data[0];
  107. index_max = 0;
  108. index_min = 0;
  109. for(int i=0;i<size / 2;i++)
  110. {
  111. //遍历找这一段的最大最小值
  112. if (cur_max < data[i])
  113. {
  114. cur_max = data[i];
  115. index_max = i;
  116. }
  117. if (cur_min > data[i])
  118. {
  119. cur_min = data[i];
  120. index_min = i;
  121. }
  122. }
  123. //根据先后顺序存最大最小,相等就存一个
  124. if (index_max < index_min)
  125. {
  126. xValue.push_back(index);
  127. yValue.push_back(cur_max / 32768.0);
  128. // m_vector.push_back(QPointF(index, cur_max / 32768.0));
  129. index++;
  130. }
  131. xValue.push_back(index);
  132. yValue.push_back(cur_min / 32768.0);
  133. // m_vector.push_back(QPointF(index, cur_min / 32768.0));
  134. index++;
  135. if (index_max > index_min)
  136. {
  137. xValue.push_back(index);
  138. yValue.push_back(cur_max / 32768.0);
  139. // m_vector.push_back(QPointF(index, cur_max / 32768.0));
  140. index++;
  141. }
  142. }
  143. delete[] buffer;
  144. #endif
  145. file.close();
  146. this->setData(xValue, yValue);
  147. this->refresh();
  148. }
  149. void AudioWaveform::setData(QVector<double> xValue, QVector<double> yValue)
  150. {
  151. m_pAudioWaveformGraph->data().clear();
  152. m_pCustomPlot->xAxis->setRange(0, xValue.size());
  153. m_pAudioWaveformGraph->setData(xValue, yValue);
  154. this->refresh();
  155. }