From f402ca837e0f75e0c54f2009de25b995736381b8 Mon Sep 17 00:00:00 2001 From: script kiddie Date: Sun, 5 Jan 2025 02:06:12 +0100 Subject: [PATCH] changed function to be more high level --- audio.go | 54 +++++++++++++++++++++++++++++++++--------------------- global.go | 13 ++++++------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/audio.go b/audio.go index d7a1d7c..e630424 100644 --- a/audio.go +++ b/audio.go @@ -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 { @@ -49,37 +54,44 @@ 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 { @@ -87,4 +99,4 @@ func (s *AudioExtractHandler) ExtractDataFromWAVAtDepth(decoder *wav.Decoder, bi } return newdata, nil -} \ No newline at end of file +} diff --git a/global.go b/global.go index 293b530..abc17f8 100644 --- a/global.go +++ b/global.go @@ -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() @@ -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 } \ No newline at end of file