Skip to content

Commit

Permalink
Read FLAC TITLE metadata tag
Browse files Browse the repository at this point in the history
- Scan FLAC data for TITLE metadata tag and use for WAV file name, if present
  • Loading branch information
ScriptTiger committed Aug 25, 2024
1 parent 4237824 commit 4ffca93
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Argument | Description

`-` can be used in place of `<file>` to designate standard output as the destination. When piping a FLAC stream, the complete header is included. However, when piping a WAV stream, the header is unfinished until the write operation is complete, meaning you may have to skip the first 44 bytes in order for the receiving application to process the stream in real time.

Without any arguments, the embedded FLAC data will be transcoded into the working directory to a WAV file of the same name as the executable, except with the `.wav` extension. So, command-line usage is only optional and the end user can just execute the application as they would any other application for this default behavior.
Without any arguments, the embedded FLAC data will be transcoded into the working directory to a WAV file of the same name as the executable, or TITLE metadata tag if present within the FLAC, except with the `.wav` extension. So, command-line usage is only optional and the end user can just execute the application as they would any other application for this default behavior.

# Appending a FLAC file to a stand-alone FLACSFX executable, recommended for most users
Download the latest pre-built release for the intended target system:
Expand Down
1 change: 1 addition & 0 deletions flacsfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func main() {
// Display stream info and exit
if info {
os.Stdout.WriteString(
"TITLE="+wavName+"\n"+
"codec_name=flac\n"+
"codec_long_name=FLAC (Free Lossless Audio Codec)\n"+
"codec_type=audio\n"+
Expand Down
62 changes: 53 additions & 9 deletions sa.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,59 @@ func init() {
filePath, _ := os.Executable()
filePath, _ = filepath.EvalSymlinks(filePath)

// Name wav file after executable
wavName = strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))+".wav"

// Slurp file into memory
fileData, _ := os.ReadFile(filePath)

// Determine length of transcoder / start of FLAC data
// Find TITLE tag and use for file name if present
tcSize := int64(1000000)
var titleTagBuilder strings.Builder
for {
if fileData[tcSize-1] == 0x00 &&
fileData[tcSize] == 0x66 &&
fileData[tcSize+1] == 0x4c &&
fileData[tcSize+2] == 0x61 &&
fileData[tcSize+3] == 0x43 &&
fileData[tcSize+4] == 0x00 {
break
fileData[tcSize] == 0x66 &&
fileData[tcSize+1] == 0x4c &&
fileData[tcSize+2] == 0x61 &&
fileData[tcSize+3] == 0x43 &&
fileData[tcSize+4] == 0x00 {
titleTag := tcSize+32
for {
if fileData[titleTag] == 0x00 &&
fileData[titleTag+1] == 0x54 &&
fileData[titleTag+2] == 0x49 &&
fileData[titleTag+3] == 0x54 &&
fileData[titleTag+4] == 0x4C &&
fileData[titleTag+5] == 0x45 &&
fileData[titleTag+6] == 0x3D {
titleTag = titleTag+7
for {
if fileData[titleTag+1] == 0x00 {break}
titleTagBuilder.WriteByte(fileData[titleTag])
titleTag++
}
break
}
if (int(titleTag) == len(fileData)-6) ||
(fileData[titleTag] == 0x00 &&
fileData[titleTag+1] == 0x00 &&
fileData[titleTag+2] == 0x00 &&
fileData[titleTag+3] == 0x00 &&
fileData[titleTag+4] == 0x00 &&
fileData[titleTag+5] == 0x00 &&
fileData[titleTag+6] == 0x00 &&
fileData[titleTag+7] == 0x00 &&
fileData[titleTag+8] == 0x00 &&
fileData[titleTag+9] == 0x00 &&
fileData[titleTag+10] == 0x00 &&
fileData[titleTag+11] == 0x00 &&
fileData[titleTag+12] == 0x00 &&
fileData[titleTag+13] == 0x00 &&
fileData[titleTag+14] == 0x00 &&
fileData[titleTag+15] == 0x00) {
break
}
titleTag++
}
break
}
if int(tcSize) == len(fileData)-6 {
os.Stdout.WriteString("No FLAC data found.")
Expand All @@ -41,6 +78,13 @@ func init() {
tcSize++
}

// Name wav file after executable, or title tag if present
if titleTagBuilder.Len() == 0 {
wavName = strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))+".wav"
} else {
wavName = titleTagBuilder.String()+".wav"
}

// Calculate length of FLAC data
flacSize := int64(len(fileData))-tcSize

Expand Down

0 comments on commit 4ffca93

Please sign in to comment.