All pastes #490150 Raw Edit

Something

public c v1 · immutable
#490150 ·published 2007-05-15 21:37 UTC
rendered paste body
#include "SDL/SDL.h"#include "math.h"#define BUFFER_SIZE 1024    // Longitud del buffer, en samples#define SAMPLING_RATE 44100// Este comentario esta extraido de SDL_audio.h, porque me ha molado/* This function is called when the audio device needs more data. * 'stream' is a pointer to the audio data buffer * 'len' is the length of that buffer in bytes. * Once the callback returns, the buffer will no longer be valid. * Stereo samples are stored in a LRLRLR ordering. */void play_sen(void *userdata, Uint8 *stream, int len){    int i;    int num_samples = len / 2;    static int pos = 0;    float buffer[num_samples];    Sint16 *dst_buf = (Sint16*) stream;    for (i=0; i<num_samples ; ++i)        buffer[i] = sin(2.0*M_PI*440.0f*(i+pos)/(float)SAMPLING_RATE);    // Clipping y conversion a Sint16    for (i=0; i<num_samples; ++i)    {        float v = buffer[i];        if (v > 1.0f)            v = 1.0f;        else if (v< -1.0f)            v = -1.0f;        dst_buf[i] = (Sint16)(32767.0f*v);    }    pos += num_samples;}void play_whitenoise(void *userdata, Uint8 *stream, int len){    int i;    int num_samples = len / 2;    Sint16 *buf = (Sint16*) stream;    for (i = 0; i<num_samples ; ++i)        buf[i] = rand()%65535-32768;}int main(int argc, char **argv){    SDL_AudioSpec desired;    desired.freq     = SAMPLING_RATE;// Frecuencia de muestreo    desired.format   = AUDIO_S16SYS; // Formato de las muestras    desired.channels = 1;            // Numero de canales    desired.samples  = BUFFER_SIZE;  // Tamaño del buffer en samples                                     // (potencia de 2)    desired.callback = play_sen;     // Callback    desired.userdata = NULL;         // Puntero a datos    SDL_Init(SDL_INIT_AUDIO);    SDL_OpenAudio(&desired, NULL);    SDL_PauseAudio(0);    getchar();    SDL_PauseAudio(1);    SDL_CloseAudio();    SDL_Quit();}