Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/util/audioplayer.py
blob: ce635ccb0b4f22bf1cc5951f3b4fe1d6b1a8bd8a (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
132
133
134
135
# Copyright 2007 World Wide Workshop Foundation
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# If you find this activity useful or end up using parts of it in one of your
# own creations we would love to hear from you at info@WorldWideWorkshop.org !
#

import os
import gst
import logging

from globals import Globals

from util.decorators import Property


class AudioPlayer(object):

  def __init__(self):
    pass


  @Property
  def uri():
    def get(self): return self.__uri
    def set(self, value): 
      if value is None or not os.path.exists(value):
        logging.error('AudioPlayer - Invalid URI: %r', value)
        return
      self.__uri = value 
      size = os.path.getsize(self.__uri)
      self.pipeline.get_by_name('source').set_property('location', self.__uri)
      self.pipeline.get_by_name('source').set_property('mmapsize', size)


  @Property
  def raw():
    def get(self): 
      if self.uri is None:
        logging.error('AudioPlayer - No data')
        return None
      f = open(self.uri, 'r')
      raw = f.read()
      f.close()
      return raw
    def set(self, value): 
      name = Globals.temporary_filename()
      f = open(name, 'w')
      f.write(value)
      f.close()
      self.uri = name
      logging.debug('AudioPlayer - set_raw wrote %d bytes to %s', len(value), name)


  @Property
  def pipeline():
    def get(self):
      if self.__pipeline is None:
        self.__pipeline = self.__build_pipeline()
      return self.__pipeline


  def play(self):
    logging.debug('AudioPlayer - started playing sound')
    self.pipeline.set_state(gst.STATE_PLAYING)
    logging.debug('AudioPlayer - finished playing sound')



  def __build_pipeline(self):
    # pipeline
    pipeline = gst.Pipeline('pipeline')

    # add source
    source = gst.element_factory_make('filesrc', 'source')
    pipeline.add(source)

    # add decoder
    decoder = gst.element_factory_make('decodebin', 'decoder')
    decoder.connect("new-decoded-pad", self.__new_decoded_pad) #, converter)
    pipeline.add(decoder)
    source.link(decoder)

    # add converter
    converter = gst.element_factory_make("audioconvert", "converter")
    pipeline.add(converter)

    # add output
    sink = gst.element_factory_make('autoaudiosink', 'sink')
    pipeline.add(sink)
    converter.link(sink)

    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect('message', self.__on_audio_message)	

    return pipeline



  # callbacks ##################################################################

  def __new_decoded_pad(self, dbin, pad, islast):  #, converter)
    converter = self.pipeline.get_by_name('converter') # TODO - pass by arg
    pad.link(converter.get_pad("sink"))


  def __on_audio_message(self, bus, message):
    t = message.type
    #logging.debug('message: %r' % t)
    if t == gst.MESSAGE_EOS:
      self.pipeline.set_state(gst.STATE_NULL)
      logging.debug('AudioPlayer - EOS')
    elif t == gst.MESSAGE_ERROR:
      self.pipeline.set_state(gst.STATE_NULL)
      err, debug = message.parse_error()
      logging.debug('AudioPlayer - Error: %r %r', err, debug)


  # Not used
  def __on_source_handoff(self, source, buffer, pad):
    logging.debug('on_source_handoff(%r, %r, %r)', source, buffer, pad)