summaryrefslogtreecommitdiffstats
path: root/cisco_ringtone_convert
blob: 4fa64b39b16b9f45ba412738677f59acb91b3ca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-

# Copyright © 2009 Jeffrey C. Ollie
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

# Based upon code originally posted by Jono Bacon to his blog:
# http://www.jonobacon.org/2006/12/27/using-gnonlin-with-gstreamer-and-python/

import sys
import optparse
import pygst
pygst.require('0.10')
import gst
import gobject

# Cisco ringtones can be at most 16080 bytes long
max_ringtone_length_bytes = 16080
ringtone_sample_rate = 8000.0
ringtone_bytes_per_sample = 1
max_ringtone_duration = max_ringtone_length_bytes / (ringtone_bytes_per_sample * ringtone_sample_rate)

class Main:
    def __init__(self, mainloop, source, destination, start, duration):
        if duration > max_ringtone_duration:
            sys.stderr.write('Cisco ring tones may be at most %4.2f seconds long, reducing duration.\n' % max_ringtone_duration)
            duration = max_ringtone_duration
            
        self.mainloop = mainloop

        self.pipeline = gst.Pipeline()
        
        bus = self.pipeline.get_bus()
        bus.add_signal_watch()
        bus.connect('message', self.handle_message)
        
        self.gnlcomposition = gst.element_factory_make('gnlcomposition')
        self.gnlcomposition.connect('pad-added', self.on_pad_added)
        self.pipeline.add(self.gnlcomposition)
        
        self.gnlfilesource = gst.element_factory_make('gnlfilesource')
        self.gnlfilesource.set_property('caps', gst.caps_from_string('audio/x-raw-int; audio/x-raw-float'))
        self.gnlfilesource.set_property('location', source)
        self.gnlfilesource.set_property('start', 0 * gst.SECOND)
        self.gnlfilesource.set_property('duration', int(duration * gst.SECOND))
        self.gnlfilesource.set_property('media-start', int(start * gst.SECOND))
        self.gnlfilesource.set_property('media-duration', int(duration * gst.SECOND))
        
        self.gnlcomposition.add(self.gnlfilesource)
        
        self.audioconvert = gst.element_factory_make('audioconvert')
        self.pipeline.add(self.audioconvert)
        
        self.audioresample = gst.element_factory_make('audioresample')
        self.pipeline.add(self.audioresample)
        self.audioconvert.link(self.audioresample)
        
        self.mulawenc = gst.element_factory_make('mulawenc')
        self.pipeline.add(self.mulawenc)
        self.audioresample.link(self.mulawenc)
        
        self.capsfilter = gst.element_factory_make('capsfilter')
        self.capsfilter.set_property('caps',
                                     gst.caps_from_string('audio/x-mulaw,rate=8000,channels=1'))
        self.pipeline.add(self.capsfilter)
        self.mulawenc.link(self.capsfilter)
        
        self.filesink = gst.element_factory_make('filesink')
        self.filesink.set_property('location', destination)
        self.pipeline.add(self.filesink)
        self.capsfilter.link(self.filesink)
        
        self.pipeline.set_state(gst.STATE_PLAYING)
        
    def on_pad_added(self, element, pad):
        if element == self.gnlcomposition:
            pad.link(self.audioconvert.get_compatible_pad(pad, pad.get_caps()))

    def handle_message(self, bus, message):
        if message.type == gst.MESSAGE_EOS:
            self.pipeline.set_state(gst.STATE_NULL)
            self.mainloop.quit()
            
        elif message.type == gst.MESSAGE_ERROR:
            error, debug = message.parse_error()
            sys.stderr.write('Error %s: %s\n' % (error, debug))
            self.pipeline.set_state(gst.STATE_NULL)
            self.mainloop.quit()

parser = optparse.OptionParser(version='0.2')

parser.add_option('-s', '--start',
                  action = 'store',
                  type = 'float',
                  dest = 'start',
                  default = 0.0,
                  help = 'Position in the audio file to start extracting ring tone from.',
                  metavar = 'SECONDS')

parser.add_option('-d', '--duration',
                  action = 'store',
                  type = 'float',
                  dest = 'duration',
                  default = max_ringtone_duration,
                  help = 'Duration of ring tone to extract from the audio file.',
                  metavar = 'SECONDS')

options, args = parser.parse_args()

if len(args) != 2:
    sys.stderr.write('Must specify a source and a destination!\n')
    sys.exit(1)

gobject.threads_init()
mainloop = gobject.MainLoop()
start=Main(mainloop, args[0], args[1], options.start, options.duration)
mainloop.run()