当前位置: 首页>>代码示例>>C++>>正文


C++ SDL_BuildAudioCVT函数代码示例

本文整理汇总了C++中SDL_BuildAudioCVT函数的典型用法代码示例。如果您正苦于以下问题:C++ SDL_BuildAudioCVT函数的具体用法?C++ SDL_BuildAudioCVT怎么用?C++ SDL_BuildAudioCVT使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SDL_BuildAudioCVT函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: audio_buildAudioCVT

/**
 * \brief Builds various audio conversion structures
 *
 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
 */
int audio_buildAudioCVT()
{
  int result;
  SDL_AudioCVT  cvt;
  SDL_AudioSpec spec1;
  SDL_AudioSpec spec2;
  int i, ii, j, jj, k, kk;

  /* No conversion needed */
  spec1.format = AUDIO_S16LSB;
  spec1.channels = 2;
  spec1.freq = 22050;
  result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
                                   spec1.format, spec1.channels, spec1.freq);
  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
  SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);

  /* Typical conversion */
  spec1.format = AUDIO_S8;
  spec1.channels = 1;
  spec1.freq = 22050;
  spec2.format = AUDIO_S16LSB;
  spec2.channels = 2;
  spec2.freq = 44100;
  result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
                                   spec2.format, spec2.channels, spec2.freq);
  SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
  SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);

  /* All source conversions with random conversion targets, allow 'null' conversions */
  for (i = 0; i < _numAudioFormats; i++) {
    for (j = 0; j < _numAudioChannels; j++) {
      for (k = 0; k < _numAudioFrequencies; k++) {
        spec1.format = _audioFormats[i];
        spec1.channels = _audioChannels[j];
        spec1.freq = _audioFrequencies[k];
        ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
        jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
        kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
        spec2.format = _audioFormats[ii];
        spec2.channels = _audioChannels[jj];
        spec2.freq = _audioFrequencies[kk];
        result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
                                         spec2.format, spec2.channels, spec2.freq);
        SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
            i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
        SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
        if (result<0) {
          SDLTest_LogError("%s", SDL_GetError());
        } else {
          SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
        }
      }
    }
  }

   return TEST_COMPLETED;
}
开发者ID:03050903,项目名称:Torque3D,代码行数:63,代码来源:testautomation_audio.c

示例2: fprintf

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSound::CSound(char *filename, int iSoundID)
{
	m_iSoundID = iSoundID;
	Uint32 len;
	if (SDL_LoadWAV(filename, &m_spec, &m_data, &len) == NULL ) {
		fprintf(stderr, "Couldn't load %s: %s\n",filename, SDL_GetError());
		m_len = CSoundTime(0);
		return;
	}
	m_len = CSoundTime(len);
	m_pos = CSoundTime(0);

	CEasySound *es = CEasySound::Instance();
	SDL_AudioSpec obtained = es->GetObtained();
	SDL_AudioCVT cvt;

	SDL_BuildAudioCVT(&cvt, m_spec.format, m_spec.channels, m_spec.freq, obtained.format, obtained.channels, obtained.freq);
	cvt.buf = (Uint8*)malloc(m_len.GetSDLTime() * cvt.len_mult);
	memcpy(cvt.buf, m_data, m_len.GetSDLTime());
	cvt.len = m_len.GetSDLTime();
	SDL_ConvertAudio(&cvt);
	SDL_FreeWAV(m_data);
	
	SDL_LockAudio();
	m_data = cvt.buf;
	// the new length of sound after convert from 8bit to 16bit
	m_len = CSoundTime( cvt.len_cvt );
	m_pos = CSoundTime( 0 );
	SDL_UnlockAudio();
	
}
开发者ID:Brandon-Ashworth,项目名称:ZNATM-Game,代码行数:34,代码来源:Sound.cpp

示例3: mixdigi_convert_sound

/*
 * Play-time conversion. Performs output conversion only once per sound effect used.
 * Once the sound sample has been converted, it is cached in SoundChunks[]
 */
void mixdigi_convert_sound(int i) {

  SDL_AudioCVT cvt;
  Uint8 *data = GameSounds[i].data;
  Uint32 dlen = GameSounds[i].length;
  int freq = GameSounds[i].freq;
  //int bits = GameSounds[i].bits;

  if (SoundChunks[i].abuf) return; //proceed only if not converted yet

  if (data) {
    if (MIX_DIGI_DEBUG) con_printf(CON_DEBUG,"converting %d (%d)\n", i, dlen);
    SDL_BuildAudioCVT(&cvt, AUDIO_U8, 1, freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, digi_sample_rate);

    cvt.buf = malloc(dlen * cvt.len_mult);
    cvt.len = dlen;
    memcpy(cvt.buf, data, dlen);
    if (SDL_ConvertAudio(&cvt)) con_printf(CON_DEBUG,"conversion of %d failed\n", i);

    SoundChunks[i].abuf = cvt.buf;
    SoundChunks[i].alen = dlen * cvt.len_mult;
    SoundChunks[i].allocated = 1;
    SoundChunks[i].volume = 128; // Max volume = 128
  }
}
开发者ID:arbruijn,项目名称:d1xnacl,代码行数:29,代码来源:digi_mixer.c

示例4: makeAudio

void makeAudio(int i, const char* filename, unsigned char repeat, float volume){
    Audio* a = audios + i;
    if (SDL_LoadWAV(filename, &(a->spec), &(a->sounddata), &(a->soundlength)) == NULL) {
        printf("Erreur lors du chargement du fichier son: %s\n", SDL_GetError());
        return;
    }
    if (SDL_BuildAudioCVT(&(a->audioConverter), a->spec.format,
                          a->spec.channels, a->spec.freq,
                          audioSpec.currentConfig.format, audioSpec.currentConfig.channels,
                          audioSpec.currentConfig.freq) < 0) {
        printf("Impossible de construire le convertisseur audio!\n");
        return;
    }
    a->audioConverter.buf = malloc(a->soundlength * a->audioConverter.len_mult);
    a->audioConverter.len = a->soundlength;
    memcpy(a->audioConverter.buf, a->sounddata, a->soundlength);
    if (SDL_ConvertAudio(&(a->audioConverter)) != 0) {
        printf("Erreur lors de la conversion du fichier audio: %s\n", SDL_GetError());
        return;
    }
    SDL_FreeWAV(a->sounddata);
    a->sounddata = malloc(a->audioConverter.len_cvt);
    memcpy(a->sounddata, a->audioConverter.buf, a->audioConverter.len_cvt);
    free(a->audioConverter.buf);

    a->soundlength = a->audioConverter.len_cvt;
    a->soundpos = 0;
    a->repeat = repeat;
    a->currentVolume = 0;
    a->maxVolume = volume;
    a->play = a->fadeIn = a->fadeOut = 0;
}
开发者ID:firegreen,项目名称:Hovercraft-IMAC-MMSS,代码行数:32,代码来源:audios.c

示例5: file

/*
 loads a wav file (stored in 'file'), converts it to the mixer's output format,
 and stores the resulting buffer and length in the sound structure
 */
void
loadSound(const char *file, struct sound *s)
{
    SDL_AudioSpec spec;         /* the audio format of the .wav file */
    SDL_AudioCVT cvt;           /* used to convert .wav to output format when formats differ */
    int result;
    if (SDL_LoadWAV(file, &spec, &s->buffer, &s->length) == NULL) {
        fatalError("could not load .wav");
    }
    /* build the audio converter */
    result = SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
                               mixer.outputSpec.format,
                               mixer.outputSpec.channels,
                               mixer.outputSpec.freq);
    if (result == -1) {
        fatalError("could not build audio CVT");
    } else if (result != 0) {
        /*
           this happens when the .wav format differs from the output format.
           we convert the .wav buffer here
         */
        cvt.buf = (Uint8 *) SDL_malloc(s->length * cvt.len_mult);       /* allocate conversion buffer */
        cvt.len = s->length;    /* set conversion buffer length */
        SDL_memcpy(cvt.buf, s->buffer, s->length);      /* copy sound to conversion buffer */
        if (SDL_ConvertAudio(&cvt) == -1) {     /* convert the sound */
            fatalError("could not convert .wav");
        }
        SDL_free(s->buffer);    /* free the original (unconverted) buffer */
        s->buffer = cvt.buf;    /* point sound buffer to converted buffer */
        s->length = cvt.len_cvt;        /* set sound buffer's new length */
    }
}
开发者ID:Helios-vmg,项目名称:CopperRat,代码行数:36,代码来源:mixer.c

示例6: LoadAndConvertSound

/* This function loads a sound with SDL_LoadWAV and converts
   it to the specified sample format. Returns 0 on success
   and 1 on failure. */
int LoadAndConvertSound(char *filename, SDL_AudioSpec *spec,
                        sound_p sound)
{
    SDL_AudioCVT cvt;       /* format conversion structure */
    SDL_AudioSpec loaded;   /* format of the loaded data */
    Uint8 *new_buf;

    /* Load the WAV file in its original sample format. */
    if (SDL_LoadWAV(filename,
                    &loaded, &sound->samples,
                    &sound->length) == NULL) {
        printf("Unable to load sound: %s\n", SDL_GetError());
        return 1;
    }

    /* Build a conversion structure for converting the samples.
       This structure contains the data SDL needs to quickly
       convert between sample formats. */
    if (SDL_BuildAudioCVT(&cvt, loaded.format,
                          loaded.channels, loaded.freq,
                          spec->format, spec->channels,
                          spec->freq) < 0) {
        printf("Unable to convert sound: %s\n", SDL_GetError());
        return 1;
    }

    /* Since converting PCM samples can result in more data
       (for instance, converting 8-bit mono to 16-bit stereo),
       we need to allocate a new buffer for the converted data.
       Fortunately SDL_BuildAudioCVT supplied the necessary
       information. */
    cvt.len = sound->length;
    new_buf = (Uint8 *) malloc(cvt.len * cvt.len_mult);
    if (new_buf == NULL) {
        printf("Memory allocation failed.\n");
        SDL_FreeWAV(sound->samples);
        return 1;
    }

    /* Copy the sound samples into the new buffer. */
    memcpy(new_buf, sound->samples, sound->length);
    /* Perform the conversion on the new buffer. */
    cvt.buf = new_buf;
    if (SDL_ConvertAudio(&cvt) < 0) {
        printf("Audio conversion error: %s\n", SDL_GetError());
        free(new_buf);
        SDL_FreeWAV(sound->samples);
        return 1;
    }

    /* Swap the converted data for the original. */
    SDL_FreeWAV(sound->samples);
    sound->samples = new_buf;
    sound->length = sound->length * cvt.len_mult;

    /* Success! */
    printf("’%s’ was loaded and converted successfully.\n",
           filename);
    return 0;
}
开发者ID:jollywing,项目名称:girl-sword,代码行数:63,代码来源:SdlSys.cpp

示例7: LoadSound

static void LoadSound(int index, char *file) {
	SDL_AudioSpec wave;
	Uint8 *data;
	Uint32 dlen;
	SDL_AudioCVT cvt;
	/* Load the sound file and convert it to 16-bit stereo at 22kHz */
	if (SDL_LoadWAV(file, &wave, &data, &dlen) == NULL) {
		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
		return;
	}
	SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100);
	cvt.buf = malloc(dlen*cvt.len_mult);
	memcpy(cvt.buf, data, dlen);
	cvt.len = dlen;
	SDL_ConvertAudio(&cvt);
	SDL_FreeWAV(data);
	
	if (sounds[index].data) {
		free(sounds[index].data);
	}
	SDL_LockAudio();
	sounds[index].data = cvt.buf;
	sounds[index].dlen = cvt.len_cvt;
	sounds[index].dpos = cvt.len_cvt;
	SDL_UnlockAudio();
}
开发者ID:pavelrevak,项目名称:train-snake,代码行数:26,代码来源:vlak.c

示例8: init_audio

bool init_audio( void )
{
	if (audio_disabled)
		return false;
	
	SDL_AudioSpec ask, got;
	
	ask.freq = freq;
	ask.format = (BYTES_PER_SAMPLE == 2) ? AUDIO_S16SYS : AUDIO_S8;
	ask.channels = 1;
	ask.samples = 512;
	ask.callback = audio_cb;
	
	printf("\trequested %d Hz, %d channels, %d samples\n", ask.freq, ask.channels, ask.samples);
	
	if (SDL_OpenAudio(&ask, &got) == -1)
	{
		fprintf(stderr, "error: failed to initialize SDL audio: %s\n", SDL_GetError());
		audio_disabled = true;
		return false;
	}
	
	printf("\tobtained  %d Hz, %d channels, %d samples\n", got.freq, got.channels, got.samples);
	
	SDL_BuildAudioCVT(&audio_cvt, ask.format, ask.channels, ask.freq, got.format, got.channels, got.freq);
	
	opl_init();
	
	SDL_PauseAudio(0); // unpause
	
	return true;
}
开发者ID:intervigilium,项目名称:android-opentyrian,代码行数:32,代码来源:loudness.c

示例9: SDL_BuildAudioCVT

//Play sound straight from file
void cSound::PlaySound(char *file)
{
	if(gGameWorld.gMenu.m_sound == 0){return;}

	SDL_AudioSpec wave;
	Uint8 *data;
	Uint32 dlen;
	SDL_AudioCVT cvt;

	/* Load the sound file and convert it to 16-bit stereo at 22kHz */
	if(SDL_LoadWAV(file, &wave, &data, &dlen) == NULL){return;}

	SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 22050);
	cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult);
	memcpy(cvt.buf, data, dlen);
	cvt.len = dlen;
	SDL_ConvertAudio(&cvt);
	SDL_FreeWAV(data);

	/* Put the sound data in the slot (it starts playing immediately) */
	if(snd.data != NULL){free(snd.data);}

	SDL_LockAudio();
	snd.data = cvt.buf;
	snd.dlen = cvt.len_cvt;
	snd.dpos = 0;
	SDL_UnlockAudio();
}
开发者ID:blchinezu,项目名称:EZX-Projects,代码行数:29,代码来源:Sound.cpp

示例10:

bool Audio::CVT::Build(const Audio::Spec & src, const Audio::Spec & dst)
{
    if(1 == SDL_BuildAudioCVT(this, src.format, src.channels, src.freq, dst.format, dst.channels, dst.freq)) return true;

    std::cerr << "Audio::CVT::Build: " << SDL_GetError() << std::endl;
    return false;
}
开发者ID:asimonov-im,项目名称:fheroes2,代码行数:7,代码来源:audio.cpp

示例11: Audio_new_cvt

void Audio_new_cvt (SDL_AudioCVT* cvt, SDL_AudioFormat format, Uint8 channels, int freq)
{
	SDL_BuildAudioCVT(
		cvt,
		format, channels, freq,
		_audio.spec.format, _audio.spec.channels, _audio.spec.freq
	);
}
开发者ID:Blaizer,项目名称:Neverhood,代码行数:8,代码来源:Audio.c

示例12: SDL_GetError

// This function loads a sound with SDL_LoadWAV and converts
// it to the specified sample format. 
void Sound::loadAndConvertSound(char *filename, SDL_AudioSpec spec) {
   SDL_AudioCVT cvt;           // audio format conversion structure
   SDL_AudioSpec loaded;       // format of the loaded data
   Uint8 *new_buf;

   // Load the WAV file in its original sample format.
   if (SDL_LoadWAV(filename,
      &loaded, &samples, &length) == NULL) {
      std::cout << "Unable to load sound: " << SDL_GetError() << std::endl;
//      throw string("Unable to load sound: ") + string(SDL_GetError());
   }

   // Build a conversion structure for converting the samples.
   // This structure contains the data SDL needs to quickly
   // convert between sample formats.
   if (SDL_BuildAudioCVT(&cvt, loaded.format,
      loaded.channels,
      loaded.freq,
      spec.format, spec.channels, spec.freq) < 0) {
         std::cout << "Unable to convert sound: " << SDL_GetError() << std::endl;
//         throw string("Unable to convert sound: ") + string(SDL_GetError());
   }

   // Since converting PCM samples can result in more data
   // (for instance, converting 8-bit mono to 16-bit stereo),
   // we need to allocate a new buffer for the converted data.
   // Fortunately SDL_BuildAudioCVT supplied the necessary
   // information.
   cvt.len = length;
   new_buf = (Uint8 *) malloc(cvt.len * cvt.len_mult);
   if (new_buf == NULL) {
      SDL_FreeWAV(samples);
      std::cout << "Memory allocation failed." << std::endl;
//      throw string("Memory allocation failed.");
   }

   // Copy the sound samples into the new buffer.
   memcpy(new_buf, samples, length);

   // Perform the conversion on the new buffer.
   cvt.buf = new_buf;
   if (SDL_ConvertAudio(&cvt) < 0) {
      free(new_buf);
      SDL_FreeWAV(samples);
      std::cout << "Audio conversion error: " << SDL_GetError() << std::endl;
//      throw string("Audio conversion error: ") + string(SDL_GetError());
   }

   // Swap the converted data for the original.
   SDL_FreeWAV(samples);
   samples = new_buf;
   length = length * cvt.len_mult;

   // Success!
   printf("'%s' was loaded and converted successfully.\n", filename);

}
开发者ID:klt592,项目名称:GTFinal,代码行数:59,代码来源:SoundManager.cpp

示例13: pcm_dma_apply_settings_nolock

static void pcm_dma_apply_settings_nolock(void)
{
    cvt_status = SDL_BuildAudioCVT(&cvt, AUDIO_S16SYS, 2, pcm_sampr,
                    obtained.format, obtained.channels, obtained.freq);

    if (cvt_status < 0) {
        cvt.len_ratio = (double)obtained.freq / (double)pcm_sampr;
    }
}
开发者ID:eisnerd,项目名称:rockbox,代码行数:9,代码来源:pcm-sdl.c

示例14: fprintf

void Jukebox::PlayZAttack()
//Function to play the background theme song specifically.
{
	
	const char *file = Z_ATTACK_SOUND;
	int index;
	SDL_AudioSpec wave;
	Uint8 *data;
	Uint32 dlen;
	SDL_AudioCVT cvt;

	//Look for an empty (or finished) sound slot
	for ( index=0; index<NUM_SOUNDS; ++index ) {
		
		if ( sounds[index].dpos == sounds[index].dlen ) {
			
			break;
			
		}
		
	}
	if ( index == NUM_SOUNDS ) {
		
		return;

	}
	
	// Load the sound file and convert it to 16-bit stereo at 22kHz
	if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
		
		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
		return;
		
	}
	
	SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 22050);
    cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult);
	memcpy(cvt.buf, data, dlen);
	cvt.len = dlen;
	SDL_ConvertAudio(&cvt);
	SDL_FreeWAV(data);

	// Put the sound data in the slot (it starts playing immediately)
	if (sounds[index].data) {
		
		free(sounds[index].data);
		
	}
	
	SDL_LockAudio();
	sounds[index].data = cvt.buf;
	sounds[index].dlen = cvt.len_cvt;
	sounds[index].dpos = 0;
	SDL_UnlockAudio();
	
}
开发者ID:danthemango,项目名称:Kitten-Pirateer,代码行数:56,代码来源:Jukebox.cpp

示例15: ExpandSoundData

static void ExpandSoundData(byte *data,
                            int samplerate,
                            int length,
                            Mix_Chunk *destination)
{
    SDL_AudioCVT convertor;
    
    if (samplerate <= mixer_freq
     && ConvertibleRatio(samplerate, mixer_freq)
     && SDL_BuildAudioCVT(&convertor,
                          AUDIO_U8, 1, samplerate,
                          mixer_format, mixer_channels, mixer_freq))
    {
        convertor.buf = destination->abuf;
        convertor.len = length;
        memcpy(convertor.buf, data, length);

        SDL_ConvertAudio(&convertor);
    }
    else
    {
        Sint16 *expanded = (Sint16 *) destination->abuf;
        int expanded_length;
        int expand_ratio;
        int i;

        // Generic expansion if conversion does not work:
        //
        // SDL's audio conversion only works for rate conversions that are
        // powers of 2; if the two formats are not in a direct power of 2
        // ratio, do this naive conversion instead.

        // number of samples in the converted sound

        expanded_length = ((uint64_t) length * mixer_freq) / samplerate;
        expand_ratio = (length << 8) / expanded_length;

        for (i=0; i<expanded_length; ++i)
        {
            Sint16 sample;
            int src;

            src = (i * expand_ratio) >> 8;

            sample = data[src] | (data[src] << 8);
            sample -= 32768;

            // expand 8->16 bits, mono->stereo

            expanded[i * 2] = expanded[i * 2 + 1] = sample;
        }
    }
}
开发者ID:JohnnyonFlame,项目名称:odamex,代码行数:53,代码来源:i_sound.cpp


注:本文中的SDL_BuildAudioCVT函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。