Mercurial > hg > Game > Cerium
view example/synthesizer/ppe/OSC.cc @ 2022:fac44ad2867d draft
make a sound
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 16 Jul 2014 02:50:32 +0900 |
parents | 6849865f96eb |
children | 8c3c826ba4d5 |
line wrap: on
line source
#include <stdio.h> #include <string.h> #include "AudioData.h" #include "Func.h" #include "OSC.h" #include <sdl.h> #include <SDL_audio.h> #include <math.h> /* これは必須 */ SchedDefineTask1(Audioosc,osc); int gvolume = 3000; double gFrequency = 440; SDL_AudioSpec Desired; SDL_AudioSpec Obtained; double square(double t){ double decimal_part = t - abs(t); return decimal_part < 0.5 ? 1 : -1; } double tri(double t){ double decimal_part = t - abs(t); if(abs(t) % 2 != 0){ return decimal_part < 0.5 ? decimal_part : 1 - decimal_part; }else{ return decimal_part < 0.5 ? -decimal_part : 1 - decimal_part; } } void callback(void *userdata,Uint8 *stream,int len){ char *waveform_name = (char*)userdata; static unsigned int step = 0; Uint16 *frames = (Uint16 *) stream; int framesize = len / 2; if(strcmp(waveform_name, "tri")){ for (int i = 0; i < framesize ; i++, step++){ frames[i] = tri(step * gFrequency / Obtained.freq) * gvolume ; } }else if(strcmp(waveform_name, "sqr")){ for (int i = 0; i < framesize ; i++, step++){ frames[i] = square(step * gFrequency / Obtained.freq) * gvolume ; } } } static int osc(SchedTask *s, void *rbuf, void *wbuf) { AudioData *i_data = (AudioDataPtr)s->get_input(0); gvolume = i_data->volume; const char* waveform_name = "tri"; gFrequency = i_data->Frequency; i_data->userdata = (void*)waveform_name; Desired.freq = i_data->freq; /* Sampling rate: 44100Hz */ Desired.format = i_data->format; /* 16-bit signed audio */ Desired.channels = i_data->channels; /* Mono */ Desired.samples = i_data->samples; /* Buffer size: 8K = 0.37 sec. */ Desired.callback = callback; Desired.userdata = i_data->userdata; SDL_OpenAudio(&Desired, &Obtained); SDL_PauseAudio(0); SDL_Delay(200); SDL_Quit(); return 0; }