Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a Sound file with a 15Khz tone [closed]

Tags:

python

c

audio

mp3

I'm playing around with high-pitched sounds. I'd like to generate an MP3 file with a 1 second 15Khz burst. Is there a simple way to do this from C or Python? I don't want to use MATLAB.

like image 434
vy32 Avatar asked Dec 16 '11 02:12

vy32


2 Answers

You could use Python's wave module to create a wave file which you could then compress to MP3. To create a one second 15khz sine wave:

import math
import wave
import struct

nchannels = 1
sampwidth = 2
framerate = 44100
nframes = 44100
comptype = "NONE"
compname = "not compressed"
amplitude = 4000
frequency = 15000

wav_file = wave.open('15khz_sine.wav', 'w')
wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
for i in xrange(nframes):
    sample = math.sin(2*math.pi*frequency*(float(i)/framerate))*amplitude/2
    wav_file.writeframes(struct.pack('h', sample))
wav_file.close()
like image 197
zeekay Avatar answered Oct 21 '22 02:10

zeekay


I would break this into 2 pieces:

  1. Create a wave file using a C++ library(like libsndfile library)
  2. Convert the wave file to mp3 using a utility (like lame). This is a command line tool which can be called from your C program as well. see -t for converting wave to mp3.

One thing to note is 15KHz is very high frequency to be heard by human and I guess most of speakers are not capable of playing it as it is beyond cutoff frequency of them. So don't be surprised if you don't hear the result.

like image 25
Kamyar Souri Avatar answered Oct 21 '22 02:10

Kamyar Souri