Files: c2d33b90a4cc52d69bde0bb9c56ba78760ed9196 / src / main_osx.cpp
2733 bytesRaw
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | static int running; |
12 | |
13 | static Player * player; |
14 | static volatile int fade_start, fade_length; |
15 | static volatile int max_channels; |
16 | |
17 | void signal_handler(int sig) |
18 | { |
19 | running = 0; |
20 | signal(sig, signal_handler); |
21 | } |
22 | |
23 | void fade_buffer(signed short *buffer, unsigned int count, int fade_start, int fade_length) |
24 | { |
25 | unsigned int i; |
26 | for (i = 0; i < count; i++) |
27 | { |
28 | if (fade_start < fade_length) |
29 | { |
30 | buffer[ i * 2 + 0 ] = (int64_t)((int64_t)buffer[ i * 2 + 0 ] * ( fade_length - fade_start )) / fade_length; |
31 | buffer[ i * 2 + 1 ] = (int64_t)((int64_t)buffer[ i * 2 + 1 ] * ( fade_length - fade_start )) / fade_length; |
32 | fade_start++; |
33 | } |
34 | else |
35 | { |
36 | buffer[ i * 2 + 0 ] = 0; |
37 | buffer[ i * 2 + 1 ] = 0; |
38 | } |
39 | } |
40 | } |
41 | |
42 | void render(void * unused, short * samples, uint32_t sampleCount) |
43 | { |
44 | syntrax_info info; |
45 | mixChunk(player, samples, sampleCount); |
46 | if (playerGetSongEnded(player)) running = 0; |
47 | if (playerGetLoopCount(player) >= 2) |
48 | { |
49 | fade_buffer( samples, sampleCount, fade_start, fade_length ); |
50 | fade_start += sampleCount; |
51 | } |
52 | playerGetInfo(player, &info); |
53 | 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); |
54 | } |
55 | |
56 | int main(int argc, const char* const* argv) |
57 | { |
58 | Song * song; |
59 | CoreAudioStream * output; |
60 | |
61 | if (argc != 2) |
62 | { |
63 | fprintf(stderr, "Usage:\t%s <song>\n", argv[0]); |
64 | return 1; |
65 | } |
66 | |
67 | song = File_loadSong(argv[1]); |
68 | if (!song) |
69 | { |
70 | fprintf(stderr, "Invalid song:\t%s\n", argv[1]); |
71 | return 1; |
72 | } |
73 | |
74 | player = playerCreate(SAMPLE_RATE); |
75 | if (!player) |
76 | { |
77 | fprintf(stderr, "Out of memory.\n"); |
78 | File_freeSong(song); |
79 | return 1; |
80 | } |
81 | |
82 | if (loadSong(player, song) < 0) |
83 | { |
84 | fprintf(stderr, "Out of memory.\n"); |
85 | playerDestroy(player); |
86 | File_freeSong(song); |
87 | return 1; |
88 | } |
89 | |
90 | initSubsong(player, 0); |
91 | |
92 | signal(SIGINT, signal_handler); |
93 | |
94 | output = new CoreAudioStream(render, 0, SAMPLE_RATE); |
95 | |
96 | if ( output ) |
97 | { |
98 | fade_start = 0; fade_length = SAMPLE_RATE * 10; |
99 | max_channels = 0; |
100 | running = 1; |
101 | output->start(); |
102 | while ( running && fade_start < fade_length ) |
103 | { |
104 | usleep(10000); |
105 | } |
106 | output->close(); |
107 | fprintf(stderr, "\n"); |
108 | } |
109 | |
110 | delete output; |
111 | |
112 | playerDestroy(player); |
113 | File_freeSong(song); |
114 | |
115 | return 0; |
116 | } |
117 |
Built with git-ssb-web