git ssb

0+

kode54 / syntrax-c



Tree: bff8b8a36f298d4d4b58de609058a7608cacf8a2

Files: bff8b8a36f298d4d4b58de609058a7608cacf8a2 / src / main_osx.c

2735 bytesRaw
1#include <stdio.h>
2#include <stdint.h>
3#include <signal.h>
4#include <ao/ao.h>
5
6#include "file.h"
7
8#define SAMPLE_RATE 44100 /* 22050 - test low-pass capability of blep synthesizer */
9
10static int running;
11
12void signal_handler(int sig)
13{
14 running = 0;
15 signal(sig, signal_handler);
16}
17
18void fade_buffer(signed short *buffer, unsigned int count, int fade_start, int fade_length)
19{
20 unsigned int i;
21 for (i = 0; i < count; i++)
22 {
23 if (fade_start < fade_length)
24 {
25 buffer[ i * 2 + 0 ] = (int64_t)((int64_t)buffer[ i * 2 + 0 ] * ( fade_length - fade_start )) / fade_length;
26 buffer[ i * 2 + 1 ] = (int64_t)((int64_t)buffer[ i * 2 + 1 ] * ( fade_length - fade_start )) / fade_length;
27 fade_start++;
28 }
29 else
30 {
31 buffer[ i * 2 + 0 ] = 0;
32 buffer[ i * 2 + 1 ] = 0;
33 }
34 }
35}
36
37int main(int argc, const char* const* argv)
38{
39 Song * song;
40 Player * player;
41 ao_device * dev;
42 ao_sample_format fmt = { 16, SAMPLE_RATE, 2, AO_FMT_NATIVE, 0 };
43
44 signed short sample_buffer[2048 * 2];
45
46 if (argc != 2)
47 {
48 fprintf(stderr, "Usage:\t%s <song>\n", argv[0]);
49 return 1;
50 }
51
52 song = File_loadSong(argv[1]);
53 if (!song)
54 {
55 fprintf(stderr, "Invalid song:\t%s\n", argv[1]);
56 return 1;
57 }
58
59 player = playerCreate(SAMPLE_RATE);
60 if (!player)
61 {
62 fprintf(stderr, "Out of memory.\n");
63 File_freeSong(song);
64 return 1;
65 }
66
67 if (loadSong(player, song) < 0)
68 {
69 fprintf(stderr, "Out of memory.\n");
70 playerDestroy(player);
71 File_freeSong(song);
72 return 1;
73 }
74
75 initSubsong(player, 0);
76
77 signal(SIGINT, signal_handler);
78
79 ao_initialize();
80
81 dev = ao_open_live( ao_default_driver_id(), &fmt, NULL );
82
83 if ( dev )
84 {
85 int fade_start = 0, fade_length = SAMPLE_RATE * 10;
86 int max_channels = 0;
87 syntrax_info info;
88 running = 1;
89 while ( running && fade_start < fade_length )
90 {
91 mixChunk(player, sample_buffer, 2048);
92 if (playerGetSongEnded(player)) break;
93 if (playerGetLoopCount(player) >= 2)
94 {
95 fade_buffer( sample_buffer, 2048, fade_start, fade_length );
96 fade_start += 2048;
97 }
98 ao_play( dev, (char*) sample_buffer, 2048 * 4 );
99 playerGetInfo(player, &info);
100 fprintf(stderr, "\ro: %3u - r: %2u - c: %2u (%2u)", info.coarse, info.fine, info.channelsPlaying, info.channelsPlaying > max_channels ? max_channels = info.channelsPlaying : max_channels);
101 }
102 fprintf(stderr, "\n");
103
104 ao_close( dev );
105 }
106
107 ao_shutdown();
108
109 playerDestroy(player);
110 File_freeSong(song);
111
112 return 0;
113}
114

Built with git-ssb-web