Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2019-05

Soliciting help with next tutoring project, playing sounds and music

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anthony Walter

In my current thread on the subject of "Next tutoring project" I believe I've nailed down my idea. The project will be a node based sound and effect generation program where students can create functions that either create tones or effects.

I also want to add the ability to apply effects to sound files, in to generated tones, and from my previous experience with sound files I wanted to ask for some help from you the community to add this specific feature into the new music project for teaching kids how to program.

What I need is the following:

A function to take an mp3 stream and given a time and a duration to convert the mp3 data into left and right audio samples of a specific samples per second and sample type.

So for example, if I load an entire mp3 into a memory stream it might be called:

var
  MusicFile: TMemoryStream;
..
  MusicFile := TMemoryStream.Create;
  MusicFile.LoadFromStream('my_music.mp3');
  WriteLn('The music file is ', Mp3StreamDuration(MusicFile), ' long in
seconds');

I would need these functions:

{ Return the duration of the music or sounds in an Mp3 stream in seconds }
function Mp3StreamDuration(Stream: TStream): Single;

{ Mp3StreamSamples outputs left and right audio samples from an mp3 stream given a Start,
Duration, SamplesPerSecond, and buffer information. Buffer count will be = Duration *
SamplesPerSecond rounded up to the nearest whole value }

type
  TSoundSample = record
    Left: SmallInt;
    Right: SmallInt;
  end;
  PSoundSample = ^TSoundSample;
procedure Mp3StreamSamples(Stream: TStream; Start, Duration: Single;
SamplesPerSecond: Integer; Buffer: PSoundSample; BufferCount: Integer);

If these functions could be repeated for the wav file format or other formats (ogg and such) that would be much appreciate.

By the way, the mp3 format is now free of any licensing restrictions.

If anyone has any questions or would like to help let me know. I can provide you with a test application to output the sound into the platform independent audio system if you need it for testing.

Thanks again everyone.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190507/1a992aef/attachment.html>

Reply by email

José Mejuto
to Anthony Walter
Anthony Walter wrote 10 lines
In my current thread on the subject of "Next tutoring project" I believe I've nailed down my idea.  The project will be a node based sound and effect generation program where students can create functions that either create tones or effects. I also want to add the ability to apply effects to sound files, in to generated tones, and from my previous experience with sound files I wanted to ask for some help from you the community to add this specific feature into the new music project for teaching kids how to program.

Hello,

Use Bass library http://www.un4seen.com/

License extract:

BASS is free for non-commercial use. If you are a non-commercial entity (eg. an individual) and you are not making any money from your product (through sales, advertising, etc), then you can use BASS in it for free. Otherwise, one of the following licences will be required. ....
....
....

Reply by email

Anthony Walter
to José Mejuto

Jose,

Thank you for the suggestion. I've used both BASS and FMOD extensively in the past. Those library are for playing sounds from files or streams using their abstracted concepts of channels. What I am looking for is something a bit more primitive, and that is manipulating sample sounds through a series of functions, which is something both of those are not suitable for. That is I need to be able to extract a section of an mp3 or wav file and convert it to left and right audio samples (in my case a pair of SmallInt values) at a given samples per second. Then that buffer of left and right SmallInt values are fed to student functions as a mix, where they can an echo to a left or right sample, pan the left or right sample, chorus the left or right sample, and so on.

For example a student might create a simple pan effect by writing:

procedure PanEffect(var Sample: TAudioSample; Time, Level: Single);
var
  Mix: Single;
begin
  if Level < 0.5 then
  begin
    Mix := 1 - Level / 0.5;
    Sample.Left := RoundSmallInt(Sample.Left + Sample.Right * Mix);
    Mix := Level / 0.5;
    Sample.Right := RoundSmallInt(Sample.Right * Mix);
  end
  else
  begin
    Level := 1 - Level;
    Mix := 1 - Level / 0.5;
    Sample.Right := RoundSmallInt(Sample.Right + Sample.Left * Mix);
    Mix := Level / 0.5;
    Sample.Left := RoundSmallInt(Sample.Left * Mix);
   end;
  end;
end;

Several effects can be chained together to alter the samples, then mix together all the samples from the various streams or tones being played and feed the mixed together samples to the sound driver producing the final sound effect.

From the perspective of the student their are either writing Tone

procedures or Effect procedures, and from the application perspective we are working with samples per active virtual keyboard key and mixing them down into a single stereo buffer of left and right SmallInts which are then dispatched to the sound driver.

In the case of mp3s or wav finds, I need a function to decode a part of a stream into a range of samples (left and right SmallInts). I don't need a black box library to play sounds with licensing attached and I've like to minimize the dependency on external restrictive third party libraries. And finally both FMOD and BASS introduce a lot of complexity if the goal is simply to retrieve audio samples as opposed to using them for playing streams using their abstracted design of sound channels. -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190507/9ed7631a/attachment-0001.html>

Reply by email

José Mejuto
to Anthony Walter
Anthony Walter wrote 10 lines
Jose, Thank you for the suggestion. I've used both BASS and FMOD extensively in the past. Those library are for playing sounds from files or streams using their abstracted concepts of channels. What I am looking for is something a bit more primitive, and that is manipulating sample sounds through a series of functions, which is something both of those are not suitable for. That is I need to be able to extract a section of an mp3 or wav file and convert it to left and right audio samples (in my case a pair of SmallInt values) at a given samples per second. Then that buffer

[...]

Hello,

I suggested BASS to decode audio files and get a raw PCM audio, not to play it directly. Raw PCM in a WAV file is almost trivial to read but mp3 and others are too complex and you always be dependent of a external .DLL/.so, many times with their own dependencies.

Of course, you can use, in example, ffmpeg command line program to convert any mp3/ogg/whatever to raw PCM files and use them from a "cache" of decompressed PCM files.

Reply by email

Anthony Walter
to José Mejuto

Jose,

I'd really like to reduce dependencies and not use black box code that comes with licencing restrictions and requires bundling of additional third party binary executable files.

Thanks for the suggestion though.

If anyone is actually interested in how to decode an mp3 into left and right audio samples, this single C file project handles this task well. It just need translation to Pascal.

https://github.com/technosaurus/PDMP3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190508/9984d13c/attachment.html>

Reply by email

Mehmet Erol Sanliturk
to Anthony Walter

On Wed, May 8, 2019 at 3:02 PM Anthony Walter via lazarus <

Show quoted text 19 lines
wrote: Jose, I'd really like to reduce dependencies and not use black box code that comes with licencing restrictions and requires bundling of additional third party binary executable files. Thanks for the suggestion though. If anyone is actually interested in how to decode an mp3 into left and right audio samples, this single C file project handles this task well. It just need translation to Pascal. https://github.com/technosaurus/PDMP3 -- _______________________________________________

I think you know SDL2 :
https://www.libsdl.org/

There are its bindings to many languages :

https://www.libsdl.org/languages.php
SDL Language Bindings ( including Pascal )

Perhaps they may be useful for you .

Mehmet Erol Sanliturk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190508/34fe8736/attachment.html>

Reply by email

Anthony Walter
to Mehmet Erol Sanliturk

Mehmet,

Thanks for that information. You may not be aware of it, but I am the author of the first Pascal translation
<https://github.com/sysrpl/Bare.Game/blob/master/source/bare.interop.sdl2.pas>of the SDL 2 headers. What SDL 2 provides is a means to create cross platform graphical and audio applications. Unfortunately it does not provide an MP3 sample decoder, and it actually depends on yet another external library (libmpg123) to play MP3 audio.

To be clear: Playing MP3 audio Decoding MP3 audio samples.

Also, again as I replied previously, I am trying to avoid linking to black box third party libraries and am looking for actual Pascal code to handle the actual decoding of an MP3 stream into left and right audio samples. -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190508/2950770a/attachment.html>

Reply by email