Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ keywords = ["compute", "vulkan", "render", "engine", "synth"]
categories = ["rendering::engine"]

[dependencies]
cen = { git = "https://github.com/n-e-l/cen", rev = "62ed91da74058bb8401d1d3e03b45a1a4d658cb2" }
cen = { git = "https://github.com/n-e-l/cen", rev = "77cd8f13a3ac80e143a56cb17472b5c5eeb14992" }
cpal = "0.13.4"
bytemuck = { version = "1.17.0", features = ["derive"] }
egui_plot = "0.34.1"
Expand Down
2 changes: 1 addition & 1 deletion audio/kick.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ vec2 kick(float t, float q) {

// Method called by the main shader
vec2 audio(float t, float v, float f, float[4] option) {
return v * 0.5 * kick(t, option[0] * 0.001);
return v * kick(t, option[0] * 0.001);
}
7 changes: 4 additions & 3 deletions shaders/audio.comp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ layout( std430, binding = 0 ) buffer AudioBuffer {
layout( push_constant ) uniform PushConstants
{
float time;
int samples;
int samples_per_second;
int total_samples;
float frequency;
float volume;
float[4] options;
Expand All @@ -20,6 +21,6 @@ audio_function
void main()
{
int i = int( gl_GlobalInvocationID.x );
if( i > constants.samples ) return;
audio_data.data[i] = audio(constants.time + float( i ) / constants.samples, constants.volume, constants.frequency, constants.options).r;
if( i > constants.total_samples ) return;
audio_data.data[i] = audio(constants.time + float( i ) / constants.samples_per_second, constants.volume, constants.frequency, constants.options).r;
}
51 changes: 51 additions & 0 deletions shaders/audio_background.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#version 450

layout( push_constant ) uniform PushConstants
{
uint samples_per_second;
uint total_samples;
uint pixels_x;
uint pixels_y;
float zoom;
uint offset;
} constants;

layout(location = 0) in vec2 uv;

layout(location = 0) out vec4 outColor;

void main()
{
vec3 color = vec3( 0 );

float offset = constants.offset / float(constants.samples_per_second);

float p = offset + (uv.x - .5) * constants.zoom;

float units_per_pixel = constants.zoom / constants.pixels_x;

const int MINOR_GRID_LINES_PER_SECOND = 100;
const int MAJOR_GRID_LINES_PER_SECOND = 10;

vec3 minor_grid_color = vec3(.02) * min(1., 2. / constants.zoom );
vec3 major_grid_color = vec3(.08) * min(1., 4. / constants.zoom );
if( fract(p * MINOR_GRID_LINES_PER_SECOND) / MINOR_GRID_LINES_PER_SECOND < units_per_pixel ) {
color = minor_grid_color;
}

const int MINOR_GRID_LINES_Y = 4 * 2; // range is from -1 to 1
if( fract(abs(uv.y - .5) * MINOR_GRID_LINES_Y ) / MINOR_GRID_LINES_Y < 2. / constants.pixels_y ) {
color = minor_grid_color;
}

if( fract(p * MAJOR_GRID_LINES_PER_SECOND) / MAJOR_GRID_LINES_PER_SECOND < units_per_pixel ) {
color = major_grid_color;
}
if( fract(abs(uv.y - .5)) < 1. / constants.pixels_y ) {
color = major_grid_color;
}

outColor = vec4(color, 1.0);

// outColor = vec4(uv, 0., 1.);
}
23 changes: 23 additions & 0 deletions shaders/audio_plot.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 450

layout( push_constant ) uniform PushConstants
{
uint samples_per_second;
uint total_samples;
uint pixels_x;
uint pixels_y;
float zoom;
uint offset;
} constants;


layout(location = 0) out vec4 outColor;

layout(location = 0) in vec2 minmax;

void main()
{
vec3 color = vec3(241. / 255.0, 121. / 255., 25. / 255.);
color = pow(color, vec3(2.2));
outColor = vec4(color, 1.0);
}
69 changes: 69 additions & 0 deletions shaders/audio_plot.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#version 450

layout( push_constant ) uniform PushConstants
{
uint samples_per_second;
uint total_samples;
uint pixels_x;
uint pixels_y;
float zoom;
uint offset;
} constants;

struct PixelData {
float minimum;
float maximum;
int direction;
int unused;
};
layout( std430, binding = 0 ) readonly buffer MinMaxBuffer {
PixelData[] data;
} minmax_data;

vec2 vertex[6] = vec2[](
vec2( 0.0, 0.0),
vec2( 1.0, 0.0),
vec2( 0.0, 1.0),

vec2( 1.0, 0.0),
vec2( 1.0, 1.0),
vec2( 0.0, 1.0)
);

layout(location = 0) out vec2 minmax;

void main()
{
float pixelwidth = 2. / float(constants.pixels_x);
float pixelheight = 2. / float(constants.pixels_y);

float x = 2. * gl_InstanceIndex / constants.pixels_x - 1.;

PixelData minmax = minmax_data.data[gl_InstanceIndex];
float minimum = minmax.minimum;
float maximum = minmax.maximum;

// Discard invalid samples
if(maximum < minimum) return;

vec2 vertex_p = vertex[gl_VertexIndex];

// Shift x position in order to have aliased lines
float x_offset = 0.;
if( minmax.direction == 1 ) {
// Upward line, move the top vertices right by half a pixel
// Move the bottom pixel left by half a pixel
x_offset = (vertex_p.y * 2. - 1.) * pixelwidth / 2.;
} else if (minmax.direction == 2) {
// Downward line, move the top vertices left by half a pixel
// Move the bottom pixel right by half a pixel
x_offset = -(vertex_p.y * 2. - 1.) * pixelwidth / 2.;
}
x_offset *= 1.;

float height = max(maximum - minimum, pixelheight * 1.);
vec2 pos = vec2(x + x_offset, minimum) + vertex_p * vec2(pixelwidth, height);


gl_Position = vec4(pos, 0.0, 1.0);
}
13 changes: 13 additions & 0 deletions shaders/fullscreen.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 450

layout(location = 0) out vec2 uv;

void main()
{
vec2 pos = vec2(
float((gl_VertexIndex << 1) & 2) * 2.0 - 1.0,
float(gl_VertexIndex & 2) * 2.0 - 1.0
);
uv = pos * 0.5 + 0.5;
gl_Position = vec4(pos, 0.0, 1.0);
}
63 changes: 63 additions & 0 deletions shaders/minmax_audio_pixels.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#version 450

layout ( local_size_x = 64, local_size_y = 1, local_size_z = 1 ) in;

layout( std430, binding = 0 ) buffer AudioBuffer {
float[] data;
} audio_data;

struct PixelData {
float minimum;
float maximum;
int direction;
int unused;
};
layout( std430, binding = 1 ) buffer MinMaxBuffer {
PixelData[] data;
} minmax_data;

layout( push_constant ) uniform PushConstants
{
uint samples_per_second;
uint total_samples;
uint pixels_x;
uint pixels_y;
float zoom;
uint offset;
} constants;

void main()
{
int pixel = int( gl_GlobalInvocationID.x );
if( pixel > constants.pixels_x ) return;

float samples_per_pixel = constants.zoom * constants.samples_per_second / float(constants.pixels_x);
float pixel_size_y = 2.0 / constants.pixels_y;

int start_sample = int(floor(constants.offset - constants.samples_per_second * constants.zoom / 2. + pixel * samples_per_pixel));
int end_sample = int(ceil(start_sample + samples_per_pixel) + 1); // Add 1 to not have any gaps

// Invalid sample as default
vec2 minmax = vec2(1., -1.); // Min bigger than max

if( start_sample > 0 && end_sample < constants.total_samples ) {
minmax = vec2(100., -100.);
for (int i = start_sample; i < end_sample; i++) {
float s = audio_data.data[i];
minmax[0] = min(minmax[0], s);
minmax[1] = max(minmax[1], s);
}

// Always fill to zero
// minmax[0] = min(minmax[0], 0.);
// minmax[1] = max(minmax[1], 0.);
}

float a = audio_data.data[start_sample];
float b = audio_data.data[end_sample];
int direction = 0;
if( a < b ) direction = 1;
if( a > b ) direction = 2;

minmax_data.data[ pixel ] = PixelData(minmax[0], minmax[1], direction, 0);
}
Loading
Loading