Skip to content

Commit

Permalink
added basic functions for audio
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-mescudi committed Jan 5, 2025
1 parent afa3779 commit b789c97
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 49 deletions.
88 changes: 88 additions & 0 deletions audio.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
package stegano

import (
c "github.com/scott-mescudi/stegano/compression"
u "github.com/scott-mescudi/stegano/pkg"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
)

// EmbedDataIntoWAVWithDepth embeds compressed data into a WAV file with a specified bit depth.
func (s *AudioEmbedHandler) EmbedDataIntoWAVWithDepth(decoder *wav.Decoder, data []byte, bitDepth uint8) (*audio.IntBuffer, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}

buffer, err := decoder.FullPCMBuffer()
if err != nil {
return nil, err
}

newdata, err := c.CompressZSTD(data)
if err != nil {
return nil, err
}

buffer = u.EmbedDataWithDepthAudio(buffer, newdata, bitDepth)

return buffer, nil
}

// ExtractDataFromWAVWithDepth extracts compressed data from a WAV file with a specified bit depth.
func (s *AudioExtractHandler) ExtractDataFromWAVWithDepth(decoder *wav.Decoder, bitDepth uint8) ([]byte, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}

buffer, err := decoder.FullPCMBuffer()
if err != nil {
return nil, err
}

data := u.ExtractDataWithDepthAudio(buffer, bitDepth)
newdata, err := c.DecompressZSTD(data)
if err != nil {
return nil, err
}

return newdata, nil
}

// EmbedDataIntoWAVAtDepth embeds compressed data into a WAV file at a specified bit depth.
func (s *AudioEmbedHandler) EmbedDataIntoWAVAtDepth(decoder *wav.Decoder, data []byte, bitDepth uint8) (*audio.IntBuffer, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}

buffer, err := decoder.FullPCMBuffer()
if err != nil {
return nil, err
}

newdata, err := c.CompressZSTD(data)
if err != nil {
return nil, err
}

buffer = u.EmbedDataAtDepthAudio(buffer, newdata, bitDepth)

return buffer, nil
}

// ExtractDataFromWAVAtDepth extracts compressed data from a WAV file at a specified bit depth.
func (s *AudioExtractHandler) ExtractDataFromWAVAtDepth(decoder *wav.Decoder, bitDepth uint8) ([]byte, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}

buffer, err := decoder.FullPCMBuffer()
if err != nil {
return nil, err
}

data := u.ExtractDataAtDepthAudio(buffer, bitDepth)
newdata, err := c.DecompressZSTD(data)
if err != nil {
return nil, err
}

return newdata, nil
}
1 change: 0 additions & 1 deletion audio_core.go

This file was deleted.

49 changes: 49 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"path/filepath"

u "github.com/scott-mescudi/stegano/pkg"

"github.com/go-audio/audio"
"github.com/go-audio/wav"
)

// GetImageCapacity calculates the maximum amount of data (in bytes)
Expand Down Expand Up @@ -125,3 +128,49 @@ func DecryptData(ciphertext []byte, password string) (plaintext []byte, err erro

return u.Decrypt(password, ciphertext)
}


// GetAudioData opens the WAV file and returns a decoder
func GetAudioData(file string) *wav.Decoder {
f, err := os.Open(file)
if err != nil {
fmt.Println("Error opening file:", err)
return nil
}

decoder := wav.NewDecoder(f)

// Decode the WAV file header and check if it's valid
if !decoder.IsValidFile() {
fmt.Println("Invalid WAV file")
return nil
}

return decoder
}

// WriteAudioFile writes the decoded and modified data to a new WAV file
func WriteAudioFile(fileName string, decoder *wav.Decoder, buffer *audio.IntBuffer) {
// Create a new file for writing the modified WAV data
outFile, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outFile.Close()

// Create a new encoder for the output file
encoder := wav.NewEncoder(outFile, int(decoder.SampleRate), int(decoder.BitDepth), int(decoder.NumChans), 1)

// Write the modified buffer to the new file
if err := encoder.Write(buffer); err != nil {
fmt.Println("Error encoding WAV file:", err)
return
}

// Close the encoder to flush the output
if err := encoder.Close(); err != nil {
fmt.Println("Error closing encoder:", err)
return
}
}
48 changes: 0 additions & 48 deletions pkg/audio.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
package pkg

import (
"fmt"
"os"

"github.com/go-audio/audio"
"github.com/go-audio/wav"
)


// GetAudioData opens the WAV file and returns a decoder
func GetAudioData(file string) *wav.Decoder {
f, err := os.Open(file)
if err != nil {
fmt.Println("Error opening file:", err)
return nil
}

decoder := wav.NewDecoder(f)

// Decode the WAV file header and check if it's valid
if !decoder.IsValidFile() {
fmt.Println("Invalid WAV file")
return nil
}

return decoder
}

func EmbedDataAtDepthAudio(buffer *audio.IntBuffer, data []byte, depth uint8) *audio.IntBuffer {
dataBits := BytesToBinary(data)
lenBits := Int32ToBinary(int32(len(data)))
Expand Down Expand Up @@ -117,28 +94,3 @@ func ExtractDataWithDepthAudio(buffer *audio.IntBuffer, depth uint8) []byte {
return byteSlice[4:(leng*2)+1]
}

// WriteAudioFile writes the decoded and modified data to a new WAV file
func WriteAudioFile(fileName string, decoder *wav.Decoder, buffer *audio.IntBuffer) {
// Create a new file for writing the modified WAV data
outFile, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outFile.Close()

// Create a new encoder for the output file
encoder := wav.NewEncoder(outFile, int(decoder.SampleRate), int(decoder.BitDepth), int(decoder.NumChans), 1)

// Write the modified buffer to the new file
if err := encoder.Write(buffer); err != nil {
fmt.Println("Error encoding WAV file:", err)
return
}

// Close the encoder to flush the output
if err := encoder.Close(); err != nil {
fmt.Println("Error closing encoder:", err)
return
}
}

0 comments on commit b789c97

Please sign in to comment.