Exemplo n.º 1
0
void Spectrogram::paintEvent(QPaintEvent *event)
{
	QElapsedTimer timer;
	timer.start();

	QRect rect = event->rect();
	QPainter painter(this);
	painter.fillRect(rect, Qt::black);

	if (inputSource != nullptr) {
		int height = rect.height();
		int y = rect.y();

		QImage image(fftSize, height, QImage::Format_RGB32);

		while (height > 0) {
			int tileOffset = y % linesPerTile(); // To handle drawing a partial first tile
			int drawHeight = std::min(linesPerTile() - tileOffset, height); // Draw rest of first tile, full tile, or partial final tile
			off_t tileId = lineToSample(y - tileOffset);
			QPixmap *tile = getPixmapTile(tileId);
			painter.drawPixmap(QRect(0, y, fftSize, drawHeight), *tile, QRect(0, tileOffset, fftSize, drawHeight));
			y += drawHeight;
			height -= drawHeight;
		}

		paintTimeAxis(&painter, rect);
	}

	qDebug() << "Paint: " << timer.elapsed() << "ms";
}
Exemplo n.º 2
0
void Spectrogram::paintEvent(QPaintEvent *event)
{
	QElapsedTimer timer;
	timer.start();

	QRect rect = event->rect();
	QPainter painter(this);
	painter.fillRect(rect, Qt::black);

	if (inputSource != nullptr) {
		int height = rect.height();

		float *line = (float*)malloc(fftSize * sizeof(float));

		QImage image(fftSize, height, QImage::Format_RGB32);
		for (int y = 0; y < height; y++) {
			getLine(line, rect.y() + y);
			for (int x = 0; x < fftSize; x++) {
				float powerRange = std::abs(powerMin - powerMax);
				float normPower = (line[x] - powerMax) * -1.0f / powerRange;
				normPower = clamp(normPower, 0.0f, 1.0f);

				float red, green, blue;
				HSVtoRGB(&red, &green, &blue, normPower * 300.0f, 1, 1 - normPower);

				image.setPixel(x, y, qRgb(red * 255, green * 255, blue * 255));
			}
		}

		QPixmap pixmap = QPixmap::fromImage(image);
		painter.drawPixmap(QRect(0, rect.y(), fftSize, height), pixmap);

		free(line);

		paintTimeAxis(&painter, rect);
	}

	qDebug() << "Paint: " << timer.elapsed() << "ms";
}