summaryrefslogtreecommitdiffstats
path: root/examples/playtofile_main.cc
blob: c7f85fd970b8cde73779dcda6c70b858d3f9bbcd (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
136
137
138
139
140
141
142
143
144
145
146
147
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#include "playtofile.h"
#include "kmedia2.h"
#include "debug.h"
#include "dynamicrequest.h"
#include "connect.h"
#include "flowsystem.h"
#include <ctype.h>
#include <iostream>

using namespace Arts;
using namespace std;

/* copypasted from simplesoundserver_impl.cc */
PlayObject createPlayObject(const string& filename)
{
	string objectType = "";

	/*
	 * figure out extension (as lowercased letters)
	 */
	string extension = "";
	bool extensionok = false;
	string::const_reverse_iterator i;
	for(i = filename.rbegin(); i != filename.rend() && !extensionok; i++)
	{
		if(*i == '.')
			extensionok = true;
		else
			extension = (char)tolower(*i) + extension;
	}

	/*
	 * query trader for PlayObjects which support this
	 */
	if(extensionok)
	{
		arts_debug("search playobject, extension = %s",extension.c_str());

		TraderQuery query;
		query.supports("Interface","Arts::PlayObject");
		query.supports("Extension",extension);

		vector<TraderOffer> *offers = query.query();
		if(!offers->empty())
			objectType = offers->front().interfaceName();	// first offer

		delete offers;
	}

	/*
	 * create a PlayObject and connect it
	 */
	if(!objectType.empty())
	{
		arts_debug("creating %s to play file", objectType.c_str());

		PlayObject result = SubClass(objectType);
		if(result.loadMedia(filename))
		{
			result._node()->start();
			return result;
		}
		else arts_warning("couldn't load file %s", filename.c_str());
	}
	else arts_warning("file format extension %s unsupported",extension.c_str());

	return PlayObject::null();
}

/*
 * This is an example for dumping things to a file. It demonstates, that
 *
 * a) you can use all aRts objects in a non-realtime environment (i.e.
 *    the time in the PlayObject will pass much faster than the outside
 *    time when dumping things to a file)
 * 
 * b) complayObjectnents will be dynamically loaded as needed
 *
 * c) you need to do small tricks to get the signal flow going when you are
 *    not having audio modules around
 *
 * d) by implementing aRts modules, you can easily grab and insert data from/to
 *    arbitary non-aRts sources - for instance, a wave editor might implement
 *    own modules to get the wave from/to memory
 */
int main(int argc, char **argv)
{
	Dispatcher d;

	if(argc != 2 && argc != 3)
	{
		cerr << "usage: playtofile <inputfile> [ <outputfile> ]" << endl;
		exit(1);
	}
	PlayObject playObject = createPlayObject(argv[1]);
	if(playObject.isNull())
	{
		cerr << "can't read inputfile " << argv[1] << endl;
		exit(1);
	}

	playObject.play();

	PlayToFile playToFile;
	if(argc == 3) playToFile.filename(argv[2]);

	connect(playObject, "left", playToFile,"left");
	connect(playObject, "right", playToFile,"right");

	// <ignore the following lines>
	// special case when using mpeglib, do tell it not to block (internal
	// interface) - we also put an usleep here to ensure that the threads
	// and everything is fully initialized as soon as we start
	usleep(100000);
	if(playObject._base()->_isCompatibleWith("DecoderBaseObject"))
		if(!DynamicRequest(playObject).method("_set_blocking").param(true).invoke())
			cerr << "mpeglib, and blocking attribute can't be changed?" << endl;
	// </ignore>

	playToFile.start();
	while(playObject.state() != posIdle)
		playToFile.goOn();

	return 0;
}