Пример #1
0
int PGE_MusPlayer::initAudio(int sampleRate, int allocateChannels, int bufferSize)
{
    int ret=0;
    sRate=sampleRate;

    MIX_Timidity_addToPathList(QString(ApplicationPath+"/timidity/").toLocal8Bit().data());
    if(isLoaded) Mix_CloseAudio();
    ret = Mix_OpenAudio(sRate, AUDIO_S16, 2, bufferSize);
    if(ret==-1) return ret;
    Mix_AllocateChannels(allocateChannels);

    // Reset the audio sample count and set the post mix callback
    if (sampleCountMutex == NULL)
    {
        sampleCountMutex = SDL_CreateMutex();
    }
    // Reset music sample count
    if (SDL_LockMutex(sampleCountMutex) == 0)
    {
        sCount = 0;
        musSCount = 0;
        Mix_SetPostMix(postMixCallback, NULL);
        SDL_UnlockMutex(sampleCountMutex);
    }
    isLoaded = true;
    return ret;
}
Пример #2
0
static bool Init()
{
   if (!gSDLIsInit)
   {
      ELOG("Please init Stage before creating sound.");
      return false;
   }

   if (!sChannelsInit)
   {
      sChannelsInit = true;
      for(int i=0;i<sMaxChannels;i++)
      {
         sUsedChannel[i] = false;
         sDoneChannel[i] = false;
      }
      Mix_ChannelFinished(onChannelDone);
      Mix_HookMusicFinished(onMusicDone);
      #ifndef EMSCRIPTEN
      Mix_SetPostMix(onPostMix,0);
      #endif
   }

   return sChannelsInit;
}
Пример #3
0
bool GLPlayerWindow::openVideoFile(std::string videoPath) {

     sasdlCtx = SASDL_open((char *)(videoPath.c_str()));
     if(sasdlCtx == NULL) {
          std::cerr << __FILE__ << ": Failed to open video file." << std::endl;
          return false;
     }

     if(Mix_OpenAudio(sasdlCtx->sa_ctx->a_codec_ctx->sample_rate, AUDIO_S16SYS,
                      sasdlCtx->sa_ctx->a_codec_ctx->channels, 512) < 0) {
          std::cerr << __FILE__ << ": Mix_OpenAudio: " << SDL_GetError() << std::endl;
          return false;
     }
     Mix_SetPostMix(SASDL_audio_decode, sasdlCtx);

     int width = SASDL_get_video_width(sasdlCtx);
     int height = SASDL_get_video_height(sasdlCtx);
     
     this->resize(width, height);
     frame = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
                                  0, 0, 0, 0);

     SASDL_play(sasdlCtx);
     
     return true;
}
Пример #4
0
void I_InitAudio(void)
{ 
	sysaudio.obtained.freq = sysaudio.desired.freq =
		SAMPLERATE;
	sysaudio.obtained.format = sysaudio.desired.format =
		AUDIO_S16SYS;
	sysaudio.obtained.channels = sysaudio.desired.channels =
		2;
	sysaudio.obtained.samples = sysaudio.desired.samples =
		SAMPLECOUNT;
	sysaudio.obtained.size = sysaudio.desired.size =
		(SAMPLECOUNT*sysaudio.obtained.channels*((sysaudio.obtained.format&0xff)>>3));

	if (!sysaudio.enabled)
		return;

#ifdef ENABLE_SDLMIXER
	{
		int freq, channels;
		Uint16 format;

		if (Mix_OpenAudio(sysaudio.desired.freq, sysaudio.desired.format,
			sysaudio.desired.channels, sysaudio.desired.samples)<0) {
			I_Error("Could not open audio: %s\n", SDL_GetError());
		}

		if (!Mix_QuerySpec(&freq, &format, &channels)) {
			I_Error("Could not open audio: %s\n", SDL_GetError());
		}

		sysaudio.obtained.freq = freq;
		sysaudio.obtained.channels = channels;
		sysaudio.obtained.format = format;
		sysaudio.obtained.size = (SAMPLECOUNT*channels*((format&0xff)>>3));

		I_InitMusic();
		I_InitSound();

		Mix_SetPostMix(I_UpdateSound, NULL);
/*		Mix_ReserveChannels(NUM_CHANNELS);*/
	}
#else
	sysaudio.desired.callback = I_UpdateAudio;
	sysaudio.desired.userdata = NULL;

	if (SDL_OpenAudio(&sysaudio.desired, &sysaudio.obtained)<0) {
		I_Error("Could not open audio: %s\n", SDL_GetError());
	}

	I_InitMusic();
	I_InitSound();

	SDL_PauseAudio(0);
#endif
}
Пример #5
0
static int PCSound_SDL_Init(pcsound_callback_func callback_func)
{
   if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
   {
      fprintf(stderr, "Unable to set up sound.\n");
      return 0;
   }
   
   if(Mix_OpenAudio(pcsound_sample_rate, AUDIO_S16SYS, 2, 1024) < 0)
   {
      fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());
      
      SDL_QuitSubSystem(SDL_INIT_AUDIO);
      return 0;
   }
   
   SDL_PauseAudio(0);

    // Get the mixer frequency, format and number of channels.

    Mix_QuerySpec(&mixing_freq, &mixing_format, &mixing_channels);

    // Only supports AUDIO_S16SYS

    if (mixing_format != AUDIO_S16SYS || mixing_channels != 2)
    {
        fprintf(stderr, 
                "PCSound_SDL only supports native signed 16-bit LSB, "
                "stereo format!\n");

        PCSound_SDL_Shutdown();
        return 0;
    }

    callback = callback_func;
    current_freq = 0;
    current_remaining = 0;

    Mix_SetPostMix(PCSound_Mix_Callback, NULL);

    return 1;
}
Пример #6
0
TMusic *MusicCreate(const char *pFileName)
{
    Mix_Music *lMusic = Mix_LoadMUS(pFileName);
 	if(lMusic==NULL)
	{
		printf("MusicCreate: %s ...\n", Mix_GetError());
		return NULL;
	}
	else
	{
		printf("MusicCreate: %s ...\n", pFileName);
	}

    CLASS_CREATE(TMusic);
    
    this->mTicks = 0;
    this->mMusicId = lMusic;
    Mix_SetPostMix(MusicCallback, (void *)this);
    
    CLASS_INSTANCE();
}
Пример #7
0
Файл: digi.c Проект: btb/d2x
/* Initialise audio devices. */
int digi_init()
{
	if (SDL_InitSubSystem(SDL_INIT_AUDIO)<0) {
		Error("SDL audio initialisation failed: %s.",SDL_GetError());
	}

	if ( Mix_OpenAudio(digi_sample_rate, AUDIO_U8, 2, SOUND_BUFFER_SIZE) < 0 ) {
		//edited on 10/05/98 by Matt Mueller - should keep running, just with no sound.
		Warning("\nError: Couldn't open audio: %s\n", SDL_GetError());
		//killed  exit(2);
		return 1;
		//end edit -MM
	}

	Mix_SetPostMix(audio_mixcallback, NULL);

	Mix_Resume(-1);
	
	atexit(digi_close);
	Digi_initialized = 1;
	return 0;
}
Пример #8
0
static int SDLCALL I_AVIteratePacketsThread(void *param)
{
    AVPacket packet;

    if(hasAudio)
    {
        // start the post mix thread here
        Mix_SetPostMix(I_AVPostMixCallback, audioCodecCtx);
    }
    
    while(av_read_frame(formatCtx, &packet) >= 0 && !userExit)
    {
        if(packet.stream_index == videoStreamIdx)
        {
            // queue the video packet
            I_AVPushPacketToQueue(videoPacketQueue, &packet);
        }
        else if(hasAudio && packet.stream_index == audioStreamIdx)
        {
            if(audioCodecCtx->sample_fmt == AV_SAMPLE_FMT_FLTP)
            {
                // queue the audio buffer from the packet
                I_AVFillAudioBuffer(&packet);
                av_free_packet(&packet);
            }
        }
        else
        {
            av_free_packet(&packet);
        }
    }

    // add end markers
    I_AVPushPacketToQueue(videoPacketQueue, NULL);
    I_AVPushPacketToQueue(audioPacketQueue, NULL);
    return 0;
}
Пример #9
0
static void I_AVShutdown(void)
{
    if(hasAudio)
    {
        Mix_SetPostMix(NULL, NULL);
    }

    SDL_WaitThread(thread, NULL);

    I_AVDeletePacketQueue(&videoPacketQueue);
    I_AVDeletePacketQueue(&audioPacketQueue);
    I_AVDeleteAudioQueue();

    av_free(videoBuffer);
    av_free(audioBuffer);
    av_free(videoFrame);
    
    avcodec_close(videoCodecCtx);
    avcodec_close(audioCodecCtx);
    avformat_close_input(&formatCtx);
    sws_freeContext(swsCtx);

    RB_DeleteTexture(&texture);
}
Пример #10
0
static int create_audiobuf_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context)
{
	int flags;
	int sample_rate;
	int desired_buffer;

	int stereo;
	int bitsize;
	int compressed;

	int format;

	if (!mve_audio_enabled)
		return 1;

	if (audiobuf_created)
		return 1;
	else
		audiobuf_created = 1;

	flags = get_ushort(data + 2);
	sample_rate = get_ushort(data + 4);
	desired_buffer = get_int(data + 6);

	stereo = (flags & MVE_AUDIO_FLAGS_STEREO) ? 1 : 0;
	bitsize = (flags & MVE_AUDIO_FLAGS_16BIT) ? 1 : 0;

	if (minor > 0) {
		compressed = flags & MVE_AUDIO_FLAGS_COMPRESSED ? 1 : 0;
	} else {
		compressed = 0;
	}

	mve_audio_compressed = compressed;

	if (bitsize == 1) {
#ifdef WORDS_BIGENDIAN
		format = AUDIO_S16MSB;
#else
		format = AUDIO_S16LSB;
#endif
	} else {
		format = AUDIO_U8;
	}

#ifdef USE_SDLMIXER
	if (GameArg.SndDisableSdlMixer)
#endif
	{
		con_printf(CON_CRITICAL, "creating audio buffers:\n");
		con_printf(CON_CRITICAL, "sample rate = %d, desired buffer = %d, stereo = %d, bitsize = %d, compressed = %d\n",
				sample_rate, desired_buffer, stereo, bitsize ? 16 : 8, compressed);
	}

	mve_audio_spec = (SDL_AudioSpec *)mve_alloc(sizeof(SDL_AudioSpec));
	mve_audio_spec->freq = sample_rate;
	mve_audio_spec->format = format;
	mve_audio_spec->channels = (stereo) ? 2 : 1;
	mve_audio_spec->samples = 4096;
	mve_audio_spec->callback = mve_audio_callback;
	mve_audio_spec->userdata = NULL;

	// MD2211: if using SDL_Mixer, we never reinit the sound system
#ifdef USE_SDLMIXER
	if (GameArg.SndDisableSdlMixer)
#endif
	{
		if (SDL_OpenAudio(mve_audio_spec, NULL) >= 0) {
			con_printf(CON_CRITICAL, "   success\n");
			mve_audio_canplay = 1;
		}
		else {
			con_printf(CON_CRITICAL, "   failure : %s\n", SDL_GetError());
			mve_audio_canplay = 0;
		}
	}

#ifdef USE_SDLMIXER
	else {
		// MD2211: using the same old SDL audio callback as a postmixer in SDL_mixer
		Mix_SetPostMix(mve_audio_spec->callback, mve_audio_spec->userdata);
		mve_audio_canplay = 1;
	}
#endif

	memset(mve_audio_buffers, 0, sizeof(mve_audio_buffers));
	memset(mve_audio_buflens, 0, sizeof(mve_audio_buflens));

	return 1;
}
Пример #11
0
int main(int argc, char *argv[])
{
     if(argc != 2)
     {
          fprintf(stderr, "Usage:\nsaplayer <filename>\n");
          return 1;
     }
     
     // FIXME: add SDL_INIT_EVENTTHREAD on Windows and Mac?
     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
     
     SASDL_init();
     SASDLContext *sasdl_ctx = SASDL_open(argv[1]);
     if(sasdl_ctx == NULL)
     {
          fprintf(stderr, "failed to open video file?\n");
          SDL_Quit();
          return 1;
     }

     // still very ugly...
     if(Mix_OpenAudio(sasdl_ctx->sa_ctx->a_codec_ctx->sample_rate, AUDIO_S16SYS,
                      sasdl_ctx->sa_ctx->a_codec_ctx->channels, 512) < 0) {
          fprintf(stderr, "Mix_OpenAudio: %s\n", SDL_GetError());
          SASDL_close(sasdl_ctx);
          SDL_Quit();
          return 1;
     }
     Mix_SetPostMix(SASDL_audio_decode, sasdl_ctx);

     double delta = 0.0f;
     SDL_Event event;
     int width = SASDL_get_video_width(sasdl_ctx);
     int height = SASDL_get_video_height(sasdl_ctx);
     
     SDL_Surface *screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
     int (*get_event)(SDL_Event *) = SDL_PollEvent;
     
     printf("video duration: %.3fs\n", SASDL_get_video_duration(sasdl_ctx));
     SASDL_play(sasdl_ctx);

     while(SASDL_eof(sasdl_ctx) == FALSE)
     {
          SASDL_draw(sasdl_ctx, screen);
          SDL_Flip(screen);

          while(get_event(&event))
               if(event.type == SDL_QUIT) {
                    goto PROGRAM_QUIT;
               } else if(event.type == SDL_KEYDOWN) {
                    switch(event.key.keysym.sym) {
                    case SDLK_LEFT:
                         delta = -10.0;
                         break;
                    case SDLK_RIGHT:
                         delta = 10.0;
                         break;
                    case SDLK_UP:
                         delta = -60.0;
                         break;
                    case SDLK_DOWN:
                         delta = 60.0;
                         break;
                    case SDLK_SPACE:
                         if(SASDL_get_video_status(sasdl_ctx) == SASDL_is_playing)
                         {
                              SASDL_pause(sasdl_ctx);
                              get_event = SDL_WaitEvent;
                              continue;
                         } else
                         {
                              SASDL_play(sasdl_ctx);
                              get_event = SDL_PollEvent;
                              goto NEXT_LOOP;
                         }
                    case SDLK_s:
                         SASDL_stop(sasdl_ctx);
                         SASDL_draw(sasdl_ctx, screen); // fill screen with black
                         SDL_Flip(screen);
                         get_event = SDL_WaitEvent;
                         continue;
                    default:
                         // ignore this event. get the next one.
                         continue;
                    }

                    double seek_dst = SASDL_get_video_clock(sasdl_ctx) + delta;
                    if(SASDL_seek_accurate(sasdl_ctx, seek_dst) < 0)
                         goto PROGRAM_QUIT;

                    SASDL_draw(sasdl_ctx, screen);
                    SDL_Flip(screen);
               }

          SASDL_wait_for_next_frame(sasdl_ctx);
     NEXT_LOOP:;
     }

PROGRAM_QUIT:
     Mix_CloseAudio();
     SASDL_close(sasdl_ctx);
     SDL_Quit();
     
     return 0;
}
Пример #12
0
int main(int argc, char **argv)
{
	int audio_rate,audio_channels;
	Uint16 audio_format;
	Uint32 t;
	Mix_Music *music;
	int volume=SDL_MIX_MAXVOLUME;

	/* initialize SDL for audio and video */
	if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)<0)
		cleanExit("SDL_Init");
	atexit(SDL_Quit);

	int initted=Mix_Init(0);
	printf("Before Mix_Init SDL_mixer supported: ");
	print_init_flags(initted);
	initted=Mix_Init(~0);
	printf("After  Mix_Init SDL_mixer supported: ");
	print_init_flags(initted);
	Mix_Quit();

	if(argc<2 || argc>4)
	{
		fprintf(stderr,"Usage: %s filename [depth] [any 3rd argument...]\n"
				"    filename is any music file supported by your SDL_mixer library\n"
				"    depth is screen depth, default is 8bits\n"
				"    if there is a third argument given, we go fullscreen for maximum fun!\n",
				*argv);
		return 1;
	}


	/* open a screen for the wav output */
	if(!(s=SDL_SetVideoMode(W,H,(argc>2?atoi(argv[2]):8),(argc>3?SDL_FULLSCREEN:0)|SDL_HWSURFACE|SDL_DOUBLEBUF)))
		cleanExit("SDL_SetVideoMode");
	SDL_WM_SetCaption("sdlwav - SDL_mixer demo","sdlwav");
	
	/* hide the annoying mouse pointer */
	SDL_ShowCursor(SDL_DISABLE);
	/* get the colors we use */
	white=SDL_MapRGB(s->format,0xff,0xff,0xff);
	black=SDL_MapRGB(s->format,0,0,0);
	
	/* initialize sdl mixer, open up the audio device */
	if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,BUFFER)<0)
		cleanExit("Mix_OpenAudio");

	/* we play no samples, so deallocate the default 8 channels... */
	Mix_AllocateChannels(0);
	
	/* print out some info on the formats this run of SDL_mixer supports */
	{
		int i,n=Mix_GetNumChunkDecoders();
		printf("There are %d available chunk(sample) decoders:\n",n);
		for(i=0; i<n; ++i)
			printf("	%s\n", Mix_GetChunkDecoder(i));
		n = Mix_GetNumMusicDecoders();
		printf("There are %d available music decoders:\n",n);
		for(i=0; i<n; ++i)
			printf("	%s\n", Mix_GetMusicDecoder(i));
	}

	/* print out some info on the audio device and stream */
	Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
	bits=audio_format&0xFF;
	sample_size=bits/8+audio_channels;
	rate=audio_rate;
	printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
			bits, audio_channels>1?"stereo":"mono", BUFFER );

	/* calculate some parameters for the wav display */
	dy=s->h/2.0/(float)(0x1<<bits);
	
	/* load the song */
	if(!(music=Mix_LoadMUS(argv[1])))
		cleanExit("Mix_LoadMUS(\"%s\")",argv[1]);

	{
		Mix_MusicType type=Mix_GetMusicType(music);
		printf("Music type: %s\n",
				type==MUS_NONE?"MUS_NONE":
				type==MUS_CMD?"MUS_CMD":
				type==MUS_WAV?"MUS_WAV":
				/*type==MUS_MOD_MODPLUG?"MUS_MOD_MODPLUG":*/
				type==MUS_MOD?"MUS_MOD":
				type==MUS_MID?"MUS_MID":
				type==MUS_OGG?"MUS_OGG":
				type==MUS_MP3?"MUS_MP3":
				type==MUS_MP3_MAD?"MUS_MP3_MAD":
				type==MUS_FLAC?"MUS_FLAC":
				"Unknown");
	}
	/* set the post mix processor up */
	Mix_SetPostMix(postmix,argv[1]);
	
	SDL_FillRect(s,NULL,black);
	SDL_Flip(s);
	SDL_FillRect(s,NULL,black);
	SDL_Flip(s);
	/* start playing and displaying the wav */
	/* wait for escape key of the quit event to finish */
	t=SDL_GetTicks();
	if(Mix_PlayMusic(music, 1)==-1)
		cleanExit("Mix_PlayMusic(0x%p,1)",music);
	Mix_VolumeMusic(volume);

	while((Mix_PlayingMusic() || Mix_PausedMusic()) && !done)
	{
		SDL_Event e;
		while(SDL_PollEvent(&e))
		{
			switch(e.type)
			{
				case SDL_KEYDOWN:
					switch(e.key.keysym.sym)
					{
						case SDLK_ESCAPE:
							done=1;
							break;
						case SDLK_LEFT:
							if(e.key.keysym.mod&KMOD_SHIFT)
							{
								Mix_RewindMusic();
								position=0;
							}
							else
							{
								int pos=position/audio_rate-1;
								if(pos<0)
									pos=0;
								Mix_SetMusicPosition(pos);
								position=pos*audio_rate;
							}
							break;
						case SDLK_RIGHT:
							switch(Mix_GetMusicType(NULL))
							{
								case MUS_MP3:
									Mix_SetMusicPosition(+5);
									position+=5*audio_rate;
									break;
								case MUS_OGG:
								case MUS_FLAC:
								case MUS_MP3_MAD:
								/*case MUS_MOD_MODPLUG:*/
									Mix_SetMusicPosition(position/audio_rate+1);
									position+=audio_rate;
									break;
								default:
									printf("cannot fast-forward this type of music\n");
									break;
							}
							break;
						case SDLK_UP:
							volume=(volume+1)<<1;
							if(volume>SDL_MIX_MAXVOLUME)
								volume=SDL_MIX_MAXVOLUME;
							Mix_VolumeMusic(volume);
							break;
						case SDLK_DOWN:
							volume>>=1;
							Mix_VolumeMusic(volume);
							break;
						case SDLK_SPACE:
							if(Mix_PausedMusic())
								Mix_ResumeMusic();
							else
								Mix_PauseMusic();
							break;
						default:
							break;
					}
					break;
				case SDL_QUIT:
					done=1;
					break;
				default:
					break;
			}
		}
		/* the postmix processor tells us when there's new data to draw */
		if(need_refresh)
			refresh();
		SDL_Delay(0);
	}
	t=SDL_GetTicks()-t;
	
	/* free & close */
	Mix_FreeMusic(music);
	Mix_CloseAudio();
	SDL_Quit();
	/* show a silly statistic */
	printf("fps=%.2f\n",((float)flips)/(t/1000.0));
	return(0);
}