1 /*
2 	Audio File Library
3 
4 	Copyright (C) 1998-1999, Michael Pruett <michael@68k.org>
5 	Copyright (C) 2001, Silicon Graphics, Inc.
6 
7 	This program is free software; you can redistribute it and/or modify
8 	it under the terms of the GNU General Public License as published by
9 	the Free Software Foundation; either version 2 of the License, or
10 	(at your option) any later version.
11 
12 	This program is distributed in the hope that it will be useful,
13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 	GNU General Public License for more details.
16 
17 	You should have received a copy of the GNU General Public License along
18 	with this program; if not, write to the Free Software Foundation, Inc.,
19 	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 
22 /*
23 	irixtestloop.c
24 
25 	This file reads the loop points from a file (presumably AIFF) and
26 	loops that part of the file several times.  Audio output is routed
27 	to IRIX's default audio output device.  This program will not
28 	compile on any platform other than IRIX.
29 */
30 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <dmedia/audio.h>
39 #include <dmedia/audiofile.h>
40 
41 #include "sgi.h"
42 
43 const int REPEAT_COUNT = 3;
44 
usage(void)45 void usage (void)
46 {
47 	printf("usage: irixtestloop file\n");
48 	printf("where file is of a format which contains a loop (e.g. AIFF)\n");
49 	exit(EXIT_FAILURE);
50 }
51 
main(int argc,char ** argv)52 main (int argc, char **argv)
53 {
54 	AFfilehandle	file;
55 	void		*buffer;
56 
57 	AFframecount	frameCount;
58 	int		sampleFormat, sampleWidth, channelCount;
59 	float		frameSize;
60 	double		sampleRate;
61 
62 	int		*loopids, *markids;
63 	int		i, loopCount, markCount;
64 	int		startmarkid, endmarkid;
65 	AFframecount	startloop, endloop;
66 
67 	ALport		outport;
68 	ALconfig	outportconfig;
69 
70 	if (argc < 2)
71 		usage();
72 
73 	file = afOpenFile(argv[1], "r", NULL);
74 	frameCount = afGetFrameCount(file, AF_DEFAULT_TRACK);
75 	frameSize = afGetVirtualFrameSize(file, AF_DEFAULT_TRACK, 1);
76 	channelCount = afGetVirtualChannels(file, AF_DEFAULT_TRACK);
77 	afGetVirtualSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
78 	sampleRate = afGetRate(file, AF_DEFAULT_TRACK);
79 
80 	/*
81 		If the file's sample format is unsigned integer data,
82 		change the virtual sample format to two's complement
83 		since the SGI Audio Library won't accept unsigned
84 		data.
85 	*/
86 	if (sampleFormat == AF_SAMPFMT_UNSIGNED)
87 	{
88 		afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK,
89 			AF_SAMPFMT_TWOSCOMP, sampleWidth);
90 	}
91 
92 	printf("frame count: %lld\n", frameCount);
93 	printf("frame size: %d bytes\n", frameSize);
94 	printf("channel count: %d\n", channelCount);
95 	printf("sample rate: %.2f Hz\n", sampleRate);
96 
97 	buffer = malloc(frameCount * frameSize);
98 	afReadFrames(file, AF_DEFAULT_TRACK, buffer, frameCount);
99 
100 	loopCount = afGetLoopIDs(file, AF_DEFAULT_INST, NULL);
101 	loopids = malloc(sizeof (int) * loopCount);
102 	afGetLoopIDs(file, AF_DEFAULT_INST, loopids);
103 
104 	markCount = afGetMarkIDs(file, AF_DEFAULT_TRACK, NULL);
105 	markids = malloc(sizeof (int) * markCount);
106 	afGetMarkIDs(file, AF_DEFAULT_TRACK, markids);
107 
108 	printf("loop ids:");
109 	for (i=0; i<loopCount; i++)
110 		printf(" %d", loopids[i]);
111 	printf("\n");
112 
113 	printf("mark ids:");
114 	for (i=0; i<markCount; i++)
115 		printf(" %d", markids[i]);
116 	printf("\n");
117 
118 	startmarkid = afGetLoopStart(file, AF_DEFAULT_INST, 1);
119 	endmarkid = afGetLoopEnd(file, AF_DEFAULT_INST, 1);
120 	startloop = afGetMarkPosition(file, AF_DEFAULT_TRACK, startmarkid);
121 	endloop = afGetMarkPosition(file, AF_DEFAULT_TRACK, endmarkid);
122 
123 	afCloseFile(file);
124 
125 	outportconfig = alNewConfig();
126 	setwidth(outportconfig, sampleWidth);
127 	setsampleformat(outportconfig, sampleFormat);
128 	alSetChannels(outportconfig, channelCount);
129 
130 	outport = alOpenPort("irixtestloop", "w", outportconfig);
131 	setrate(outport, sampleRate);
132 
133 	alWriteFrames(outport, buffer, startloop);
134 	for (i=0; i<REPEAT_COUNT; i++)
135 	{
136 		printf("iteration %d: start %lld, end %lld, length %lld\n",
137 			i, endloop, startloop, endloop - startloop);
138 		alWriteFrames(outport,
139 			(char *) buffer + (int) (startloop * frameSize),
140 			endloop - startloop);
141 	}
142 
143 	waitport(outport);
144 
145 	alClosePort(outport);
146 	alFreeConfig(outportconfig);
147 
148 	free(buffer);
149 }
150