Example #1
0
static int
MINTSTFA_CheckAudio(_THIS)
{
    int i;

    DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",
                 SDL_AUDIO_BITSIZE(this->spec.format)));
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
    DEBUG_PRINT(("big endian=%d, ",
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));

    if (SDL_AUDIO_BITSIZE(this->spec.format) > 16) {
        this->spec.format = AUDIO_S16SYS;       /* clamp out int32/float32 ... */
    }

    if (this->spec.channels > 2) {
        this->spec.channels = 2;        /* no more than stereo! */
    }

    /* Check formats available */
    MINTAUDIO_freqcount = 0;
    for (i = 0; i < 16; i++) {
        SDL_MintAudio_AddFrequency(this, freqs[i], 0, i, -1);
    }

#if 1
    for (i = 0; i < MINTAUDIO_freqcount; i++) {
        DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
                     i, MINTAUDIO_frequencies[i].frequency,
                     MINTAUDIO_frequencies[i].masterclock,
                     MINTAUDIO_frequencies[i].predivisor));
    }
#endif

    MINTAUDIO_numfreq = SDL_MintAudio_SearchFrequency(this, this->spec.freq);
    this->spec.freq = MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;

    DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",
                 SDL_AUDIO_BITSIZE(this->spec.format)));
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
    DEBUG_PRINT(("big endian=%d, ",
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));

    return 0;
}
Example #2
0
static void
MINTSTFA_InitAudio(_THIS)
{
    void *buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
    void *oldpile = (void *) Super(0);

    /* Stop replay */
    cookie_stfa->sound_enable = STFA_PLAY_DISABLE;

    /* Select replay format */
    cookie_stfa->sound_control =
        MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
    if (SDL_AUDIO_BITSIZE(this->spec.format) == 8) {
        cookie_stfa->sound_control |= STFA_FORMAT_8BIT;
    } else {
        cookie_stfa->sound_control |= STFA_FORMAT_16BIT;
    }
    if (this->spec.channels == 2) {
        cookie_stfa->sound_control |= STFA_FORMAT_STEREO;
    } else {
        cookie_stfa->sound_control |= STFA_FORMAT_MONO;
    }
    if (SDL_AUDIO_ISSIGNED(this->spec.format) != 0) {
        cookie_stfa->sound_control |= STFA_FORMAT_SIGNED;
    } else {
        cookie_stfa->sound_control |= STFA_FORMAT_UNSIGNED;
    }
    if (SDL_AUDIO_ISBIGENDIAN(this->spec.format) != 0) {
        cookie_stfa->sound_control |= STFA_FORMAT_BIGENDIAN;
    } else {
        cookie_stfa->sound_control |= STFA_FORMAT_LITENDIAN;
    }

    /* Set buffer */
    cookie_stfa->sound_start = (unsigned long) buffer;
    cookie_stfa->sound_end = (unsigned long) (buffer + this->spec.size);

    /* Set interrupt */
    cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt;

    /* Restart replay */
    cookie_stfa->sound_enable = STFA_PLAY_ENABLE | STFA_PLAY_REPEAT;

    Super(oldpile);

    DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
}
Example #3
0
static int
SNDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
{
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
    struct sio_par par;
    int status;

    this->hidden = (struct SDL_PrivateAudioData *)
        SDL_malloc(sizeof(*this->hidden));
    if (this->hidden == NULL) {
        return SDL_OutOfMemory();
    }
    SDL_memset(this->hidden, 0, sizeof(*this->hidden));

    this->hidden->mixlen = this->spec.size;

    /* !!! FIXME: SIO_DEVANY can be a specific device... */
    if ((this->hidden->dev = SNDIO_sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL) {
        SNDIO_CloseDevice(this);
        return SDL_SetError("sio_open() failed");
    }

    SNDIO_sio_initpar(&par);

    par.rate = this->spec.freq;
    par.pchan = this->spec.channels;
    par.round = this->spec.samples;
    par.appbufsz = par.round * 2;

    /* Try for a closest match on audio format */
    status = -1;
    while (test_format && (status < 0)) {
        if (!SDL_AUDIO_ISFLOAT(test_format)) {
            par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0;
            par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0;
            par.bits = SDL_AUDIO_BITSIZE(test_format);

            if (SNDIO_sio_setpar(this->hidden->dev, &par) == 0) {
                continue;
            }
            if (SNDIO_sio_getpar(this->hidden->dev, &par) == 0) {
                SNDIO_CloseDevice(this);
                return SDL_SetError("sio_getpar() failed");
            }
            if (par.bps != SIO_BPS(par.bits)) {
                continue;
            }
            if ((par.bits == 8 * par.bps) || (par.msb)) {
                status = 0;
                break;
            }
        }
        test_format = SDL_NextAudioFormat();
    }

    if (status < 0) {
        SNDIO_CloseDevice(this);
        return SDL_SetError("sndio: Couldn't find any hardware audio formats");
    }

    if ((par.bps == 4) && (par.sig) && (par.le))
        this->spec.format = AUDIO_S32LSB;
    else if ((par.bps == 4) && (par.sig) && (!par.le))
        this->spec.format = AUDIO_S32MSB;
    else if ((par.bps == 2) && (par.sig) && (par.le))
        this->spec.format = AUDIO_S16LSB;
    else if ((par.bps == 2) && (par.sig) && (!par.le))
        this->spec.format = AUDIO_S16MSB;
    else if ((par.bps == 2) && (!par.sig) && (par.le))
        this->spec.format = AUDIO_U16LSB;
    else if ((par.bps == 2) && (!par.sig) && (!par.le))
        this->spec.format = AUDIO_U16MSB;
    else if ((par.bps == 1) && (par.sig))
        this->spec.format = AUDIO_S8;
    else if ((par.bps == 1) && (!par.sig))
        this->spec.format = AUDIO_U8;
    else {
        SNDIO_CloseDevice(this);
        return SDL_SetError("sndio: Got unsupported hardware audio format.");
    }

    this->spec.freq = par.rate;
    this->spec.channels = par.pchan;
    this->spec.samples = par.round;

    /* Calculate the final parameters for this audio specification */
    SDL_CalculateAudioSpec(&this->spec);

    /* Allocate mixing buffer */
    this->hidden->mixlen = this->spec.size;
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
    if (this->hidden->mixbuf == NULL) {
        SNDIO_CloseDevice(this);
        return SDL_OutOfMemory();
    }
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen);

    if (!SNDIO_sio_start(this->hidden->dev)) {
        return SDL_SetError("sio_start() failed");
    }

    /* We're ready to rock and roll. :-) */
    return 0;
}
Example #4
0
static int
COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
{

    fprintf(stderr, "OpenDevice\n");
    AudioStreamBasicDescription strdesc;
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
    int valid_datatype = 0;

    /* Initialize all variables that we clean on shutdown */
    this->hidden = (struct SDL_PrivateAudioData *)
        SDL_malloc((sizeof *this->hidden));
    if (this->hidden == NULL) {
        SDL_OutOfMemory();
        fprintf(stderr, "OutOfMemory\n");
        return (0);
    }
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));

    /* Setup a AudioStreamBasicDescription with the requested format */
    SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
    strdesc.mFormatID = kAudioFormatLinearPCM;
    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
    strdesc.mChannelsPerFrame = this->spec.channels;
    strdesc.mSampleRate = this->spec.freq;
    strdesc.mFramesPerPacket = 1;

    while ((!valid_datatype) && (test_format)) {
        this->spec.format = test_format;
        /* Just a list of valid SDL formats, so people don't pass junk here. */
        switch (test_format) {
        case AUDIO_U8:
        case AUDIO_S8:
        case AUDIO_U16LSB:
        case AUDIO_S16LSB:
        case AUDIO_U16MSB:
        case AUDIO_S16MSB:
        case AUDIO_S32LSB:
        case AUDIO_S32MSB:
        case AUDIO_F32LSB:
        case AUDIO_F32MSB:
            valid_datatype = 1;
            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;

            if (SDL_AUDIO_ISFLOAT(this->spec.format))
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
            break;
        }
    }

    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
        COREAUDIO_CloseDevice(this);
        fprintf(stderr, "Unsupported audio format\n");
        return 0;
    }

    strdesc.mBytesPerFrame =
        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
    strdesc.mBytesPerPacket =
        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;

    if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
        COREAUDIO_CloseDevice(this);
        fprintf(stderr, "prepare_audiounit failed\n");
        return 0;               /* prepare_audiounit() will call SDL_SetError()... */
    }

    return 1;                   /* good to go. */
}
Example #5
0
SDL2SoundCore::SDL2SoundCore(
              SoundMixerTypes::SoundMixerType soundMixerType__,
              SoundStateTypes::SoundStateType soundStateType__)
  : SoundCore(soundMixerType__,
              soundStateType__),
    device(0) {

  // Initialize the audio device
  desired.freq = desiredFreq;
  desired.format = desiredFormat;
  desired.channels = desiredChannels;
  desired.samples = desiredSamples;
  desired.callback = audioCallback;
  desired.userdata = state_;
  
/*  std::cout << "desired" << std::endl;
  std::cout << "freq: " << desired.freq << std::endl;
  std::cout << "format: " << desired.format << std::endl;
  std::cout << "channels: " << desired.channels << std::endl;
  std::cout << "samples: " << desired.samples << std::endl; */
  
  device = SDL_OpenAudioDevice(NULL,
                               0,
                               &desired,
                               &obtained,
                               SDL_AUDIO_ALLOW_ANY_CHANGE);
                               
/*  if (device == 0) {
    std::cerr << "error" << std::endl;
  }
  else {
    std::cout << "obtained" << std::endl;
    std::cout << "freq: " << obtained.freq << std::endl;
    std::cout << "format: " << obtained.format << std::endl;
    std::cout << "channels: " << obtained.channels << std::endl;
    std::cout << "samples: " << obtained.samples << std::endl;
  } */
  
  // Translate format settings
  
  SignednessTypes::SignednessType dataSignedness = SignednessTypes::nosign;
  if ( SDL_AUDIO_ISSIGNED( desired.format ) ) {
    dataSignedness = SignednessTypes::sign;
  }
  
  EndiannessTypes::EndiannessType dataEndianness = EndiannessTypes::little;
  if ( SDL_AUDIO_ISBIGENDIAN( desired.format ) ) {
    dataEndianness = EndiannessTypes::big;
  }
  
  // Initialize the mixer
  state_->mixer().initialize(obtained.samples,
                            desiredChannels,  // channels
                                // (SDL doesn't seem to record this correctly?)
                            dataSignedness,  // signedness
                            dataEndianness,  // endianness
                            (desired.format 
                              & SDL_AUDIO_MASK_BITSIZE) / 8,  // bytes per sample
                            desired.freq); // frequency
                            
  
  SDL_PauseAudioDevice(device, 0);
}