Skip to content

Commit

Permalink
better waveform drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed Jun 6, 2023
1 parent 5a47b79 commit d33a440
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
67 changes: 50 additions & 17 deletions OpenUtau/Controls/OtoPlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public Tuple<int, double[]>? F0 {
const int kFftSize = 1024;
const int kMelSize = 80;

private static WriteableBitmap? wavBitmap;
private WriteableBitmap? wavBitmap;
private byte[]? wavBitmapData;
private static WriteableBitmap? melBitmap;
private WriteableBitmap? melBitmap;

private IBrush blueFill = new SolidColorBrush(Colors.LightBlue, 0.5);
private IBrush pinkFill = new SolidColorBrush(Colors.Pink, 0.5);
Expand Down Expand Up @@ -215,22 +215,55 @@ void UpdateWav() {
int endSample = (int)Math.Clamp(
(xStart + xSpan) / duration * samples.Length, 0, samples.Length - 1);
double sampelsPerPiexl = (endSample - startSample) / width;
for (int x = 0; x < width; ++x) {
double min = 0;
double max = 0;
for (int j = startSample + (int)(sampelsPerPiexl * x);
j < startSample + (int)(sampelsPerPiexl * (x + 1)); ++j) {
min = Math.Min(min, samples[j]);
max = Math.Max(max, samples[j]);
if (sampelsPerPiexl > 64) {
for (int x = 0; x < width; ++x) {
double min = 0;
double max = 0;
for (int j = startSample + (int)(sampelsPerPiexl * x);
j < startSample + (int)(sampelsPerPiexl * (x + 1)); ++j) {
min = Math.Min(min, samples[j]);
max = Math.Max(max, samples[j]);
}
int maxY = (int)Math.Clamp((height - 1) * (0.5 - max / 2), 0, height - 1);
int minY = (int)Math.Clamp((height - 1) * (0.5 - min / 2), 0, height - 1);
for (int y = maxY; y <= minY; ++y) {
int index = y * width + x;
wavBitmapData[index * 4] = 0;
wavBitmapData[index * 4 + 1] = 0;
wavBitmapData[index * 4 + 2] = 0xFF;
wavBitmapData[index * 4 + 3] = 0xFF;
}
}
int maxY = (int)Math.Clamp((height - 1) * (0.5 - max / 2), 0, height - 1);
int minY = (int)Math.Clamp((height - 1) * (0.5 - min / 2), 0, height - 1);
for (int y = maxY; y <= minY; ++y) {
int index = y * width + x;
wavBitmapData[index * 4] = 0;
wavBitmapData[index * 4 + 1] = 0;
wavBitmapData[index * 4 + 2] = 0xFF;
wavBitmapData[index * 4 + 3] = 0xFF;
} else {
double lastX = 0;
double lastY = 0;
for (int i = startSample; i < endSample; ++i) {
double x = Math.Clamp((i - startSample) / sampelsPerPiexl, 0, width - 1);
double y = Math.Clamp((height - 1) * (0.5 - samples[i] / 2), 0, height - 1);
if (i > startSample) {
double dx;
double dy;
if (x - lastX > Math.Abs(y - lastY)) {
dx = 1;
dy = (y - lastY) / (x - lastX);
} else {
dx = (x - lastX) / Math.Abs(y - lastY);
dy = Math.Sign(y - lastY);
}
double xx = lastX;
double yy = lastY;
while (xx < x) {
int index = (int)(Math.Round(yy) * width + Math.Round(xx));
wavBitmapData[index * 4] = 0;
wavBitmapData[index * 4 + 1] = 0;
wavBitmapData[index * 4 + 2] = 0xFF;
wavBitmapData[index * 4 + 3] = 0xFF;
xx += dx;
yy += dy;
}
}
lastX = x;
lastY = y;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenUtau/Controls/WaveformImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override void Render(DrawingContext context) {

int startSample = 0;
for (int i = 0; i < bitmap.PixelSize.Width; ++i) {
int endTick = (int)(viewModel.TickOrigin + viewModel.TickOffset + (i + 1) / viewModel.TickWidth);
double endTick = viewModel.TickOrigin + viewModel.TickOffset + (i + 1.0) / viewModel.TickWidth;
double endMs = project.timeAxis.TickPosToMsPos(endTick);
int endSample = Math.Clamp((int)((endMs - leftMs) * 44100 / 1000) * 2, 0, sampleCount);
if (endSample > startSample) {
Expand Down

0 comments on commit d33a440

Please sign in to comment.