Skip to content

Commit

Permalink
[Misc] Better project FourCC definition
Browse files Browse the repository at this point in the history
  • Loading branch information
native-m committed Oct 27, 2024
1 parent 370ad23 commit c648679
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/core/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ struct File {
inline bool is_open() const { return open_; }
};

consteval uint32_t fourcc(const char ch[5]) {
// TODO: Big endian support
return ch[0] | (ch[1] << 8) | (ch[2] << 16) | (ch[3] << 24);
}

std::filesystem::path to_system_preferred_path(const std::filesystem::path& path);
std::filesystem::path remove_filename_from_path(const std::filesystem::path& path);
void explore_folder(const std::filesystem::path& path);
Expand Down
20 changes: 8 additions & 12 deletions src/engine/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ static constexpr uint32_t project_midi_table_version = 1;
static constexpr uint32_t project_track_version = 1;
static constexpr uint32_t project_clip_version = 1;

consteval uint32_t fourcc(const char ch[5]) {
return ch[0] | (ch[1] << 8) | (ch[2] << 16) | (ch[3] << 24);
}

ProjectFileResult read_project_file(const std::filesystem::path& path, Engine& engine, SampleTable& sample_table,
MidiTable& midi_table, GuiTimeline& timeline) {
File file;
Expand Down Expand Up @@ -229,7 +225,7 @@ ProjectFileResult write_project_file(const std::filesystem::path& path, Engine&
}

PFHeader header {
.magic_numbers = 'RPBW',
.magic_numbers = fourcc("WBPR"),
.version = project_header_version,
.track_count = (uint32_t)engine.tracks.size(),
.sample_count = (uint32_t)sample_table.samples.size(),
Expand Down Expand Up @@ -263,7 +259,7 @@ ProjectFileResult write_project_file(const std::filesystem::path& path, Engine&
engine.project_info.genre = std::move(project_info.genre);

// Sample table header
if (file.write_u32('TSBW') < 4) // Magic number
if (file.write_u32(fourcc("WBST")) < 4) // Magic number
return ProjectFileResult::ErrCannotAccessFile;
if (file.write_u32(project_sample_table_version) < 4) // Version
return ProjectFileResult::ErrCannotAccessFile;
Expand All @@ -281,7 +277,7 @@ ProjectFileResult write_project_file(const std::filesystem::path& path, Engine&
idx = 0;

// Midi table header
if (file.write_u32('TMBW') < 4) // Magic number
if (file.write_u32(fourcc("WBMT")) < 4) // Magic number
return ProjectFileResult::ErrCannotAccessFile;
if (file.write_u32(project_midi_table_version) < 4) // Version
return ProjectFileResult::ErrCannotAccessFile;
Expand Down Expand Up @@ -313,7 +309,7 @@ ProjectFileResult write_project_file(const std::filesystem::path& path, Engine&

for (const auto track : engine.tracks) {
PFTrackHeader track_header {
.magic_numbers = 'RTBW',
.magic_numbers = fourcc("WBTR"),
.version = project_track_version,
.flags =
{
Expand All @@ -329,14 +325,14 @@ ProjectFileResult write_project_file(const std::filesystem::path& path, Engine&
.clip_count = (uint32_t)track->clips.size(),
};
if (file.write(&track_header, sizeof(PFTrackHeader)) < sizeof(PFTrackHeader))
return ProjectFileResult::ErrCannotAccessFile;
return ProjectFileResult::ErrEndOfFile;
if (track_header.flags.has_name)
if (file.write_array(track->name) < 4)
return ProjectFileResult::ErrCannotAccessFile;
return ProjectFileResult::ErrEndOfFile;

for (const auto clip : track->clips) {
PFClipHeader clip_header {
.magic_numbers = 'LCBW',
.magic_numbers = fourcc("WBCL"),
.version = project_clip_version,
.type = clip->type,
.flags =
Expand Down Expand Up @@ -372,7 +368,7 @@ ProjectFileResult write_project_file(const std::filesystem::path& path, Engine&
}

if (file.write(&clip_header, sizeof(PFClipHeader)) < sizeof(PFClipHeader))
return ProjectFileResult::ErrCannotAccessFile;
return ProjectFileResult::ErrEndOfFile;
if (clip_header.flags.has_name)
if (file.write_array(clip->name) < 4)
return ProjectFileResult::ErrCannotAccessFile;
Expand Down

0 comments on commit c648679

Please sign in to comment.