1 /* Ghetto Sound - Sampler
5 * - port unbuffered getchar to windows
14 #include "SDL_mixer.h"
15 //Docs: http://www.libsdl.org/projects/SDL_mixer/docs/
16 //example http://www.kekkai.org/roger/sdl/mixer/
18 static struct termios stored_settings
;
19 void tc_reset() { tcsetattr(0,TCSANOW
,&stored_settings
); }
21 struct termios new_settings
;
22 tcgetattr(0,&stored_settings
);
23 new_settings
= stored_settings
;
24 new_settings
.c_lflag
&= (~ICANON
);
25 new_settings
.c_lflag
&= (~ECHO
);
26 new_settings
.c_cc
[VTIME
] = 0;
27 new_settings
.c_cc
[VMIN
] = 1;
28 tcsetattr(0,TCSANOW
,&new_settings
);
39 Mix_Chunk
**sample
= NULL
;
44 for(i
=0;i
<samplec
;i
++) { Mix_FreeChunk(sample
[i
]); }
49 char *keymap
= "xcvbnmasdfghjklwertuiop789456123zy";
51 int main(int argc
, char* argv
[]) {
53 printf("\n\tUsage:\t%s *.wav *.ogg [...]\n", argv
[0]);
54 printf("\t\t%s --keys 7894561230 *.wav *.ogg [...] #to use own key-bindings\n", argv
[0]);
55 puts("\t\t- lowercase: play sample once; uppercase: loop sample;");
57 puts("\t- currently supported formats: WAVE, AIFF, RIFF, OGG(libvorbis), VOC, MOD(libmikmod), MIDI, MP3(smpeg)");
58 puts("\t- currently there are issues with mp3s - you should convert it:");
59 puts("\t\t- mp3 to wav: ffmpeg -i in.mp3 out.wav");
60 puts("\t\t- mp3 to ogg: ffmpeg -i in.mp3 -acodec vorbis -ac 2 -aq 60 out.ogg");
66 if(SDL_Init(SDL_INIT_AUDIO
)) {
67 perror("Cannot initialize SDL using SDL_Init!\n");
72 if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT
, 2, 256)) { //sampl.freq(def22050), format, 2stereo/1mono, chunksize(default 4095)
73 perror("Cannot open audio using Mix_OpenAudio!\n");
76 //alloc enough mixer channels
77 Mix_AllocateChannels(argc
*2);
79 Mix_Volume(-1,MIX_MAX_VOLUME
/2);
81 //alloc enough samples
82 sample
= malloc(sizeof(Mix_Chunk
)*argc
);
87 argv
++; argc
--; //get rid of argv[0]
88 while(argc
) { //loop through args (load samples and parse options)
89 if(!strcmp(argv
[0], "--keys")) { //alternative keymap
91 puts("\nERR: Cannot specify keys after loading first sample!");
94 if(--argc
) { argv
++; }
97 argv
++; argc
--; continue;
99 if(samplec
< strlen(keymap
)) {
100 sample
[samplec
] = Mix_LoadWAV(argv
[0]);
101 if(sample
[samplec
]) {
102 printf("[%c]: %s%c", keymap
[samplec
], argv
[0], (samplec
+1)%4==0?'\n':'\t');
105 printf("\nERR: Mix_LoadWAV(%s): %s\n", argv
[0], Mix_GetError());
108 if(i
) puts("\nERR: Not enough keys on keyboard (or in keymap)!");
117 puts("[Q]: exit!\t[SPACE]: silence!\t[ENTER]: delay\t[-]: halucination");
118 int keymaplen
= strlen(keymap
);
119 //freopen("test.seq", "r", stdin);
122 char c
; while(c
= getchar()) {
123 //lowercase: play sample once
124 for(i
=0;i
<keymaplen
&& i
< samplec
;i
++) if(c
== keymap
[i
]) {
125 if(Mix_PlayChannel(samplec
+i
, sample
[i
], 0) == -1) printf("ERR: Mix_PlayChannel(sample #%d): %s\n", i
, Mix_GetError());
127 //uppercase: start/stop loop
128 for(i
=0;i
<keymaplen
&& i
< samplec
;i
++) if(c
== keymap
[i
]-'a'+'A') {
132 if(Mix_PlayChannel(i
, sample
[i
], -1) == -1) printf("ERR: Mix_PlayChannel(sample #%d, loop): %s\n", i
, Mix_GetError());
135 if(c
== ' ') Mix_HaltChannel(-1);
136 if(c
== '\n') sleep(1);
137 if(c
== '#') while(getchar() != '\n');
138 if(c
== '?') while(Mix_Playing(-1)) usleep(1000);
139 if(c
== '}') rewind(stdin
);
140 if(c
== 'Q' || c
== 'q') break;
141 if(c
== '-') for(i
=360;i
>=0;i
--) { //halucination
142 Mix_SetPosition(MIX_CHANNEL_POST
, i
, 0);
This page took 0.472635 seconds and 4 git commands to generate.