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

Fixes for modern V #3

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 LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2019 Abdullah Atta
Copyright (c) 2024 Karol Zimmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ A crazy simple library for reading/writing WAV files written in V!

## Installation:

Install using `vpkg`

```bash
vpkg get https://github.com/thecodrr/vave
```

Install using `V`'s builtin `vpm` (you will need to import the module with: `import thecodrr.vave` with this method of installation):

```shell
Expand Down Expand Up @@ -140,6 +134,7 @@ And **[follow](https://github.com/thecodrr)** me for my next creations! 🤩
MIT License

Copyright (c) 2019 Abdullah Atta
Copyright (c) 2024 Karol Zimmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 13 additions & 14 deletions constants.v
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module vave

const (
WAV_RIFF_CHUNK_ID = 'RIFF'.str
WAV_FORMAT_CHUNK_ID = 'fmt '.str
WAV_FACT_CHUNK_ID = 'fact'.str
WAV_DATA_CHUNK_ID = 'data'.str
WAVE_ID = 'WAVE'.str
WAV_RIFF_HEADER_SIZE = u32(8)
DEFAULT_SUB_FORMAT = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
)
const wav_riff_chunk_id = 'RIFF'.str
const wav_format_chunk_id = 'fmt '.str
const wav_fact_chunk_id = 'fact'.str
const wav_data_chunk_id = 'data'.str
const wave_id = 'WAVE'.str
const wav_riff_header_size = u32(8)
const default_sub_format = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38,
0x9b, 0x71]

pub enum Formats {
pcm = 0x0001,
ieee = 0x0003,
alaw = 0x0006,
mulaw = 0x0007,
pcm = 0x0001
ieee = 0x0003
alaw = 0x0006
mulaw = 0x0007
extensible = 0xfffe
}
}
24 changes: 15 additions & 9 deletions header.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct WavDataChunk{
fn (w &WavFile) parse_format_chunk() WavFormatChunk {
chunk := WavFormatChunk{}

if !w.parse_chunk_header(&chunk) && !compare(&chunk.id, WAV_FORMAT_CHUNK_ID) {
if !w.parse_chunk_header(&chunk) && !compare(&chunk.id, wav_format_chunk_id) {
panic("Couldn't find the format chunk.")
}

Expand All @@ -67,9 +67,13 @@ fn (w &WavFile) parse_format_chunk() WavFormatChunk {
panic("This wav file has more than 2 channels but isn't WAV_FORMAT_EXTENSIBLE.")
}

if !(Formats(chunk.format_tag) in [.pcm, .ieee, .alaw, .mulaw]) {
if !(unsafe {
Formats(chunk.format_tag) in [.pcm, .ieee, .alaw, .mulaw]
}) {
if chunk.ext_size != 0 {
if !(Formats(chunk.sub_format.format_code) in [.pcm, .ieee, .alaw, .mulaw]) {
if !(unsafe {
Formats(chunk.sub_format.format_code) in [.pcm, .ieee, .alaw, .mulaw]
}) {
panic("Only PCM, IEEE float and log-PCM log files are accepted.")
}
} else {
Expand All @@ -82,30 +86,30 @@ fn (w &WavFile) parse_format_chunk() WavFormatChunk {

fn (w &WavFile) parse_master_chunk() WavMasterChunk {
chunk := WavMasterChunk{}
if !w.parse_chunk_header(&chunk) || !compare(&chunk.id, WAV_RIFF_CHUNK_ID) || !w.read_u32(&chunk.wave_id) || !compare(&chunk.wave_id, WAVE_ID) {
if !w.parse_chunk_header(&chunk) || !compare(&chunk.id, wav_riff_chunk_id) || !w.read_u32(&chunk.wave_id) || !compare(&chunk.wave_id, wave_id) {
panic("Couldn't find the RIFF chunk. This is probably not a WAVE file.")
}
return chunk
}

fn (w &WavFile) parse_fact_chunk() WavFactChunk {
chunk := WavFactChunk{}
if w.parse_chunk_header(&chunk) && compare(&chunk.id, WAV_FACT_CHUNK_ID) && w.parse_chunk_body(&chunk, chunk.size) {
if w.parse_chunk_header(&chunk) && compare(&chunk.id, wav_fact_chunk_id) && w.parse_chunk_body(&chunk, chunk.size) {
return chunk
}
return chunk
}

fn (w mut WavFile) parse_header() bool {
fn (mut w WavFile) parse_header() bool {
w.chunk = w.parse_master_chunk()
w.chunk.format_chunk = w.parse_format_chunk()
w.chunk.fact_chunk = w.parse_fact_chunk()
if compare(&w.chunk.fact_chunk.id, WAV_DATA_CHUNK_ID) {
if compare(&w.chunk.fact_chunk.id, wav_data_chunk_id) {
w.chunk.data_chunk.id = w.chunk.fact_chunk.id
w.chunk.data_chunk.size = w.chunk.fact_chunk.size
w.chunk.fact_chunk.size = 0
} else {
for !compare(&w.chunk.data_chunk.id, WAV_DATA_CHUNK_ID) && !w.eof() {
for !compare(&w.chunk.data_chunk.id, wav_data_chunk_id) && !w.eof() {
w.read_u32(&w.chunk.data_chunk.id)
}
if w.eof() {
Expand All @@ -130,7 +134,9 @@ fn (w &WavFile) parse_chunk_body(chunk voidptr, size u32) bool {
fn compare(a voidptr, b byteptr) bool {
data := byteptr(a)
for i in 0..4 {
if byte(data[i]) != b[i] {return false}
if unsafe { byte(data[i]) != b[i] } {
return false
}
}
return true
}
6 changes: 3 additions & 3 deletions io.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn (w &WavFile) read_u16(buf voidptr) bool {
}

fn (w &WavFile) read_into_struct(c voidptr, skip int, size u32) bool {
return w.read_bytes(*byte(c) + skip, int(size))
return w.read_bytes(unsafe { voidptr(int(c) + skip) }, int(size))
}

/* fn (w &WavFile) pos() i64 {
Expand All @@ -28,5 +28,5 @@ fn (w &WavFile) read_into_struct(c voidptr, skip int, size u32) bool {
} */

fn (w &WavFile) eof() bool {
return feof(w.fp) > -1 || ftell(w.fp) == int(w.get_header_size() + w.chunk.data_chunk.size)
}
return C.feof(w.fp) > -1 || C.ftell(w.fp) == int(w.get_header_size() + w.chunk.data_chunk.size)
}
32 changes: 16 additions & 16 deletions reader.v
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
module vave

//TODO implement different reading functions like mono, stereo etc.
// TODO implement different reading functions like mono, stereo etc.

//read_sample reads one sample from the audio stream
//it can be used for streaming etc. The returned data
//must be freed manually or it will cause a memory leak.
// read_sample reads one sample from the audio stream
// it can be used for streaming etc. The returned data
// must be freed manually or it will cause a memory leak.
pub fn (w &WavFile) read_sample() byteptr {
return w.read_samples(1)
}

//read_raw reads all the audio data from the audio stream
//the data is put into w.data instead of returning it
//and automatically frees it on w.close()
// read_raw reads all the audio data from the audio stream
// the data is put into w.data instead of returning it
// and automatically frees it on w.close()
pub fn (w &WavFile) read_raw() byteptr {
return w.read_samples(int(w.total_samples()))
}

//read_samples reads multiple samples from the audio stream
//the returned data must be freed manually or it will cause a memory leak.
// read_samples reads multiple samples from the audio stream
// the returned data must be freed manually or it will cause a memory leak.
pub fn (w &WavFile) read_samples(count int) byteptr {
if w.mode in ['wb', 'wbx', 'ab'] {
panic("File was opened in wrong mode.")
panic('File was opened in wrong mode.')
}
if w.chunk.format_chunk.format_tag == u16(Formats.extensible) {
println("warn: EXTENSIBLE format is not supported.")
println('warn: EXTENSIBLE format is not supported.')
}
total_samples := int(w.num_channels()) * count * int(w.sample_size())
data := malloc(total_samples)
data := unsafe { malloc(total_samples) }
C.fread(data, w.sample_size(), int(w.num_channels()) * count, w.fp)
if ferror(w.fp) > 0 {
panic("Failed to read the data chunk.")
}
if w.fp == C.NULL {
panic('Failed to read the data chunk.')
}
return data
}
}
2 changes: 1 addition & 1 deletion v.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Module {
name: 'vave'
version: '0.0.2'
version: '0.1.0'
deps: []
}
Loading