this repo has no description
1/* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
8
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
23
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
29
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/*=============================================================================
39 playaudiofile.cpp
40
41=============================================================================*/
42
43#include "CAChannelMappingPlayer.h"
44#include "CAXException.h"
45
46#if TARGET_OS_MAC
47 #include <unistd.h>
48#else
49 #include <QTML.h>
50 #include "CAWindows.h"
51#endif
52
53static void usage()
54{
55 fprintf(stderr,
56 "Usage:\n"
57 "%s [option...] audio_file\n\n"
58 "Options: (may appear before or after arguments)\n"
59 " {-a --all}\n"
60 " for files with >2 channels, play each pair of channels in succession\n"
61 " {-f --frame} FRAME\n"
62 " starting sample frame number\n"
63 " {-s --seconds} START_SECONDS END_SECONDS\n"
64 " segment of file to play, in seconds\n"
65#if !TARGET_OS_WIN32
66 , getprogname());
67#else
68 , "afplay");
69#endif
70 exit(1);
71}
72
73static void Play(CAChannelMappingPlayer &player, SInt64 startFrame, SInt64 endFrame)
74{
75 CAAudioFile &file = player.GetFile();
76 if (startFrame < 0) {
77 startFrame = 0;
78 }
79 if (endFrame <= 0) {
80 SInt64 numFrames = SInt64(file.GetNumberFrames() * file.GetClientDataFormat().mSampleRate / file.GetFileDataFormat().mSampleRate);
81 endFrame = numFrames;
82 }
83 player.Start();
84 while (true) {
85 SInt64 pos = player.GetCurrentFrame();
86 //file.Tell();
87 if (pos >= endFrame)
88 break;
89 usleep(250000); // 250 ms
90 }
91 player.Stop();
92}
93
94void MissingArgument()
95{
96 fprintf(stderr, "Missing argument\n");
97 usage();
98}
99
100int main(int argc, const char *argv[])
101{
102 const char *fileToPlay = NULL;
103 bool playallpairs = false;
104 double startSeconds = -1, endSeconds = -1;
105 SInt64 startFrame = 0, endFrame = 0;
106
107 #if TARGET_OS_WIN32
108 InitializeQTML(0L);
109 #endif
110
111 for (int i = 1; i < argc; ++i) {
112 const char *arg = argv[i];
113 if (arg[0] != '-') {
114 if (fileToPlay != NULL) {
115 fprintf(stderr, "may only specify one file to play\n");
116 usage();
117 }
118 fileToPlay = arg;
119 } else {
120 arg += 1;
121 if (arg[0] == 'a' || !strcmp(arg, "-all"))
122 playallpairs = true;
123 else if (arg[0] == 'f' || !strcmp(arg, "-frame")) {
124 if (++i == argc)
125 MissingArgument();
126 arg = argv[i];
127 sscanf(arg, "%qd", &startFrame);
128 } else if (arg[0] == 's' || !strcmp(arg, "-seconds")) {
129 if (++i == argc)
130 MissingArgument();
131 arg = argv[i];
132 sscanf(arg, "%lf", &startSeconds);
133 if (i + 1 < argc && argv[i+1][0] != '-') {
134 arg = argv[++i];
135 sscanf(arg, "%lf", &endSeconds);
136 }
137 } else {
138 fprintf(stderr, "unknown argument: %s\n\n", arg - 1);
139 usage();
140 }
141 }
142 }
143
144 if (fileToPlay == NULL)
145 usage();
146
147 try {
148 const int kNumberBuffers = 3;
149 const unsigned kBufferSize = 0x1000;
150 FSRef playFSRef;
151 XThrowIfError(FSPathMakeRef((UInt8 *)fileToPlay, &playFSRef, NULL), "Input file not found");
152 CAChannelMappingPlayer player(kNumberBuffers, kBufferSize);
153 player.SetFile(playFSRef);
154 CAAudioFile &file = player.GetFile();
155 CAChannelMapper *mapper = player.GetMapper();
156 if (playallpairs && mapper == NULL)
157 playallpairs = false;
158
159 if (startSeconds >= 0) {
160 Float64 sr = file.GetClientDataFormat().mSampleRate;
161 startFrame = SInt64(startSeconds * sr);
162 if (endSeconds > startSeconds)
163 endFrame = SInt64(endSeconds * sr);
164 printf("secs: %.1f-%.1f frames: %qd-%qd\n", startSeconds, endSeconds, startFrame, endFrame);
165 }
166
167 if (playallpairs) {
168 int nChannelsInFile = file.GetFileDataFormat().NumberChannels();
169 for (int channel = 0; channel < nChannelsInFile && playallpairs; channel += 2) {
170 printf("channels %d-%d of %d\n", channel, channel+1, nChannelsInFile);
171 mapper->ResetMixer();
172 mapper->ConnectChannelToChannel(channel, 0);
173 mapper->ConnectChannelToChannel(channel+1, 1);
174 player.SetCurrentPosition(0.);
175 Play(player, startFrame, endFrame);
176 }
177 } else {
178 file.Seek(startFrame);
179 Play(player, startFrame, endFrame);
180 }
181 }
182 catch (CAXException &e) {
183 char buf[256];
184 fprintf(stderr, "Error: %s (%s)\n", e.mOperation, CAXException::FormatError(buf, sizeof(buf), e.mError));
185 return 1;
186 }
187 catch (...) {
188 fprintf(stderr, "An unknown error occurred\n");
189 return 1;
190 }
191 return 0;
192}
193