static int configure_audio(struct testcard_state *s) { UNUSED(s); #if defined HAVE_LIBSDL_MIXER && ! defined HAVE_MACOSX char filename[1024] = ""; int fd; Mix_Music *music; ssize_t bytes_written = 0l; SDL_Init(SDL_INIT_AUDIO); if( Mix_OpenAudio( AUDIO_SAMPLE_RATE, AUDIO_S16LSB, audio_capture_channels, 4096 ) == -1 ) { fprintf(stderr,"[testcard] error initalizing sound\n"); return -1; } strncpy(filename, "/tmp/uv.midiXXXXXX", sizeof filename - 1); fd = mkstemp(filename); if (fd < 0) { perror("mkstemp"); return -1; } do { ssize_t ret; ret = write(fd, song1 + bytes_written, sizeof(song1) - bytes_written); if(ret < 0) return -1; bytes_written += ret; } while (bytes_written < (ssize_t) sizeof(song1)); close(fd); music = Mix_LoadMUS(filename); s->audio_data = (char *) calloc(1, AUDIO_BUFFER_SIZE /* 1 sec */); s->audio_start = 0; s->audio_end = 0; s->audio.bps = AUDIO_BPS; s->audio.ch_count = audio_capture_channels; s->audio.sample_rate = AUDIO_SAMPLE_RATE; // register grab as a postmix processor if(!Mix_RegisterEffect(MIX_CHANNEL_POST, grab_audio, NULL, s)) { printf("[testcard] Mix_RegisterEffect: %s\n", Mix_GetError()); return -1; } if(Mix_PlayMusic(music,-1)==-1){ fprintf(stderr, "[testcard] error playing midi\n"); return -1; } Mix_Volume(-1, 0); printf("[testcard] playing audio\n"); return 0; #else return -2; #endif }
void sfx::play_ambient_variant_sound( const std::string &id, const std::string &variant, int volume, int channel, int duration, float pitch ) { if( !check_sound( volume ) ) { return; } const sound_effect *eff = find_random_effect( id, variant ); if( eff == nullptr ) { return; } const sound_effect &selected_sound_effect = *eff; Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id ); Mix_Chunk *shifted_effect = do_pitch_shift( effect_to_play, pitch ); Mix_VolumeChunk( shifted_effect, selected_sound_effect.volume * get_option<int>( "AMBIENT_SOUND_VOLUME" ) * volume / ( 100 * 100 ) ); if( duration ) { if( Mix_FadeInChannel( channel, shifted_effect, -1, duration ) == -1 ) { dbg( D_ERROR ) << "Failed to play sound effect: " << Mix_GetError(); } } else { if( Mix_PlayChannel( channel, shifted_effect, -1 ) == -1 ) { dbg( D_ERROR ) << "Failed to play sound effect: " << Mix_GetError(); } } Mix_RegisterEffect( channel, empty_effect, cleanup_when_channel_finished, shifted_effect ); }
static int lua_Mix_RegisterEffect(State & state){ Stack * stack = state.stack; if (stack->is<LUA_TFUNCTION>(2)){ lua_EffectWrapper * wrapper = new lua_EffectWrapper; wrapper->state = &state; stack->pushValue(2); wrapper->fnRef = stack->ref(); if (stack->is<LUA_TFUNCTION>(3)){ stack->pushValue(3); wrapper->fnRef = stack->ref(); } else{ wrapper->errFnRef = LUA_REFNIL; } int channel = stack->to<int>(1); Mix_RegisterEffect(channel, lua_Mix_EffectCallback, lua_Mix_EffectDoneCallback, wrapper); return 1; } else{ return 0; } }
int BEE::Sound::effect_set_post(int se_mask) { if (se_mask & se_none) { Mix_UnregisterAllEffects(MIX_CHANNEL_POST); effect_reset_data(); } else { if (se_mask & se_chorus) { Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_chorus, sound_effect_chorus_cleanup, chorus_data); } if (se_mask & se_echo) { Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_echo, sound_effect_echo_cleanup, echo_data); } if (se_mask & se_flanger) { Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_flanger, sound_effect_flanger_cleanup, flanger_data); } if (se_mask & se_gargle) { game->messenger_send({"engine", "sound"}, BEE_MESSAGE_WARNING, "The gargle sound effect is currently unimplemented and will have no effect"); Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_gargle, sound_effect_gargle_cleanup, gargle_data); } if (se_mask & se_reverb) { game->messenger_send({"engine", "sound"}, BEE_MESSAGE_WARNING, "The reverb sound effect is currently unimplemented and will have no effect"); Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_reverb, sound_effect_reverb_cleanup, reverb_data); } if (se_mask & se_compressor) { game->messenger_send({"engine", "sound"}, BEE_MESSAGE_WARNING, "The compressor sound effect is currently unimplemented and will have no effect"); Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_compressor, sound_effect_compressor_cleanup, compressor_data); } if (se_mask & se_equalizer) { game->messenger_send({"engine", "sound"}, BEE_MESSAGE_WARNING, "The equalizer sound effect is currently unimplemented and will have no effect"); Mix_RegisterEffect(MIX_CHANNEL_POST, sound_effect_equalizer, sound_effect_equalizer_cleanup, equalizer_data); } } return 0; }
static void SoundPlayAtPosition( SoundDevice *device, Mix_Chunk *data, int distance, int bearing, const bool isMuffled) { if (data == NULL) { return; } // If distance is very close, don't place any panning on it if (distance < DISTANCE_CLOSE) bearing = 0; if (isMuffled) { distance += OUT_OF_SIGHT_DISTANCE_PLUS; } distance /= 2; // Don't play anything if it's too distant // This means we don't waste sound channels if (distance > 255) { return; } if (!device->isInitialised) { return; } LOG(LM_SOUND, LL_TRACE, "distance(%d) bearing(%d)", distance, bearing); int channel; for (;;) { channel = Mix_PlayChannel(-1, data, 0); if (channel >= 0 || device->channels > 128) { break; } // Check if we cannot play the sound; allocate more channels device->channels *= 2; if (Mix_AllocateChannels(device->channels) != device->channels) { printf("Couldn't allocate channels!\n"); return; } // When allocating new channels, need to reset their volume Mix_Volume(-1, ConfigGetInt(&gConfig, "Sound.SoundVolume")); } Mix_SetPosition(channel, (Sint16)bearing, (Uint8)distance); if (isMuffled) { if (!Mix_RegisterEffect(channel, MuffleEffect, NULL, NULL)) { fprintf(stderr, "Mix_RegisterEffect: %s\n", Mix_GetError()); } } }
int32_t SDLDrv_PCM_Init(int32_t *mixrate, int32_t *numchannels, void * initdata) { int32_t err = 0; int32_t chunksize; uint16_t fmt; UNREFERENCED_PARAMETER(numchannels); UNREFERENCED_PARAMETER(initdata); if (Initialised) { SDLDrv_PCM_Shutdown(); } chunksize = 512; #ifdef __ANDROID__ chunksize = droidinfo.audio_buffer_size; #endif if (*mixrate >= 16000) chunksize *= 2; if (*mixrate >= 32000) chunksize *= 2; err = Mix_OpenAudio(*mixrate, AUDIO_S16SYS, *numchannels, chunksize); if (err < 0) { ErrorCode = SDLErr_OpenAudio; return SDLErr_Error; } if (Mix_QuerySpec(mixrate, &fmt, numchannels)) { if (fmt == AUDIO_U8 || fmt == AUDIO_S8) { ErrorCode = SDLErr_OpenAudio; return SDLErr_Error; } } //Mix_SetPostMix(fillData, NULL); EffectFence = SDL_CreateMutex(); // channel 0 and 1 are actual sounds // dummy channel 2 runs our fillData() callback as an effect Mix_RegisterEffect(2, fillData, NULL, NULL); DummyBuffer = (uint8_t *) calloc(1, chunksize); DummyChunk = Mix_QuickLoad_RAW(DummyBuffer, chunksize); Mix_PlayChannel(2, DummyChunk, -1); Initialised = 1; return SDLErr_Ok; }
void sfx::play_variant_sound_pitch( const std::string &id, const std::string &variant, int volume, int angle, float pitch ) { if( !check_sound( volume ) ) { return; } const sound_effect *eff = find_random_effect( id, variant ); if( eff == nullptr ) { return; } const sound_effect &selected_sound_effect = *eff; Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id ); Mix_Chunk *shifted_effect = do_pitch_shift( effect_to_play, pitch ); Mix_VolumeChunk( shifted_effect, selected_sound_effect.volume * get_option<int>( "SOUND_EFFECT_VOLUME" ) * volume / ( 100 * 100 ) ); int channel = Mix_PlayChannel( -1, shifted_effect, 0 ); Mix_RegisterEffect( channel, empty_effect, cleanup_when_channel_finished, shifted_effect ); Mix_SetPosition( channel, angle, 1 ); }
void THMovie::play(const SDL_Rect &destination_rect, int iChannel) { m_destination_rect = SDL_Rect{ destination_rect.x, destination_rect.y, destination_rect.w, destination_rect.h }; if(!m_pRenderer) { m_sLastError = std::string("Cannot play before setting the renderer"); return; } m_pVideoQueue = new THAVPacketQueue(); m_pMoviePictureBuffer->reset(); m_pMoviePictureBuffer->allocate(m_pRenderer, m_pVideoCodecContext->width, m_pVideoCodecContext->height); m_pAudioPacket = nullptr; m_iAudioPacketSize = 0; m_pbAudioPacketData = nullptr; m_iAudioBufferSize = 0; m_iAudioBufferIndex = 0; m_iAudioBufferMaxSize = 0; m_pAudioQueue = new THAVPacketQueue(); m_iCurSyncPts = 0; m_iCurSyncPtsSystemTime = SDL_GetTicks(); if(m_iAudioStream >= 0) { Mix_QuerySpec(&m_iMixerFrequency, nullptr, &m_iMixerChannels); #ifdef CORSIX_TH_USE_FFMPEG m_pAudioResampleContext = swr_alloc_set_opts( m_pAudioResampleContext, m_iMixerChannels==1?AV_CH_LAYOUT_MONO:AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, m_iMixerFrequency, m_pAudioCodecContext->channel_layout, m_pAudioCodecContext->sample_fmt, m_pAudioCodecContext->sample_rate, 0, nullptr); swr_init(m_pAudioResampleContext); #elif defined(CORSIX_TH_USE_LIBAV) m_pAudioResampleContext = avresample_alloc_context(); av_opt_set_int(m_pAudioResampleContext, "in_channel_layout", m_pAudioCodecContext->channel_layout, 0); av_opt_set_int(m_pAudioResampleContext, "out_channel_layout", m_iMixerChannels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO, 0); av_opt_set_int(m_pAudioResampleContext, "in_sample_rate", m_pAudioCodecContext->sample_rate, 0); av_opt_set_int(m_pAudioResampleContext, "out_sample_rate", m_iMixerFrequency, 0); av_opt_set_int(m_pAudioResampleContext, "in_sample_fmt", m_pAudioCodecContext->sample_fmt, 0); av_opt_set_int(m_pAudioResampleContext, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); avresample_open(m_pAudioResampleContext); #endif m_pChunk = Mix_QuickLoad_RAW(m_pbChunkBuffer, ms_audioBufferSize); m_iChannel = Mix_PlayChannel(iChannel, m_pChunk, -1); if(m_iChannel < 0) { m_iChannel = -1; m_sLastError = std::string(Mix_GetError()); } else { Mix_RegisterEffect(m_iChannel, th_movie_audio_callback, nullptr, this); } } m_pStreamThread = SDL_CreateThread(th_movie_stream_reader_thread, "Stream", this); m_pVideoThread = SDL_CreateThread(th_movie_video_thread, "Video", this); }
void movie_player::play(int iChannel) { if(!renderer) { last_error = std::string("Cannot play before setting the renderer"); return; } video_queue = new av_packet_queue(); movie_picture_buffer->reset(); movie_picture_buffer->allocate(renderer, video_codec_context->width, video_codec_context->height); audio_packet = nullptr; audio_packet_size = 0; audio_packet_data = nullptr; audio_buffer_size = 0; audio_buffer_index = 0; audio_buffer_max_size = 0; audio_queue = new av_packet_queue(); current_sync_pts = 0; current_sync_pts_system_time = SDL_GetTicks(); if(audio_stream_index >= 0) { Mix_QuerySpec(&mixer_frequency, nullptr, &mixer_channels); #ifdef CORSIX_TH_USE_FFMPEG audio_resample_context = swr_alloc_set_opts( audio_resample_context, mixer_channels==1?AV_CH_LAYOUT_MONO:AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, mixer_frequency, audio_codec_context->channel_layout, audio_codec_context->sample_fmt, audio_codec_context->sample_rate, 0, nullptr); swr_init(audio_resample_context); #elif defined(CORSIX_TH_USE_LIBAV) audio_resample_context = avresample_alloc_context(); av_opt_set_int(audio_resample_context, "in_channel_layout", audio_codec_context->channel_layout, 0); av_opt_set_int(audio_resample_context, "out_channel_layout", mixer_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO, 0); av_opt_set_int(audio_resample_context, "in_sample_rate", audio_codec_context->sample_rate, 0); av_opt_set_int(audio_resample_context, "out_sample_rate", mixer_frequency, 0); av_opt_set_int(audio_resample_context, "in_sample_fmt", audio_codec_context->sample_fmt, 0); av_opt_set_int(audio_resample_context, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); avresample_open(audio_resample_context); #endif empty_audio_chunk = Mix_QuickLoad_RAW(audio_chunk_buffer, audio_chunk_buffer_capacity); audio_channel = Mix_PlayChannel(iChannel, empty_audio_chunk, -1); if(audio_channel < 0) { audio_channel = -1; last_error = std::string(Mix_GetError()); Mix_FreeChunk(empty_audio_chunk); } else { Mix_RegisterEffect(audio_channel, th_movie_audio_callback, nullptr, this); } } stream_thread = std::thread(&movie_player::read_streams, this); video_thread = std::thread(&movie_player::run_video, this); }
int DSL_BeginBufferedPlayback( char *BufferStart, int BufferSize, int NumDivisions, unsigned SampleRate, int MixMode, void ( *CallBackFunc )( void ) ) { Uint16 format; Uint8 *tmp; int channels; int chunksize; if (mixer_initialized) { DSL_SetErrorCode(DSL_MixerActive); return DSL_Error; } _CallBackFunc = CallBackFunc; _BufferStart = BufferStart; _BufferSize = (BufferSize / NumDivisions); _NumDivisions = NumDivisions; _SampleRate = SampleRate; _remainder = 0; format = (MixMode & SIXTEEN_BIT) ? AUDIO_S16SYS : AUDIO_U8; channels = (MixMode & STEREO) ? 2 : 1; /* 23ms is typically ideal (11025,22050,44100) 46ms isn't bad */ chunksize = 512; if (SampleRate >= 16000) chunksize *= 2; if (SampleRate >= 32000) chunksize *= 2; /* // SDL mixer does this already if (MixMode & SIXTEEN_BIT) chunksize *= 2; if (MixMode & STEREO) chunksize *= 2; */ if (Mix_OpenAudio(SampleRate, format, channels, chunksize) < 0) { DSL_SetErrorCode(DSL_MixerInitFailure); return DSL_Error; } /* Mix_SetPostMix(mixer_callback, NULL); */ /* have to use a channel because postmix will overwrite the music... */ Mix_RegisterEffect(0, mixer_callback, NULL, NULL); /* create a dummy sample just to allocate that channel */ blank_buf = (Uint8 *)malloc(4096); memset(blank_buf, 0, 4096); blank = Mix_QuickLoad_RAW(blank_buf, 4096); Mix_PlayChannel(0, blank, -1); mixer_initialized = 1; return DSL_Ok; }
LRESULT MusPlayer_WinAPI::MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_HSCROLL: { if(m_self->m_volume == (HWND)lParam) { switch(LOWORD(wParam)) { case TB_ENDTRACK: case TB_THUMBPOSITION: case TB_THUMBTRACK: case SB_LEFT: case SB_RIGHT: DWORD dwPos;// current position of slider dwPos = SendMessageW(m_self->m_volume, TBM_GETPOS, 0, 0); SendMessageW(m_self->m_volume, TBM_SETPOS, (WPARAM)TRUE, //redraw flag (LPARAM)dwPos); m_self->on_volume_valueChanged(dwPos); break; default: break; } } break; } case WM_COMMAND: { switch(HIWORD(wParam)) { case BN_CLICKED: { switch( LOWORD(wParam) ) { case CMD_Open: m_self->on_open_clicked(); break; case CMD_Play: m_self->on_play_clicked(); break; case CMD_Stop: m_self->on_stop_clicked(); break; case CMD_SetDefault: m_self->on_resetDefaultADLMIDI_clicked(); break; case CMD_RecordWave: { BOOL checked = IsDlgButtonChecked(hWnd, CMD_RecordWave); if (checked) { CheckDlgButton(hWnd, CMD_RecordWave, BST_UNCHECKED); m_self->on_recordWav_clicked(false); } else { CheckDlgButton(hWnd, CMD_RecordWave, BST_CHECKED); m_self->on_recordWav_clicked(true); } break; } case CMD_Reverb: { if (PGE_MusicPlayer::reverbEnabled) { CheckMenuItem(m_self->m_contextMenu, CMD_Reverb, MF_UNCHECKED); Mix_UnregisterEffect(MIX_CHANNEL_POST, reverbEffect); PGE_MusicPlayer::reverbEnabled = false; } else { CheckMenuItem(m_self->m_contextMenu, CMD_Reverb, MF_CHECKED); Mix_RegisterEffect(MIX_CHANNEL_POST, reverbEffect, reverbEffectDone, NULL); PGE_MusicPlayer::reverbEnabled = true; } break; } case CMD_ShowLicense: { ShellExecuteW(0, 0, L"http://www.gnu.org/licenses/gpl", 0, 0 , SW_SHOW); break; } case CMD_ShowSource: { ShellExecuteW(0, 0, L"https://github.com/WohlSoft/PGE-Project", 0, 0 , SW_SHOW); break; } default: break; } break; } } break; } case WM_DROPFILES: { HDROP hDropInfo = (HDROP)wParam; wchar_t sItem[MAX_PATH]; memset(sItem, 0, MAX_PATH*sizeof(wchar_t)); if(DragQueryFileW(hDropInfo, 0, (LPWSTR)sItem, sizeof(sItem))) { m_self->openMusicByArg(Wstr2Str(sItem)); } DragFinish(hDropInfo); break; } case WM_CONTEXTMENU: { SetForegroundWindow(hWnd); TrackPopupMenu(m_self->m_contextMenu,TPM_RIGHTBUTTON|TPM_TOPALIGN|TPM_LEFTALIGN, LOWORD( lParam ), HIWORD( lParam ), 0, hWnd, NULL); break; } //Инфо о минимальном и максимальном размере окна case WM_GETMINMAXINFO: { MINMAXINFO *minMax = (MINMAXINFO*)lParam; minMax->ptMinTrackSize.x = 350; minMax->ptMinTrackSize.y = m_self->m_height; break; } case WM_CREATE: { m_self->initUI(hWnd); break; } //Окно было закрыто case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hWnd, msg, wParam, lParam); }
int main(int argc, char* argv[]){ foutp=fopen("data.out", "w"); close(foutp); //smpls = gen_triangle_buffer(); // smpls = gen_sawtooth_buffer(); smpls = snd_load("/media/c5bcf67f-e9eb-4e12-be75-d8b6e09e27ba/olivier/hti/ginf/cpu-emu/cpu-emu/sin.table"); /* //sound_buffer = malloc(sizeof(SNDFMT)*512); */ /* // mem[0] = malloc(sizeof(SNDFMT)*mem0size); */ /* // mem[1] = malloc(sizeof(SNDFMT)*mem1size); */ /* // init without mixer init_sdl (); */ /* // start SDL with audio support */ if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) exit (-1); atexit (SDL_Quit); screen = SDL_SetVideoMode (640, 480, 16, SDL_HWSURFACE); if (screen == NULL) exit (-1); // open SMPFREQ, signed 8bit, system byte order, // stereo audio, using 1024 byte chunks if(Mix_OpenAudio(SMPFREQ, AUDIO_S8, 1, WAVEFORM_LENGTH)==-1) { printf("Mix_OpenAudio: %s\n", Mix_GetError()); exit(2); } Mix_Chunk *sound = NULL; sound = Mix_QuickLoad_RAW(smpls,WAVEFORM_LENGTH); if(sound == NULL) { fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError()); } int channel; channel = Mix_PlayChannel(-1, sound, -1); if(channel == -1) { fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError()); } //while(Mix_Playing(channel) != 0); int len = WAVEFORM_LENGTH; smpls = gen_sin_buffer(); save("sin.out",smpls,len); smpls = gen_triangle_buffer(); save("triangle.out",smpls,len); smpls = gen_sawtooth_buffer(); save("sawtooth.out",smpls,len); smpls = gen_square_buffer(); save("square.out",smpls,len); smpls = gen_pwm_buffer(0.2); save("pwm.out",smpls,len); // violine int attack = 500; int decay = 300; int sustain = 10; int sustainlength0 = (attack / 1000.0) * SMPFREQ; int sustain_max = 15; // 4 Bit 0-15 float sustain0 = (float) sustain * ((float)1.0)/(float)sustain_max; float sustain_level = sustain0; int release = 750; int release0 = (release / 1000.0) * SMPFREQ; int attack0 = (attack / 1000.0) * SMPFREQ; int decay0 = (decay / 1000.0) * SMPFREQ; int t1 = attack0; int t2 = t1 + decay0; int t3 = t2 + sustainlength0; int t4 = t3 + release0; instrument[0].a_attack = ((float)1.0/(float)attack0); instrument[0].a_decay = ((float) ( 1.0 - sustain0)) / (float) ( t1-t2) ; instrument[0].b_decay = (float)1.0 - ((float)instrument[0].a_decay*(float)attack0); instrument[0].a_release = sustain_level / (float) (t3-t4) ; instrument[0].b_release = sustain_level - (float)instrument[0].a_release * (float)t3; instrument[0].adsr_counter = 0; instrument[0].gate=0; adsr_length = gen_adsr_buffer(&adsr_buffer); saveadsr("data.out",adsr_buffer,adsr_length); savebuffer = malloc(sizeof(float)*adsr_length); // register adsrEffect as a mix processor if(!Mix_RegisterEffect(channel, adsrEffect, NULL, &(instrument[0]) )) { printf("Mix_RegisterEffect: %s\n", Mix_GetError()); } /* Mix_VolumeChunk(sound, volume); */ /* printf("volume: %d\n", volume); */ /* Hz=440; */ /* // main_synthesiser(1, NULL); */ /* printf("start play\n"); */ /* // play (); */ /* // SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME); */ int i=0; SDL_Event event; int done=0; int hzinc=0,amplitudeinc; /* float* adsrptr; int len; saveadsr("data.out",adsrptr,len); if(smp_index < WAVEFORM_LENGTH){ for(i = 0; i < len; i++){ adsrptr[i] = adsrptr[i] * smpls[(int)smp_index]; // smp_index +=freq; smp_index++; if(smp_index >= WAVEFORM_LENGTH) smp_index = 0; } } Mix_CloseAudio(); SDL_Quit(); return 0; */ while(!done) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym){ case SDLK_LEFT: volume-=10; Mix_VolumeChunk(sound, volume); // Mix_VolumeChunk(sound, (volume>MIX_MAX_VOLUME-10) ? (volume = MIX_MAX_VOLUME):(volume-=10) ); printf("SDLK_LEFT volume: %i\n",volume); amplitudeinc = -1; break; case SDLK_RIGHT: volume+=10; Mix_VolumeChunk(sound, volume ); // Mix_VolumeChunk(sound, (volume<10) ? (volume = 0): (volume+=10) ); printf("SDLK_RIGHT volume: %i\n",volume); amplitudeinc = +1;; break; case SDLK_UP: hzinc = 1; printf("up active\n"); break; case SDLK_DOWN: hzinc = -1; printf("down active\n"); break; default: handleKey(event.key); break; } break; case SDL_KEYUP: switch(event.key.keysym.sym){ case SDLK_LEFT: amplitudeinc = 0; break; case SDLK_RIGHT: amplitudeinc = 0; break; case SDLK_UP: tonstufe++; hzinc = 0; printf("up in-active\n"); break; case SDLK_DOWN: tonstufe--; hzinc = 0; printf("down in-active\n"); break; default: handleKey(event.key); break; } break; case SDL_QUIT: done = 1; printf("SDL_QUIT\n"); break; case SDL_MOUSEBUTTONUP: switch(event.button.button){ case SDL_BUTTON_WHEELUP: Hz += 1; break; case SDL_BUTTON_WHEELDOWN: Hz -= 1; break; } break; default: /* do nothing */ break; } } freq += hzinc; //Hz= 2*Hz; Hz = Hz + hzinc; amplitude = amplitude + amplitudeinc; // printf("Hz: %f\n",Hz); //% Hz = frequency(music[i++%10]); int y; // SDL_Flip(screen); SDL_Delay (50); } // save("sndout6.snd"); saveadsr("adsr.out",savebuffer,adsr_length); Mix_FreeChunk(sound); Mix_CloseAudio(); SDL_Quit(); return 0; }
int main(int argc, char* argv[]){ SNDFMT* smpls = gen_sin_buffer(); // smpls = snd_load("/media/c5bcf67f-e9eb-4e12-be75-d8b6e09e27ba/olivier/hti/ginf/cpu-emu/cpu-emu/sin.table"); /* // start SDL with audio support */ if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) exit (-1); atexit (SDL_Quit); screen = SDL_SetVideoMode (640, 480, 16, SDL_HWSURFACE); if (screen == NULL) exit (-1); // open SMPFREQ, signed 8bit, system byte order, // stereo audio, using 1024 byte chunks if(Mix_OpenAudio(SMPFREQ, AUDIO_S8, 1, WAVEFORM_LENGTH)==-1) { printf("Mix_OpenAudio: %s\n", Mix_GetError()); exit(2); } Mix_AllocateChannels(SID_VOICES_NR); Mix_Chunk *sound = NULL; sound = Mix_QuickLoad_RAW(smpls,WAVEFORM_LENGTH); if(sound == NULL) { fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError()); } int channel; int channel2; channel = Mix_PlayChannel(-1, sound, -1); // channel2 = Mix_PlayChannel(-1, sound, -1); if(channel == -1) { fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError()); } init_SID(&sid); if(!Mix_RegisterEffect(channel, adsrEffect0, NULL, sid.voicea )) { printf("Mix_RegisterEffect: %s\n", Mix_GetError()); } /* if(!Mix_RegisterEffect(channel, adsrEffect0, NULL, sid.voiceb )) { printf("Mix_RegisterEffect: %s\n", Mix_GetError()); } if(!Mix_RegisterEffect(channel, adsrEffect0, NULL, sid.voicec )) { printf("Mix_RegisterEffect: %s\n", Mix_GetError()); } */ /* play time */ struct sid_6581_voice *voicea = sid.voicea; struct sid_6581_voice *voiceb = sid.voiceb; struct sid_6581_voice *voicec = sid.voicec; int i=0,j=0; for(i=0;i<3;i++){ voicea->frequency = 23436; voicea->control = voicea->control | SID_GATE ; SDL_Delay (50); voicea->frequency = 7382; voicea->attack=200; voicea->decay=20; voicea->sustain = 8; voicea->release=20; voicea->attack = 200; voicea->control = voicea->control & ~SID_GATE ; SDL_Delay (500); voicea->frequency = 14764; voicea->attack=200; voicea->decay=20; voicea->sustain = 6; voicea->release=1000; voicea->control = voicea->control | SID_GATE ; for(j=0;j<10;j++){ SDL_Delay (10); voicea->frequency = 14764+14764*j*1/100; } voicea->control = voicea->control & ~SID_GATE ; voicea->control = voicea->control | SID_GATE ; SDL_Delay (50); voicea->control = voicea->control & ~SID_GATE ; SDL_Delay (50); voicea->frequency = 10440; voicea->control = voicea->control | SID_GATE ; SDL_Delay (50); voicea->control = ((((voicea->control & ~SID_GATE) | SID_SAWT ) & ~SID_PULS ) & ~SID_TRIA ) & ~SID_NOIS ; SDL_Delay (50); voicea->frequency = 7382; voicea->control = voicea->control | SID_GATE ; SDL_Delay (50); voicea->control = voicea->control & ~SID_GATE ; SDL_Delay (50); } /* two voices */ voicea->frequency = 7382; voicea->attack=200; voicea->decay=200; voicea->sustain = 6; voicea->release=1000; voiceb->frequency = 9301; voiceb->attack=120; voiceb->decay=20; voiceb->sustain = 6; voiceb->release=1000; voicea->control = voicea->control | SID_GATE | SID_PULS ; voiceb->control = voiceb->control | SID_GATE | SID_SAWT ; SDL_Delay(400); voicea->control = voicea->control & ~SID_GATE ; voiceb->control = voiceb->control & ~SID_GATE ; SDL_Delay(300); sid.voicea->frequency = 7382; sid.voiceb->frequency = 8286; sid.voicea->control = voicea->control | SID_GATE ; sid.voiceb->control = voiceb->control | SID_GATE ; SDL_Delay(400); sid.voicea->control = voicea->control & ~SID_GATE ; sid.voiceb->control = voiceb->control & ~SID_GATE ; SDL_Delay(300); sid.voicea->frequency = 7382; sid.voiceb->frequency = 8779; sid.voicea->control = voicea->control | SID_GATE ; sid.voiceb->control = voiceb->control | SID_GATE ; SDL_Delay(400); sid.voicea->control = voicea->control & ~SID_GATE ; sid.voiceb->control = voiceb->control & ~SID_GATE ; SDL_Delay(300); sid.voicea->frequency = 7382; sid.voiceb->frequency = 9301; sid.voicea->control = voicea->control | SID_GATE ; sid.voiceb->control = voiceb->control | SID_GATE ; SDL_Delay(400); sid.voicea->control = voicea->control & ~SID_GATE ; sid.voiceb->control = voiceb->control & ~SID_GATE ; SDL_Delay(300); SDL_Event event; int done=0; interface_voice = sid.voicea; while(!done) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym){ case SDLK_LEFT: (sid.filter->volume - 1) < 0 ? 0 : sid.filter->volume--; volume-=10; Mix_VolumeChunk(sound, volume); // Mix_VolumeChunk(sound, (volume>MIX_MAX_VOLUME-10) ? (volume = MIX_MAX_VOLUME):(volume-=10) ); printf("SDLK_LEFT volume: %i\n",volume); break; case SDLK_RIGHT: (sid.filter->volume + 1) >16 ? 15 : sid.filter->volume++; volume+=10; Mix_VolumeChunk(sound, volume ); // Mix_VolumeChunk(sound, (volume<10) ? (volume = 0): (volume+=10) ); printf("SDLK_RIGHT volume: %i\n",volume); break; case SDLK_UP: printf("up active\n"); break; case SDLK_DOWN: printf("down active\n"); break; default: handleKey(event.key); break; } break; case SDL_KEYUP: switch(event.key.keysym.sym){ case SDLK_LEFT: break; case SDLK_RIGHT: break; case SDLK_UP: interface_voice->frequency = interface_voice->frequency + interface_voice->frequency*1/100; printf("up in-active\n"); break; case SDLK_DOWN: interface_voice->frequency = interface_voice->frequency - interface_voice->frequency*1/100; printf("down in-active\n"); break; default: handleKey(event.key); break; } break; case SDL_QUIT: done = 1; printf("SDL_QUIT\n"); break; case SDL_MOUSEBUTTONUP: switch(event.button.button){ case SDL_BUTTON_WHEELUP: interface_voice->frequency = interface_voice->frequency + interface_voice->frequency*1/100; break; case SDL_BUTTON_WHEELDOWN: interface_voice->frequency = interface_voice->frequency - interface_voice->frequency*1/100; break; } break; default: /* do nothing */ break; } } // SDL_Flip(screen); SDL_Delay (50); } Mix_FreeChunk(sound); Mix_CloseAudio(); SDL_Quit(); return 0; }