git ssb

0+

kode54 / syntrax-c



Tree: ccbcc93b4e2df83c81f0bf9633f3ab416908e84b

Files: ccbcc93b4e2df83c81f0bf9633f3ab416908e84b / src / main_osx.cpp

2742 bytesRaw
1#include <stdio.h>
2#include <stdint.h>
3#include <signal.h>
4
5#include "audio_output.h"
6
7#include "file.h"
8
9#define SAMPLE_RATE 44100 /* 22050 - test low-pass capability of blep synthesizer */
10
11static int running;
12
13static Player * player;
14static volatile int fade_start, fade_length;
15static volatile int max_channels;
16
17void signal_handler(int sig)
18{
19 running = 0;
20 signal(sig, signal_handler);
21}
22
23void 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
42void render(void * unused, short * samples, uint32_t sampleCount)
43{
44 syntrax_info info;
45 mixChunk(player, sample_buffer, sampleCount);
46 if (playerGetSongEnded(player)) running = 0;
47 if (playerGetLoopCount(player) >= 2)
48 {
49 fade_buffer( sample_buffer, 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
56int 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 CoreAudioPlayer(render, 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