~ubuntu-branches/ubuntu/trusty/python-sfml/trusty

« back to all changes in this revision

Viewing changes to python/samples/sound_capture.py

  • Committer: Package Import Robot
  • Author(s): James Cowgill
  • Date: 2013-12-09 17:50:52 UTC
  • mfrom: (1.1.3) (8.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131209175052-11v6drpb6g3yksst
Tags: 1.5.1.is.1.3+dfsg-1
* New upstream version 1.3 (from python-sfml.org)
  - This is a complete rewrite of the Python bindings for SFML2, and
    the new maintainer is using a different version numbering scheme.
* Added myself to the list of uploaders
* Change package priority from extra to optional
* Bumped standards version (to 3.9.5) and debhelper compat (to 9)
* Added Python 3 and documentation packages
* Improve package description for debug packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
from PySFML import sf
4
 
 
5
 
def Main():
6
 
        # Check that the device can capture audio
7
 
        if sf.SoundRecorder.CanCapture() == False:
8
 
                print "Sorry, audio capture is not supported by your system"
9
 
                return
10
 
 
11
 
        # Choose the sample rate
12
 
        SampleRate = 0
13
 
        SampleRate = int(raw_input("Please choose the sample rate for sound capture (44100 is CD quality) : "))
14
 
 
15
 
        # Wait for user input...
16
 
        print "Press enter to start recording audio"
17
 
        raw_input()
18
 
 
19
 
        # Here we'll use an integrated custom recorder, which saves the captured data into a sfSoundBuffer
20
 
        Recorder = sf.SoundBufferRecorder()
21
 
 
22
 
        # Audio capture is done in a separate thread, so we can block the main thread while it is capturing
23
 
        Recorder.Start(SampleRate)
24
 
        print "Recording... press enter to stop"
25
 
        raw_input()
26
 
        Recorder.Stop()
27
 
 
28
 
        # Get the buffer containing the captured data
29
 
        Buffer = Recorder.GetBuffer()
30
 
 
31
 
        # Display captured sound informations
32
 
        print "Sound information :"
33
 
        print " " + str(Buffer.GetDuration())      + " seconds"
34
 
        print " " + str(Buffer.GetSampleRate())    + " samples / seconds"
35
 
        print " " + str(Buffer.GetChannelsCount()) + " channels"
36
 
 
37
 
        # Choose what to do with the recorded sound data
38
 
        Choice = str(raw_input("What do you want to do with captured sound (p = play, s = save) ? "))
39
 
 
40
 
        if Choice == 's':
41
 
                # Choose the filename
42
 
                Filename = str(raw_input("Choose the file to create : "))
43
 
 
44
 
                # Save the buffer
45
 
                Buffer.SaveToFile(Filename);
46
 
        else:
47
 
                # Create a sound instance and play it
48
 
                Sound = sf.Sound(Buffer)
49
 
                Sound.Play()
50
 
 
51
 
                # Wait until finished
52
 
                while Sound.GetStatus() == sf.Sound.Playing:
53
 
                        # Display the playing position - I don't know how to do this in python
54
 
                        # std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec";
55
 
 
56
 
                        # Leave some CPU time for other threads
57
 
                        sf.Sleep(0.1)
58
 
 
59
 
        # Finished !
60
 
        print "Done !"
61
 
 
62
 
        # Wait until the user presses 'enter' key
63
 
        print "Press enter to exit..."
64
 
        raw_input()
65
 
 
66
 
        return
67
 
 
68
 
Main()