Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  music in 4k games  (Read 2067 times)
0 Members and 1 Guest are viewing this topic.
Offline g666

Junior Devvie





« Posted 2006-01-06 22:17:36 »

What are the options?

I have tried using midichannel.noteOn() but it makes the game very juddery as soon as the sysnthesizer.open() is called, even if it takes place in a separate thread. This is on 1.4, on 1.5 (jre ) it is still slow but no sound (no soundbank?).

desperately seeking sanity
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2006-01-06 22:56:05 »

If you don't need anything fancy like different "instruments" you could write something that generates sine or square waves at the right frequency to play a little melody.  If you make 2d arrays one deimension being the note and the other being the samples you can write the samples to an audio  line based on looking up the note.

Offline DonaldEKnuth

Junior Devvie





« Reply #2 - Posted 2006-01-06 23:35:23 »

swpalmer: That sounds like a nice way to get some sounds, if you have the time could you please elaborate a bit more on it?

My 4k 2006 Entry: Sokoban4k
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #3 - Posted 2006-01-07 07:35:41 »

Generating a waveform in a buffer (array) & writing it directly to a line is probably the most space efficient way of getting sampled sound effects.  However for music, you will need to keep checking the line & topping it up with the next note, which will use a bit more code.  This also does not allow polyphonic sound (more than one note).  To avoid clicks, each note must have attack & decay to zero.

I have had quite good results by generating an array of buffers with samples for each note across several octaves, then creating a buffer as long as the music track, then numerically adding the samples to it as required to make up the track.  Create an AudioClip using the music track buffer & command it to loop indefinitely.  SharpShooter16k used this technique.  There is a slight difficulty when adding samples to the music track.  This requires 16bit unsigned addition, but the buffer is in bytes.  In SharpShooter I was already using NIO buffers for openGL & used those to map a byte buffer to a short buffer.  For a 4k program, my best solution so far is to create the track using a normal int array, and then manually copy the values into a byte array.

You also need to store a set of note data to generate the track (frequency, length, start time).  Currently mine is packed into an integer array, but this is very space inefficient.  It would be better to encode this into a very long string.  Must do this sometime Smiley  Oh... and you have to compose the tune of course.  It helps to have some actual talent in this area Grin

Unfortunately, having done all the above uses over 500 compressed bytes; much more for a long track.  So It hasn't actually made it into any of my entries.  Maybe soon, but I'm really really short on spare time Sad

Alan

Time flies like a bird. Fruit flies like a banana.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #4 - Posted 2006-01-08 00:13:05 »

Here's the basic audio code that I'm using for bungie sound effects, altered to  play "music"
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JFrame;

/*
 * Created on 6-Jan-2006 by Scott Palmer
 *
 */


public class A extends JFrame implements Runnable {
   //static int threadCount;

   public static void main(String[] A) throws Exception {
      new A();
   }

   A() throws Exception {
      // start the music
      new Thread(this).start();

      do {
         while (true) {
            // get input
            ;
            // compute stuff
            ;
            // draw stuff
            ;
            Thread.sleep(10);
         }
      } while (false);
   }

   public void run() {
      // Uncomment below for polyphonic sound.  Use voiceNumber
      // as an index to the sound track to play, or to create
      // a server thread for sound effects.

      // int voiceNumber = threadCount;
      // while(++threadCount < N)
      //     new Thread(this).start();

      try {
         final int a = 18000; // note amplitude
         final double z = Math.PI / 44000;
         // I'm using a frequency table for the notes here
         // algorithmic note generation is likely better
         final double f[] = { 261.63, // C4
               294.33, // D4
               327.03, // E4
               348.83, // F4
               392.44, // G4
               436.05, // A4
               490.55, // B4
         };

         SourceDataLine L = (SourceDataLine) AudioSystem
               .getLine(new DataLine.Info(SourceDataLine.class,
                     new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                           44000, // 44kHz sampling freq
                           16,// 16 bit samples
                           1, // mono
                           2, // 2 bytes per "frame"
                           44000, // 1 frame/sample, so frame rate = sample rate
                           true)));// big endian (works on Macs and Windows)
         L.open();
         L.start();
         // audio line is ready

         byte[] b = new byte[88000];
         while (true) {
            // here you would read the notes to play from a string or array
            int n = (int) (f.length * Math.random());
            // 22000 = 11000 16-bit samples.
            // Note duration is in 1/4 second units.
            int d = (int) (1 + 2 * Math.random());
            for (int t = 0; t < d * 22000;) {
               final short s = (short) (a * Math.sin(t * f[n] * z));
               // could add something to ensure that phase
               // ends cleanly to avoid pops between notes.
               b[t++] = (byte) (s >>> 8);
               b[t++] = (byte) (s & 0xFF);
            }
            L.write(b, 0, d * 22000);
         }
      } catch (Exception e) {
         //e.printStackTrace();
      }
   }
}

Offline g666

Junior Devvie





« Reply #5 - Posted 2006-01-08 20:07:20 »

I just used a tiny midi file for sound and a single line of code to set it looping, but it still causes juddering. everything with java sound causes juddering!

desperately seeking sanity
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (73 views)
2014-11-26 15:22:04

toopeicgaming1999 (63 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (57 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!