Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved scaling for waveform plot views #280

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EZAudio.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
469F4CF41B749F7800666A46 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
469F4CF41B749F7800666A46 /* Products */ = {
isa = PBXGroup;
Expand Down
5 changes: 5 additions & 0 deletions EZAudio/EZAudioPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ FOUNDATION_EXPORT UInt32 const EZAudioPlotDefaultHistoryBufferLength;
*/
FOUNDATION_EXPORT UInt32 const EZAudioPlotDefaultMaxHistoryBufferLength;

/**
The number of additional points required for the first and last point. These are required when drawing filled plots are both on the center line.
*/
FOUNDATION_EXPORT UInt32 const kEZAudioPlotAdditionalPointCount;

//------------------------------------------------------------------------------
#pragma mark - EZAudioPlotWaveformLayer
//------------------------------------------------------------------------------
Expand Down
27 changes: 18 additions & 9 deletions EZAudio/EZAudioPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
UInt32 const kEZAudioPlotDefaultHistoryBufferLength = 512;
UInt32 const EZAudioPlotDefaultHistoryBufferLength = 512;
UInt32 const EZAudioPlotDefaultMaxHistoryBufferLength = 8192;
UInt32 const kEZAudioPlotAdditionalPointCount = 2;

//------------------------------------------------------------------------------
#pragma mark - EZAudioPlot (Implementation)
Expand Down Expand Up @@ -271,7 +272,7 @@ - (CGPathRef)createPathWithPoints:(CGPoint *)points
if (pointCount > 0)
{
path = CGPathCreateMutable();
double xscale = (rect.size.width) / ((float)self.pointCount);
double xscale = (rect.size.width) / ((float)(self.pointCount - 1 - kEZAudioPlotAdditionalPointCount));
double halfHeight = floor(rect.size.height / 2.0);
int deviceOriginFlipped = [self isDeviceOriginFlipped] ? -1 : 1;
CGAffineTransform xf = CGAffineTransformIdentity;
Expand Down Expand Up @@ -347,14 +348,22 @@ - (void)updateBuffer:(float *)buffer withBufferSize:(UInt32)bufferSize

- (void)setSampleData:(float *)data length:(int)length
{
CGPoint *points = self.points;
for (int i = 0; i < length; i++)
{
points[i].x = i;
points[i].y = data[i] * self.gain;
}
points[0].y = points[length - 1].y = 0.0f;
self.pointCount = length;
CGPoint *allPoints = self.points;

CGPoint *points = &allPoints[1];
for (int i = 0; i < length; i++)
{
points[i].x = i;
points[i].y = data[i] * self.gain;
}

const int first = 0;
const int last = length + 1;
allPoints[first].x = 0.0f;
allPoints[last].x = length - 1;
allPoints[first].y = allPoints[last].y = 0.0f;

self.pointCount = length + kEZAudioPlotAdditionalPointCount;
}

//------------------------------------------------------------------------------
Expand Down
26 changes: 20 additions & 6 deletions EZAudio/EZAudioPlotGL.m
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,16 @@ - (void)updateBuffer:(float *)buffer withBufferSize:(UInt32)bufferSize

- (void)setSampleData:(float *)data length:(int)length
{
int pointCount = self.shouldFill ? length * 2 : length;
EZAudioPlotGLPoint *points = self.info->points;
const BOOL shouldFill = self.shouldFill;

int pointCount = (shouldFill ? length * 2 : length) + (shouldFill ? kEZAudioPlotAdditionalPointCount : 0);

EZAudioPlotGLPoint *allPoints = self.info->points;
EZAudioPlotGLPoint *points = (shouldFill ? &allPoints[1] : allPoints);

for (int i = 0; i < length; i++)
{
if (self.shouldFill)
if (shouldFill)
{
points[i * 2].x = points[i * 2 + 1].x = i;
points[i * 2].y = data[i];
Expand All @@ -315,9 +320,18 @@ - (void)setSampleData:(float *)data length:(int)length
points[i].y = data[i];
}
}
points[0].y = points[pointCount - 1].y = 0.0f;

if (shouldFill)
{
const int first = 0;
const int last = pointCount - 1;
allPoints[first].x = 0.0f;
allPoints[last].x = length - 1;
allPoints[first].y = allPoints[last].y = 0.0f;
}

self.info->pointCount = pointCount;
self.info->interpolated = self.shouldFill;
self.info->interpolated = shouldFill;
#if !TARGET_OS_IPHONE
[self.openGLContext lock];
glBindVertexArray(self.info->vab);
Expand Down Expand Up @@ -485,7 +499,7 @@ - (void)redrawWithPoints:(EZAudioPlotGLPoint *)points
glClear(GL_COLOR_BUFFER_BIT);
GLenum mode = interpolated ? GL_TRIANGLE_STRIP : GL_LINE_STRIP;
float interpolatedFactor = interpolated ? 2.0f : 1.0f;
float xscale = 2.0f / ((float)pointCount / interpolatedFactor);
float xscale = 2.0f / (((float)(pointCount) / interpolatedFactor) - 3.0f);
float yscale = 1.0f * gain;
GLKMatrix4 transform = GLKMatrix4MakeTranslation(-1.0f, 0.0f, 0.0f);
transform = GLKMatrix4Scale(transform, xscale, yscale, 1.0f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
666062321C5421A400FB99FA /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
666062321C5421A400FB99FA /* Products */ = {
isa = PBXGroup;
Expand Down
1 change: 1 addition & 0 deletions EZAudioExamples/OSX/FFT/FFT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
66374D891C544D2E000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374D891C544D2E000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
6660627A1C5427CC00FB99FA /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
6660627A1C5427CC00FB99FA /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
66374DA91C54503E000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374DA91C54503E000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
662B6CB61C542DF500353D48 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
662B6CB61C542DF500353D48 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
66374D3B1C542E18000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374D3B1C542E18000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
66374D661C54456C000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374D661C54456C000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
66374E181C545A8A000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374E181C545A8A000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
1 change: 1 addition & 0 deletions EZAudioExamples/iOS/FFT/FFT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
66374EEA1C545B18000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374EEA1C545B18000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
66374E3B1C545AA8000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374E3B1C545AA8000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
66374EC71C545AFB000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374EC71C545AFB000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
66374E5E1C545AC6000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374E5E1C545AC6000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
66374E811C545AD8000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374E811C545AD8000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
66374EA41C545AEA000B19D0 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
66374EA41C545AEA000B19D0 /* Products */ = {
isa = PBXGroup;
Expand Down