August'24: Kamaelia is in maintenance mode and will recieve periodic updates, about twice a year, primarily targeted around Python 3 and ecosystem compatibility. PRs are always welcome. Latest Release: 1.14.32 (2024/3/24)

Cookbook Example

How can I...?

Example 12 : Simple TCP based streamer that repeatedly streams (3 times) the same audio file. The reason it's the same file is because it's configured with a playlist that is created: [ shortfile, shortfile, shortfile ]. If this was [ song_one, song_two, song_three ], then this would be a simple streamer that had a playlist of 3 songs played repeatedly. Something that does this is often referred to as a carousel. Components used:SimpleServer, JoinChooserToCarousel, FixedRateControlledReusableFileReader, ForwardIteratingChooser, pipeline

#!/usr/bin/python

from Kamaelia.Chassis.ConnectedServer import SimpleServer
from Kamaelia.Chassis.Prefab import JoinChooserToCarousel
from Kamaelia.File.Reading import FixedRateControlledReusableFileReader
from Kamaelia.Util.PipelineComponent import pipeline
from Kamaelia.Util.Graphline import Graphline
from Kamaelia.Util.Chooser import ForwardIteratingChooser

shortfile = "/opt/kde3/share/sounds/KDE_Startup_2.ogg"

FILES_TO_STREAM = [ shortfile, shortfile, shortfile ]# [  file_to_stream, file_to_stream2 ]
BITRATE         = 800000 # 38000
CHUNKSIZEBYTES  = 512
SERVERPORT      = 1500

def MultiFileReaderProtocol(filenames, bitrate, chunksizebytes):
    def protocolFactory(*argv, **argd):
        return JoinChooserToCarousel(
            ForwardIteratingChooser(filenames),
            FixedRateControlledReusableFileReader(readmode="bytes",
                                               rate=bitrate/8,
                                               chunksize=chunksizebytes)
          )
    return protocolFactory

if __name__ == '__main__':
   filereader = MultiFileReaderProtocol( FILES_TO_STREAM, BITRATE, CHUNKSIZEBYTES)
   server     = SimpleServer( protocol = filereader, port = SERVERPORT ).run()

Source: Examples/example12/SimpleMultiFileStreamer.py