4

I am converting a 30+ year old Turbo Pascal program to FireMonkey. One of the things that appears to have no equivalent is the SOUND command. I found elsewhere on StackOverflow that it can be replaced with the Windows beep (in VCL), but I need a solution that works for FireMonkey on Android. My program displays lyrics along with each sound, so I can't just record the sound and play MP3 files. I also want to maintain that clunky one-note 8-bit sound of the SOUND command in Turbo Pascal. The main command is Sound(Round(Frequency)). Any idea how to mimic that in FireMonkey? Here is the original code for playing each note, in case it helps:

procedure Playnote(speed,Octave,Note,Duration: integer);
var
   Frequency : real;
   I         : integer;
begin
   if note = 0 then sleep(speed*duration)   // was "delay" in Turboo Pascal
   else begin
      Frequency := 32.625;
      for I := 1 to Octave do Frequency := Frequency * 2;
      for I := 1 to Note - 1 do Frequency := Frequency * 1.059463094;
      if Duration <> 0 then begin
         if soundon then Sound(Round(Frequency));      
         sleep(speed*Duration);    // was "delay" in Turboo Pascal
         NoSound;                                     
      end
      else if soundon then Sound(Round(Frequency));    
   end;
end;    {end PLAYNOTE }

For a little more background, it's a text adventure game. There is a saloon with a jukebox. Playing songs on the jukebox gives you clues from the lyrics, which display as the song plays.

Mad Martian
  • 109
  • 6
  • 1
    Sound is basically programming the PC screamer (not the sound card, but the "beep" of the internal speaker). Many PCs don't have that one anymore, and Androids never had it. Probably you will have to generate a wav for it. Research Android's sound apis. – Marco van de Voort Jul 11 '16 at 07:49
  • You need to generate the samples using the `Sin` math function then send the samples to the audio device. Either generate a wav object in memory and play it, or open a audio output stream where you feed it with your samples (an 0's when there is nothing to play). – Hans Jul 11 '16 at 08:18
  • Take a look at how I do this in my [`Console` unit](http://www.rvelthuis.de/programs/console.html) for Delphi. The principle is the same. It should probably work on other OSes too, but with different function calls. Basically, on `Sound` I set a state, and if that is set, then `Delay()` actually plays the sound for a set time (the time of the delay). `NoSound()` then resets the state. There should be an equivalent of `Beep()` in almost any OS. – Rudy Velthuis Jul 11 '16 at 16:21
  • @Hans: Sin is really unnecessary. A sound with a block wave (half 1, half -1, but then scaled) has many more harmonics and can be heard much better. For such a low level sound, any aliasing will probably not matter much either, so there is no need to filter it. Now, if you really want to get close to a Sin, a triangular wave will do. – Rudy Velthuis Jul 11 '16 at 16:30
  • http://stackoverflow.com/questions/6462105/how-do-i-access-androids-default-beep-sound – Rudy Velthuis Jul 11 '16 at 16:32
  • 1
    http://stackoverflow.com/questions/9106276/android-how-to-generate-a-frequency – Rudy Velthuis Jul 11 '16 at 18:21
  • http://stackoverflow.com/questions/2413426/playing-an-arbitrary-tone-with-android – Rudy Velthuis Jul 11 '16 at 18:30
  • Rudy, your console unit sounds awesome! I will have to play with it. Any chance of getting that working in FireMonkey? My Turbo Pascal program is totally written for console so that could save me a lot of time and present the game close to its original form. – Mad Martian Jul 13 '16 at 06:14
  • 1
    FireMonkey is a GUI framework. Not very useful for console programs. If you want to emulate a console, my unit is not very useful. It only works on real Windows consoles. – Rudy Velthuis Aug 05 '16 at 20:57

0 Answers0