SFML/bindings/python/samples/sound_capture_py3.py
LaurentGom 0e2297af28 Moved all bindings to the "bindings" sub-directory
Renamed the CSFML directory to c
Renamed the DSFML directory to d
--> bindings must now be updated to match the new organization!

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1630 4e206d99-4929-0410-ac5d-dfc041789085
2010-11-09 17:13:17 +00:00

68 lines
2.0 KiB
Python

#!/usr/bin/env python
from PySFML import sf
def Main():
# Check that the device can capture audio
if sf.SoundRecorder.CanCapture() == False:
print("Sorry, audio capture is not supported by your system")
return
# Choose the sample rate
SampleRate = int(input("Please choose the sample rate for sound capture (44100 is CD quality) : "))
# Wait for user input...
print("Press enter to start recording audio")
input()
# Here we'll use an integrated custom recorder, which saves the captured data into a sfSoundBuffer
Recorder = sf.SoundBufferRecorder()
# Audio capture is done in a separate thread, so we can block the main thread while it is capturing
Recorder.Start(SampleRate)
print("Recording... press enter to stop")
input()
Recorder.Stop()
# Get the buffer containing the captured data
Buffer = Recorder.GetBuffer()
# Display captured sound informations
print("Sound information :")
print(" " + str(Buffer.GetDuration()) + " seconds")
print(" " + str(Buffer.GetSampleRate()) + " samples / seconds")
print(" " + str(Buffer.GetChannelsCount()) + " channels")
# Choose what to do with the recorded sound data
Choice = str(input("What do you want to do with captured sound (p = play, s = save) ? "))
if Choice == 's':
# Choose the filename
Filename = str(input("Choose the file to create : "))
# Save the buffer
Buffer.SaveToFile(Filename);
else:
# Create a sound instance and play it
Sound = sf.Sound(Buffer)
Sound.Play()
# Wait until finished
while Sound.GetStatus() == sf.Sound.Playing:
# Display the playing position - I don't know how to do this in python
# std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec";
# Leave some CPU time for other threads
sf.Sleep(0.1)
# Finished !
print("Done !")
# Wait until the user presses 'enter' key
print("Press enter to exit...")
input()
return
Main()