Skip to content

Commit

Permalink
changed function to be more high level
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-mescudi committed Jan 5, 2025
1 parent b789c97 commit f402ca8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
54 changes: 33 additions & 21 deletions audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,47 @@ 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) {
func (s *AudioEmbedHandler) EmbedDataIntoWAVWithDepth(audioFilename, outputFilename string, data []byte, bitDepth uint8) ( error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}
return ErrDepthOutOfRange
}

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

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

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

return buffer, nil
err = WriteAudioFile(outputFilename, decoder, buffer)
if err != nil {
return err
}

return 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) {
func (s *AudioExtractHandler) ExtractDataFromWAVWithDepth(audioFilename string, bitDepth uint8) ([]byte, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}

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

data := u.ExtractDataWithDepthAudio(buffer, bitDepth)
newdata, err := c.DecompressZSTD(data)
if err != nil {
Expand All @@ -49,42 +54,49 @@ func (s *AudioExtractHandler) ExtractDataFromWAVWithDepth(decoder *wav.Decoder,
}

// 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) {
func (s *AudioEmbedHandler) EmbedDataIntoWAVAtDepth(audioFilename, outputFilename string, data []byte, bitDepth uint8) error {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}
return ErrDepthOutOfRange
}

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

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

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

return buffer, nil
err = WriteAudioFile(outputFilename, decoder, buffer)
if err != nil {
return err
}

return 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) {
func (s *AudioExtractHandler) ExtractDataFromWAVAtDepth(audioFilename string, bitDepth uint8) ([]byte, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}

}

decoder := GetAudioData(audioFilename)
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
}
}
13 changes: 6 additions & 7 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,11 @@ func GetAudioData(file string) *wav.Decoder {
}

// WriteAudioFile writes the decoded and modified data to a new WAV file
func WriteAudioFile(fileName string, decoder *wav.Decoder, buffer *audio.IntBuffer) {
func WriteAudioFile(fileName string, decoder *wav.Decoder, buffer *audio.IntBuffer) (error) {
// 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
return fmt.Errorf("Error creating output file: %e\n", err)
}
defer outFile.Close()

Expand All @@ -164,13 +163,13 @@ func WriteAudioFile(fileName string, decoder *wav.Decoder, buffer *audio.IntBuff

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

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

return nil
}

0 comments on commit f402ca8

Please sign in to comment.