Skip to content

Commit

Permalink
Export track breaks to CUE via foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
thp committed Apr 10, 2022
1 parent ddc4a2a commit e491d67
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/cue.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ track_break_write_cue(gpointer data, gpointer user_data)

TrackBreak *track_break = data;

gchar *time = track_break_format_time(track_break, TRUE);
gchar *time = track_break_format_offset(track_break, TRUE);

fprintf(ws->fp, "TRACK %02d AUDIO\n", ws->index);
fprintf(ws->fp, "INDEX 01 %s\n", time);
Expand Down
69 changes: 38 additions & 31 deletions src/toc.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,40 +79,47 @@ int toc_read_file(const char *toc_filename, GList *breaks)
return 0;
}

int toc_write_file(const char *toc_filename, const char *wav_filename, GList *breaks, gulong total_duration)
{
struct WriteTOCForEach {
FILE *fp;
TrackBreak *next_break = NULL;
char *tocDuration;
const char *wav_filename;
};

fp = fopen(toc_filename, "w");
if(!fp) return 1;
static void
write_toc_entry(int index, gboolean write, gulong start_offset, gulong end_offset, const gchar *filename, void *user_data)
{
struct WriteTOCForEach *wtfe = user_data;

fprintf(fp, "// Generated with wavbreaker\n\nCD_DA\n");
int i = 0;
int len = g_list_length(breaks);
while (i < len) {
next_break = (TrackBreak *) g_list_nth_data(breaks, i);
TrackBreak *next_next = g_list_nth_data(breaks, i + 1);
gulong end_offset = (next_next != NULL) ? next_next->offset : total_duration;

if (next_break != NULL) {
fprintf(fp, "\n// track %02d\n", i);
fprintf(fp, "TRACK AUDIO\n");

gchar *tocTime = track_break_format_time(next_break, TRUE);

if (i != len-1) {
tocDuration = track_break_format_duration(next_break, end_offset, TRUE);
fprintf(fp, "FILE \"%s\" %s %s\n", wav_filename, tocTime, tocDuration);
g_free(tocDuration);
} else {
fprintf(fp, "FILE \"%s\" %s\n", wav_filename, tocTime);
}
g_free(tocTime);
}
i++;
fprintf(wtfe->fp, "\n// track %02d\n", index);
fprintf(wtfe->fp, "TRACK AUDIO\n");

gchar *tocTime = track_break_format_timestamp(start_offset, TRUE);
gchar *tocDuration = track_break_format_timestamp(end_offset - start_offset, TRUE);

fprintf(wtfe->fp, "FILE \"%s\" %s %s\n", wtfe->wav_filename, tocTime, tocDuration);

g_free(tocDuration);
g_free(tocTime);
}

gboolean
toc_write_file(const char *toc_filename, const char *wav_filename, GList *breaks, gulong total_duration)
{
FILE *fp = fopen(toc_filename, "w");

if (!fp) {
return FALSE;
}

fprintf(fp, "// Generated with wavbreaker\n\nCD_DA\n");

struct WriteTOCForEach wtfe = {
.fp = fp,
.wav_filename = wav_filename,
};

track_break_list_foreach(breaks, total_duration, write_toc_entry, &wtfe);

fclose(fp);
return 0;

return TRUE;
}
2 changes: 1 addition & 1 deletion src/toc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <glib.h>

int toc_read_file(const char *toc_filename, GList *breaks);
int toc_write_file(const char *toc_filename, const char *wav_filename, GList *breaks, gulong total_duration);
gboolean toc_write_file(const char *toc_filename, const char *wav_filename, GList *breaks, gulong total_duration);

#endif /* TOC_H */

33 changes: 28 additions & 5 deletions src/track_break.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

#include <stdio.h>

static gchar *
_format_time(gulong time, gboolean toc_format)
gchar *
track_break_format_timestamp(gulong time, gboolean toc_format)
{
int min = time / (CD_BLOCKS_PER_SEC * 60);
int sec = time % (CD_BLOCKS_PER_SEC * 60);
Expand All @@ -39,15 +39,15 @@ _format_time(gulong time, gboolean toc_format)
}

gchar *
track_break_format_time(TrackBreak *track_break, gboolean toc_format)
track_break_format_offset(TrackBreak *track_break, gboolean toc_format)
{
return _format_time(track_break->offset, toc_format);
return track_break_format_timestamp(track_break->offset, toc_format);
}

gchar *
track_break_format_duration(TrackBreak *track_break, gulong next_offset, gboolean toc_format)
{
return _format_time(next_offset - track_break->offset, toc_format);
return track_break_format_timestamp(next_offset - track_break->offset, toc_format);
}

/** @param str Time in MM:SS:FF format (where there are CD_BLOCKS_PER_SEC frames per second).
Expand All @@ -71,3 +71,26 @@ msf_time_to_offset(const gchar *str)

return offset;
}

typedef void (*track_break_visitor_func)(int index, gboolean write, gulong start_offset, gulong end_offset, const gchar *filename, void *user_data);

void
track_break_list_foreach(GList *list, gulong total_duration, track_break_visitor_func visitor, void *visitor_user_data)
{
int index = 0;

GList *cur = g_list_first(list);
while (cur != NULL) {
TrackBreak *track_break = cur->data;

GList *next = g_list_next(cur);
TrackBreak *next_track_break = (next != NULL) ? next->data : NULL;

gulong start_offset = track_break->offset;
gulong end_offset = (next_track_break != NULL) ? next_track_break->offset : total_duration;

visitor(index++, track_break->write, start_offset, end_offset, track_break->filename, visitor_user_data);

cur = next;
}
}
10 changes: 9 additions & 1 deletion src/track_break.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ struct TrackBreak_ {
};

gchar *
track_break_format_time(TrackBreak *track_break, gboolean toc_format);
track_break_format_timestamp(gulong time, gboolean toc_format);

gchar *
track_break_format_offset(TrackBreak *track_break, gboolean toc_format);

gchar *
track_break_format_duration(TrackBreak *track_break, gulong next_offset, gboolean toc_format);

guint
msf_time_to_offset(const gchar *str);

typedef void (*track_break_visitor_func)(int index, gboolean write, gulong start_offset, gulong end_offset, const gchar *filename, void *user_data);

void
track_break_list_foreach(GList *list, gulong total_duration, track_break_visitor_func visitor, void *visitor_user_data);
7 changes: 2 additions & 5 deletions src/wavbreaker.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ track_break_add_to_model(gpointer data, gpointer user_data)
TrackBreak *next_break = g_list_nth_data(track_break_list, g_list_index(track_break_list, track_break)+ 1);
gulong next_offset = (next_break != NULL) ? next_break->offset : sample_get_num_sample_blocks(g_sample);

gchar *time = track_break_format_time(track_break, FALSE);
gchar *time = track_break_format_offset(track_break, FALSE);
gchar *duration = track_break_format_duration(track_break, next_offset, FALSE);

gtk_list_store_insert_with_values(store, NULL, -1,
Expand Down Expand Up @@ -2923,7 +2923,6 @@ main(int argc, char *argv[])

int track_breaks_export_to_file( char* filename) {
FILE *fp = NULL;
int write_err = -1;

if( g_str_has_suffix (filename, ".txt")) {

Expand All @@ -2943,9 +2942,7 @@ int track_breaks_export_to_file( char* filename) {

} else if( g_str_has_suffix (filename, ".toc")) {

write_err = toc_write_file( filename, sample_get_basename(g_sample), track_break_list, sample_get_num_sample_blocks(g_sample));

if( write_err) {
if (!toc_write_file(filename, sample_get_basename(g_sample), track_break_list, sample_get_num_sample_blocks(g_sample))) {
popupmessage_show( main_window, _("Export failed"), _("There has been an error exporting track breaks to the TOC file."));
return -1;
}
Expand Down

0 comments on commit e491d67

Please sign in to comment.