FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SDL_OpenAudio callback function get's len equal to 0

 
Post new topic   Reply to topic     Forum Index -> Derelict
View previous topic :: View next topic  
Author Message
Zardoz



Joined: 28 May 2011
Posts: 17
Location: Spain

PostPosted: Tue Jul 26, 2011 12:38 pm    Post subject: SDL_OpenAudio callback function get's len equal to 0 Reply with quote

Hi. I'm trying to generate tones using SDL Audio directly. I make a qucik C Program in C that works, now next was translate to D.

For I can see in console, the callback functions it's called, but it's gets a buffer length of 0, so not generate any sound. The C equivalent program works flawless... Using dmd 2.053 on Ubuntu 11.04 x64

Here is the code :
Code:

import std.stdio;
import std.string;
import std.math;
import std.conv;
 
import derelict.sdl.sdl;
import derelict.util.compat;
import derelict.util.exception;

pragma(lib, "/usr/local/lib/derelict2/libDerelictUtil.a");

void create_tone(void *userdata, Uint8 *stream, int len) {
  static double angle = 0.0 ;
  int i = 0 ;
  stderr.writeln("Filling ", len, " ", stream, " ", userdata);
  for(i=0;i<len;i++) {
    *stream++ = to!Uint8(255.0*cos(angle)) ;
    angle += PI/200.0 ;
    if( angle > 2.0*PI ) {
      angle -= 2.0*PI ;
    }
  }
}

int main () {
  writeln("Loading...");
 
  DerelictSDL.load();
   
  if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    throw new Exception("Unable to init SDL: "~ toDString(SDL_GetError()));
  }
 
  /* Open the audio device */
  SDL_AudioSpec desired, obtained;
 
  desired.freq = 44100;
  desired.format = AUDIO_S16;
  desired.channels = 1;
  desired.samples = 512;       
  desired.callback = &create_tone;
  desired.userdata = null;
 
  /* Open the audio device and start playing sound! */
  if ( SDL_OpenAudio(&desired, &obtained) < 0 ) {
    throw new Exception("Unable to open audio: "~ toDString(SDL_GetError()));
  }
 
  SDL_PauseAudio(0);
   
  scope(exit) {
    SDL_CloseAudio();
    SDL_Quit();
  }
 
  writeln(obtained.samples, " ", obtained.freq);
  SDL_Delay(250);
 
  return 0;
}
Back to top
View user's profile Send private message
aldacron



Joined: 05 May 2004
Posts: 1322
Location: Seoul, South Korea

PostPosted: Wed Jul 27, 2011 4:57 am    Post subject: Reply with quote

I suspect it might have something to do with the fact that your call back is not properly declared. It needs to have an "extern(C)" in front of it. D functions are not C functions. Anytime you pass a function pointer to a C API, the function itself should be declared as extern(C). The compiler really shouldn't allow you to pass a pointer to a D function when a pointer to a C function is expected. IIRC, there's a bug report for it. But until it's fixed, you need to be careful about this sort of thing.

If that's not the problem, then I'm afraid I have no idea.
_________________
The One With D | The One With Aldacron | D Bits
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Derelict All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group