Skip to content

Commit

Permalink
remove i24_4b format
Browse files Browse the repository at this point in the history
  • Loading branch information
alichraghi authored and slimsag committed Nov 5, 2023
1 parent 0784968 commit 0a6bb12
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 199 deletions.
4 changes: 1 addition & 3 deletions examples/record.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ pub fn main() !void {
fn readCallback(_: ?*anyopaque, input: []const u8) void {
const format_size = recorder.format().size();
const frames = input.len / format_size;

var buffer: [16 * 1024]f32 = undefined;
recorder.read(f32, buffer[0..frames], input);

sysaudio.convertFrom(f32, buffer[0..frames], recorder.format(), input);
_ = file.write(std.mem.sliceAsBytes(buffer[0 .. input.len / format_size])) catch {};
}
7 changes: 6 additions & 1 deletion examples/sine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ fn writeCallback(_: ?*anyopaque, output: []u8) void {
while (i < output.len) : (i += frame_size) {
const frame_index: f32 = @floatFromInt(i / frame_size);
const sample = @sin((seconds_offset + frame_index * seconds_per_frame) * radians_per_second);
player.write(f32, &.{ sample, sample }, output[i..][0..frame_size]);
sysaudio.convertTo(
f32,
&.{ sample, sample },
player.format(),
output[i..][0..frame_size],
);
}

seconds_offset = @mod(seconds_offset + seconds_per_frame * @as(f32, @floatFromInt(frames)), 1.0);
Expand Down
5 changes: 0 additions & 5 deletions src/alsa.zig
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,6 @@ pub fn toAlsaFormat(format: main.Format) c.snd_pcm_format_t {
.u8 => c.SND_PCM_FORMAT_U8,
.i16 => if (is_little) c.SND_PCM_FORMAT_S16_LE else c.SND_PCM_FORMAT_S16_BE,
.i24 => if (is_little) c.SND_PCM_FORMAT_S24_3LE else c.SND_PCM_FORMAT_S24_3BE,
.i24_4b => if (is_little) c.SND_PCM_FORMAT_S24_LE else c.SND_PCM_FORMAT_S24_BE,
.i32 => if (is_little) c.SND_PCM_FORMAT_S32_LE else c.SND_PCM_FORMAT_S32_BE,
.f32 => if (is_little) c.SND_PCM_FORMAT_FLOAT_LE else c.SND_PCM_FORMAT_FLOAT_BE,
};
Expand Down Expand Up @@ -832,7 +831,3 @@ pub fn toCHMAP(pos: main.ChannelPosition) c_uint {
.top_back_center => c.SND_CHMAP_TRC,
};
}

test "reference declarations" {
std.testing.refAllDeclsRecursive(@This());
}
86 changes: 32 additions & 54 deletions src/conv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ const shr = std.math.shr;
const maxInt = std.math.maxInt;
const clamp = std.math.clamp;

pub inline fn unsignedToSigned(in: anytype, out: anytype) void {
const InputType = std.meta.Child(@TypeOf(in));
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const half = 1 << (@bitSizeOf(InputType) - 1);
const trunc = @bitSizeOf(OutputType) - @bitSizeOf(InputType);
out_sample.* = @as(OutputType, @intCast(in_sample.* -% half)) << trunc;
pub inline fn unsignedToSigned(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const half = 1 << (@bitSizeOf(SrcType) - 1);
const trunc = @bitSizeOf(DestType) - @bitSizeOf(SrcType);
out_sample.* = @as(DestType, @intCast(in_sample.* -% half)) << trunc;
}
}

Expand All @@ -29,13 +26,10 @@ test unsignedToSigned {
try expectEqual(@as(i32, -2063597568), u8_to_i32[0]);
}

pub inline fn unsignedToFloat(in: anytype, out: anytype) void {
const InputType = std.meta.Child(@TypeOf(in));
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const half = (1 << @typeInfo(InputType).Int.bits) / 2;
out_sample.* = (@as(OutputType, @floatFromInt(in_sample.*)) - half) * 1.0 / half;
pub inline fn unsignedToFloat(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const half = (1 << @typeInfo(SrcType).Int.bits) / 2;
out_sample.* = (@as(DestType, @floatFromInt(in_sample.*)) - half) * 1.0 / half;
}
}

Expand All @@ -45,13 +39,10 @@ test unsignedToFloat {
try expectEqual(@as(f32, -0.9609375), u8_to_f32[0]);
}

pub inline fn signedToUnsigned(in: anytype, out: anytype) void {
const InputType = std.meta.Child(@TypeOf(in));
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const half = 1 << @bitSizeOf(OutputType) - 1;
const trunc = @bitSizeOf(InputType) - @bitSizeOf(OutputType);
pub inline fn signedToUnsigned(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const half = 1 << @bitSizeOf(DestType) - 1;
const trunc = @bitSizeOf(SrcType) - @bitSizeOf(DestType);
out_sample.* = @intCast((in_sample.* >> trunc) + half);
}
}
Expand All @@ -70,13 +61,10 @@ test signedToUnsigned {
try expectEqual(@as(u8, 128), i32_to_u8[0]);
}

pub inline fn signedToSigned(in: anytype, out: anytype) void {
const InputType = std.meta.Child(@TypeOf(in));
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const trunc = @bitSizeOf(InputType) - @bitSizeOf(OutputType);
out_sample.* = shr(OutputType, @intCast(in_sample.*), trunc);
pub inline fn signedToSigned(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const trunc = @bitSizeOf(SrcType) - @bitSizeOf(DestType);
out_sample.* = shr(DestType, @intCast(in_sample.*), trunc);
}
}

Expand All @@ -100,22 +88,17 @@ test signedToSigned {
try expectEqual(@as(i24, 1280), i16_to_i24[0]);
try expectEqual(@as(i32, 327680), i16_to_i32[0]);

// TODO: correctness
// try expectEqual(@as(i16, 0), i24_to_i16[0]);
try expectEqual(@as(i16, 0), i24_to_i16[0]);
try expectEqual(@as(i32, 1280), i24_to_i32[0]);

// TODO
// try expectEqual(@as(i16, 0), i32_to_i16[0]);
// try expectEqual(@as(i24, 0), i32_to_i24[0]);
try expectEqual(@as(i16, 0), i32_to_i16[0]);
try expectEqual(@as(i24, 0), i32_to_i24[0]);
}

pub inline fn signedToFloat(in: anytype, out: anytype) void {
const InputType = std.meta.Child(@TypeOf(in));
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const max: f32 = maxInt(InputType) + 1;
out_sample.* = @as(OutputType, @floatFromInt(in_sample.*)) * (1.0 / max);
pub inline fn signedToFloat(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const max: comptime_float = maxInt(SrcType) + 1;
out_sample.* = @as(DestType, @floatFromInt(in_sample.*)) * (1.0 / max);
}
}

Expand All @@ -130,16 +113,13 @@ test signedToFloat {

try expectEqual(@as(f32, 1.52587890625e-4), i16_to_f32[0]);
try expectEqual(@as(f32, 5.9604644775391e-7), i24_to_f32[0]);
// TODO
// try expectEqual(@as(f32, 0), i32_to_f32[0]);
try expectEqual(@as(f32, 2.32830643e-09), i32_to_f32[0]);
}

pub inline fn floatToUnsigned(in: anytype, out: anytype) void {
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const half = maxInt(OutputType) / 2;
out_sample.* = @intFromFloat(clamp((in_sample.* * half) + (half + 1), 0, maxInt(OutputType)));
pub inline fn floatToUnsigned(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const half = maxInt(DestType) / 2;
out_sample.* = @intFromFloat(clamp((in_sample.* * half) + (half + 1), 0, maxInt(DestType)));
}
}

Expand All @@ -149,11 +129,9 @@ test floatToUnsigned {
try expectEqual(@as(u8, 191), f32_to_u8[0]);
}

pub inline fn floatToSigned(in: anytype, out: anytype) void {
const OutputType = std.meta.Child(@TypeOf(out));

for (in, out) |*in_sample, *out_sample| {
const max = maxInt(OutputType);
pub inline fn floatToSigned(comptime SrcType: type, src: []const SrcType, comptime DestType: type, dst: []DestType) void {
for (src, dst) |*in_sample, *out_sample| {
const max = maxInt(DestType);
out_sample.* = @truncate(@as(i32, @intFromFloat(in_sample.* * max)));
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/coreaudio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ fn createStreamDesc(format: main.Format, sample_rate: u24, ch_count: usize) !c.A
.i32 => c.kAudioFormatFlagIsSignedInteger,
.f32 => c.kAudioFormatFlagIsFloat,
.u8 => return error.IncompatibleDevice,
.i24_4b => return error.IncompatibleDevice,
},
.mBytesPerPacket = format.frameSize(@intCast(ch_count)),
.mFramesPerPacket = 1,
Expand All @@ -726,7 +725,6 @@ fn createStreamDesc(format: main.Format, sample_rate: u24, ch_count: usize) !c.A
.i32 => 32,
.f32 => 32,
.u8 => unreachable,
.i24_4b => unreachable,
},
.mReserved = 0,
};
Expand All @@ -737,7 +735,3 @@ fn createStreamDesc(format: main.Format, sample_rate: u24, ch_count: usize) !c.A

return desc;
}

test "reference declarations" {
std.testing.refAllDeclsRecursive(@This());
}
4 changes: 0 additions & 4 deletions src/dummy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,3 @@ pub const Recorder = struct {
fn freeDevice(allocator: std.mem.Allocator, device: main.Device) void {
allocator.free(device.channels);
}

test "reference declarations" {
std.testing.refAllDeclsRecursive(@This());
}
4 changes: 0 additions & 4 deletions src/jack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,3 @@ pub fn freeDevice(allocator: std.mem.Allocator, device: main.Device) void {
allocator.free(device.id);
allocator.free(device.channels);
}

test "reference declarations" {
std.testing.refAllDeclsRecursive(@This());
}
Loading

0 comments on commit 0a6bb12

Please sign in to comment.