Skip to content

Commit

Permalink
Started implementation of audio loading from SKAP file (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Jun 20, 2024
1 parent cc4c5e7 commit 9fc2329
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/sk_assetpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

typedef enum {
SK_ASSETPACK_BLOB_KIND_IMAGE,
SK_ASSETPACK_BLOB_KIND_AUDIO,
SK_ASSETPACK_BLOB_KIND_COUNT
} sk_assetpack_blob_kind;

Expand Down
31 changes: 31 additions & 0 deletions include/sk_audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <raylib.h>
#include <sk_assetpack.h>

void sk_audio_init(sk_assetpack *ap);

Sound sk_audio_load(const char *filename);

Music sk_audio_stream_load(const char *filename);
47 changes: 47 additions & 0 deletions src/sk_assetpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sk_log.h>
#include <sk_assetpack.h>
#include <skap_idx_image.h>
#include <skap_idx_audio.h>

u8 sk_assetpack_open(sk_assetpack *ap) {
if (!ap) {
Expand Down Expand Up @@ -69,6 +70,26 @@ u8 sk_assetpack_open(sk_assetpack *ap) {
return 0;
}
}
ap->idxs[SK_ASSETPACK_BLOB_KIND_AUDIO] = sk_hashmap_create(ap->header.idx_audio_count, sizeof(skap_idx_audio));
for (usz i = 0; i < ap->header.idx_audio_count; ++i) {
skap_idx_audio audio_idx = {0};
if (fread(&audio_idx, sizeof(skap_idx_audio), 1, fd) != 1) {
SK_LOG_ERROR("sk_assetpack_open :: unable to load idx_audio #%zu", i + 1);
sk_hashmap_destroy(&ap->idxs[SK_ASSETPACK_BLOB_KIND_AUDIO]);
*ap = (sk_assetpack) {0};
ap = 0;
fclose(fd);
return 0;
}
if (!sk_hashmap_set(&ap->idxs[SK_ASSETPACK_BLOB_KIND_AUDIO], audio_idx.metadata.name, &audio_idx)) {
SK_LOG_ERROR("sk_assetpack_open :: unable to insert idx_audio #%zu in hashmap", i + 1);
sk_hashmap_destroy(&ap->idxs[SK_ASSETPACK_BLOB_KIND_AUDIO]);
*ap = (sk_assetpack) {0};
ap = 0;
fclose(fd);
return 0;
}
}
ap->blob_section_start_pos = ftell(fd);
ap->fd = fd;
SK_LOG_INFO("sk_assetpack_open :: `assets.skap` loaded successfully");
Expand Down Expand Up @@ -118,6 +139,32 @@ u8 sk_assetpack_lookup(sk_assetpack *ap, sk_assetpack_blob_kind kind, const char
}
fseek(ap->fd, ap->blob_section_start_pos, SEEK_SET);
}
else if (kind == SK_ASSETPACK_BLOB_KIND_AUDIO) {
skap_idx_audio audio_idx = {0};
if (!sk_hashmap_get(&ap->idxs[kind], name, &audio_idx)) {
SK_LOG_ERROR("sk_assetpack_lookup :: unable to retrieve idx_audio `%s` from hashmap", name);
return 0;
}
fseek(ap->fd, audio_idx.blob_offset, SEEK_SET);
((Wave *) out_obj)->frameCount = audio_idx.metadata.frame_count;
((Wave *) out_obj)->sampleRate = audio_idx.metadata.sample_rate;
((Wave *) out_obj)->sampleSize = audio_idx.metadata.sample_size;
((Wave *) out_obj)->channels = audio_idx.metadata.channels;
((Wave *) out_obj)->data = malloc(sizeof(u8) * audio_idx.blob_size);
if (fread(((Wave *) out_obj)->data, sizeof(u8), audio_idx.blob_size, ap->fd) != audio_idx.blob_size) {
SK_LOG_ERROR("sk_assetpack_lookup :: unable to load audio blob (`%s`) from SKAP file", name);
free(((Wave *) out_obj)->data);
*((Wave *) out_obj) = (Wave) {0};
return 0;
}
if (!IsWaveReady(*((Wave *) out_obj))) {
SK_LOG_ERROR("sk_assetpack_lookup :: resulting parsed audio (`%s`) is not valid");
free(((Wave *) out_obj)->data);
*((Wave *) out_obj) = (Wave) {0};
return 0;
}
fseek(ap->fd, ap->blob_section_start_pos, SEEK_SET);
}
else {
SK_LOG_ERROR("sk_assetpack_lookup :: `kind` needs to be valid");
return 0;
Expand Down
55 changes: 55 additions & 0 deletions src/sk_audio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <sk_log.h>
#include <sk_audio.h>

static sk_assetpack *skap = 0;

void sk_audio_init(sk_assetpack *ap) { skap = ap; }

Sound sk_audio_load(const char *filename) {
#ifndef NDEBUG
return LoadSound(filename);
#else
if (!skap) SK_LOG_ERROR("sk_audio_load :: SKAP was not initialized (maybe forgot to exec `sk_audio_init` ?)");
Wave wave = {0};
if (!sk_assetpack_lookup(skap, SK_ASSETPACK_BLOB_KIND_AUDIO, filename, &wave)) {
SK_LOG_ERROR("sk_audio_load :: unable to retrieve audio from SKAP file");
}
Sound sound = LoadSoundFromWave(wave);
UnloadWave(wave);
return sound;
#endif
}

Music sk_audio_stream_load(const char *filename) {
#ifndef NDEBUG
return LoadMusicStream(filename);
#else
if (!skap) SK_LOG_ERROR("sk_audio_stream_load :: SKAP was not initialized (maybe forgot to exec `sk_audio_init` ?)");
Wave wave = {0};
if (!sk_assetpack_lookup(skap, SK_ASSETPACK_BLOB_KIND_AUDIO, filename, &wave)) {
SK_LOG_ERROR("sk_audio_stream_load :: unable to retrieve audio from SKAP file");
}
return LoadMusicStreamFromMemory(".mp3", wave.data, wave.frameCount * wave.channels * (wave.sampleSize / 8));
#endif
}
3 changes: 2 additions & 1 deletion tools/skap/include/skap_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ typedef struct {
u8 fmt_ver;
u64 build_ver;
usz idx_image_count;
usz idx_audio_count;
} skap_header;

skap_header skap_header_create(usz idx_image_count);
skap_header skap_header_create(usz idx_image_count, usz idx_audio_count);

u8 skap_header_append(FILE *fd, skap_header *h);
2 changes: 1 addition & 1 deletion tools/skap/src/skap.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main(void) {
skap_idx_image_loadall(imgs, img_paths, IMG_COUNT);
skap_idx_audio_loadall(audios, audio_paths, AUDIO_COUNT);
FILE *fd = skap_file_create();
skap_header header = skap_header_create(IMG_COUNT);
skap_header header = skap_header_create(IMG_COUNT, AUDIO_COUNT);
if (!skap_header_append(fd, &header)) skap_return_defer(1);
for (usz i = 0; i < IMG_COUNT; ++i) {
img_idxs[i] = skap_idx_image_create(img_paths[i], &imgs[i]);
Expand Down
5 changes: 3 additions & 2 deletions tools/skap/src/skap_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ static u64 compute_build_ver(void) {
return build_ver;
}

skap_header skap_header_create(usz idx_image_count) {
skap_header skap_header_create(usz idx_image_count, usz idx_audio_count) {
return (skap_header) {
.signature = { 'S', 'K', 'A', 'P' },
.fmt_ver = 1,
.build_ver = compute_build_ver(),
.idx_image_count = idx_image_count
.idx_image_count = idx_image_count,
.idx_audio_count = idx_audio_count
};
}

Expand Down

0 comments on commit 9fc2329

Please sign in to comment.