Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Generating tones and pitches ie square sine in x hz / gps lat long
-
The
wave
module will be included in 1.5, though it actually doesn't help very much on iOS because there's no way to play uncompressed .wav files (and thewave
module doesn't support compression). -
Sure about that? I just created a short pcm wav, and played it with
sound.play_effect
, seemed to work fine! -
JonB... It would be fascinating to understand how you do this if you have a code snippet that you would be willing to share.
-
See the eurosignal.py I posted above.
It wants to run as a script, so just manually set num filename and wav filename.
Then, play withplay_effect'
. I have not tried any other sample rates, or any more interesting tones, etc -
For what it's worth... Uncompressed waves work, and
play_effect
plays them without renaming to caf.# generate wav file containing sine waves import math, wave, array duration = 3 # seconds freq = 440 # of cycles per second (Hz) (frequency of the sine waves) volume = 100 # percent data = array.array('h') # signed short integer (-32768 to 32767) data sampleRate = 44100.0 # of samples per second (standard) numChan = 1 # of channels (1: mono, 2: stereo) dataSize = 2 # 2 bytes because of using signed short integers => bit depth = 16 numSamplesPerCyc = (sampleRate / freq) numSamples = int(sampleRate * duration) for i in range(numSamples): sample = 32767 * float(volume) / 100 sample *= math.sin(math.pi * 2 * (i / numSamplesPerCyc)) data.append(int(sample)) f = wave.open('440hz.wav', 'w') f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed")) f.writeframes(data.tostring()) f.close() import sound sound.play_effect('440hz.wav')
One issue, is that play_effect appears to cache files, so a new file name is needed each time if you are modifying params.
-
Interesting! To work around the
play_effect
caching, you could also do something like this:# ... import sound player = sound.Player('440hz.wav') player.play()
-
omz: do you know about the pyo module ? It's quite powerful and would be a great addition to pythonista ! You can build quite complex synths and effects with it.
Website: http://ajaxsoundstudio.com/software/pyo/
Source: https://code.google.com/p/pyo/ -
Pyo is GPL v3, and written in c... So looks like a no go.
-
JonB: isn't numpy also written in C ?
-
Thanks for the snippets above, they're really helpful!
FWIW, one can generate that test sample a lot faster using numpy as follows:
import numpy as np th=np.linspace(0, 2*np.pi*freq*duration, numSamples, endpoint=False) data = (32767*volume/100.0*np.sin(th)).astype(np.int16)