DEV Community

Ben Santora
Ben Santora

Posted on

Julia Programming - Simple Wave Generator

Julia

using PortAudio, WAV

# Parameters
fs = 44100  # Sample rate (Hz)
duration = 2  # Duration (seconds)
f = 220  # Frequency of the sine wave (Hz)

# Generate sine wave
t = 0:1/fs:duration  # Time vector
y = 0.5 * sin.(2 * pi * f * t)  # Sine wave signal

# Save as .wav file
WAV.wavwrite(y, fs, "audio220.wav")

# Play the audio using a Windows-based audio player (like VLC or feh in WSL)
Enter fullscreen mode Exit fullscreen mode

This is a simple, enjoyable program for beginners to Julia and it's easy to change the values for different results.

I always use a Linux terminal when I'm programming. When I'm on my Windows laptop, I use WSL. WSL doesn't have access to the Windows 10 sound applications, so for this program I opted to simply have it create a .wav file. Then if I'm on Linux or Win 10, I can simply play the .wav with VLC.

You will need Julia installed on your PC and for this program, you'll need the WAV package. Because I am creating a standalone .wav file and not trying to play the created .wav file through the PC, PortAudio isn't actually used here.

Ben Santora - October 2024

Top comments (0)