Skip to content

Commit

Permalink
feat: add raw_to_wav.py
Browse files Browse the repository at this point in the history
  • Loading branch information
druskus20 committed Nov 16, 2024
1 parent 1e7fc50 commit 1fe8524
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ export LD_LIBRARY_PATH=/usr/local/lib

```bash
cargo run --example simple
cargo run --example encode -- test.txt # creates test.txt
cargo run --example decode -- test.txt # reads test.txt
cargo run --example encode -- test.txt # creates test.raw
cargo run --example decode -- test.txt # reads test.raw
```

When opening these raw files with an audio software like Audacity, it will most
likely not work as intended. A script like
[raw_to_wav.py](./examples/raw_to_wav.py) can be used to convert the raw file
to a wav file.

## Tips on debugging memory leaks

Expand Down
25 changes: 25 additions & 0 deletions examples/raw_to_wav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import wave
import numpy as np

# File paths
raw_file_path = "timecode.raw" # Path to the raw audio file
output_wav_path = "timecode.wav" # Path to save the WAV file

# Parameters for the raw file format (assuming 16-bit mono PCM, 44.1kHz sample rate)
sample_rate = 48000 # Samples per second (44.1 kHz)
num_channels = 1 # Mono audio
sample_width = 1 # 16-bit samples (2 bytes per sample)

# Open the raw audio file and read the data
with open(raw_file_path, "rb") as raw_file:
raw_samples = np.frombuffer(raw_file.read(), dtype=np.int16)

# Create and write the WAV file
with wave.open(output_wav_path, "wb") as wav_file:
wav_file.setnchannels(num_channels)
wav_file.setsampwidth(sample_width)
wav_file.setframerate(sample_rate)
wav_file.writeframes(raw_samples.tobytes())

print(f"WAV file saved as {output_wav_path}")

0 comments on commit 1fe8524

Please sign in to comment.