ffmpeg
ffmpeg can do it, as usual.
Create a 2 second mono 1000 Hz sinusoidal out.wav
sound file:
sudo apt-get install ffmpeg
ffmpeg -f lavfi -i "sine=f=1000:d=2" out.wav
Stereo instead with -ac 2
:
ffmpeg -f lavfi -i "sine=f=1000:d=2" -ac 2 out.wav
The file will be 2x as large, and ffprobe
will say it has 2 channels
instead of 1 channel
.
Generate 2 seconds of 500 Hz on left and 1000 Hz on right, related question: https://stackoverflow.com/questions/59551013/how-to-generate-stereo-sine-wave-using-ffmpeg
ffmpeg -filter_complex '
sine=f=500[0];
sine=f=1000[1];
[0][1]amerge,atrim=end=2
' out.wav
Play the 1000 Hz for 2 seconds without creating a file:
ffplay -autoexit -nodisp -f lavfi -i 'sine=f=1000:d=2'
Play 1000 Hz forever until you go mad:
ffplay -nodisp -f lavfi -i "sine=f=1000"
Produce a 2 second mixture/overlay of 500 Hz and 1000 Hz on both channels, related question: https://stackoverflow.com/questions/14498539/how-to-overlay-downmix-two-audio-files-using-ffmpeg
ffplay -nodisp -autoexit -f lavfi -i '
sine=f=500[0];
sine=f=1000[1];
[0][1]amix=inputs=2, atrim=end=2
'
Produce 2 seconds of 500 Hz followed by 2 seconds of 1000 Hz:
ffplay -nodisp -autoexit -f lavfi -i '
sine=f=500:d=2[0];
sine=f=1000:d=2[1];
[0][1]concat=n=2:v=0:a=1
'
2 seconds of 250 Hz + 500 Hz followed by two seconds of 1000 Hz + 2000 Hz:
ffplay -nodisp -autoexit -f lavfi -i '
sine=f=250[0];
sine=f=500[1];
sine=f=1000[2];
sine=f=2000[3];
[0][1]amix=inputs=2, atrim=end=2[01];
[2][3]amix=inputs=2, atrim=end=2[23];
[01][23]concat=n=2:v=0:a=1[out];
'
Frequency sweep from 0 to 20 kHZ over 10 seconds, main question: https://superuser.com/questions/1156429/how-to-generate-a-time-dependent-frequency-wave/1156468#1156468
ffplay -autoexit -f lavfi -i "aevalsrc='sin(2000*t*2*PI*t)':d=10"
Documentation:
The other section sunder Audio sources document other useful sound generation algorithms in addition to sine
, e.g.:
anoisesrc
: noises of several colors, e.g. white, pink, brown
aevalsrc
takes arbitrary mathematical expressions, and should therefore be able to produce triangular waveforms (TODO expression)
Bibliography:
Tested on ffmpeg 6.0, Ubuntu 23.10.
Minimal C audio generation example without extra libraries
Just for fun: https://stackoverflow.com/questions/732699/how-is-audio-represented-with-numbers-in-computers/36510894#36510894