Categories
discuss

Sine Wave Sound Generator in Java

What’s the simplest way to generate a sine wave sound at any frequency in Java? A sample size more than 2 bytes would help, but it doesn’t really matter.

Answer

See Beeper for a self-contained example.


Perhaps something simpler?

That 51 lines of snippet (repeated below – spaced out for single line & in-line comments) as shown at the top of the linked answer, is about as simple as generating a tone gets (OK, you can take out 5+ lines for the harmonic).

People seem to assume it should be a method built into the toolkit to produce a pure tone. It is not, and takes a little calculating to make one.

/** Generates a tone, and assigns it to the Clip. */
public void generateTone()
    throws LineUnavailableException {
    if ( clip!=null ) {
        clip.stop();
        clip.close();
    } else {
        clip = AudioSystem.getClip();
    }
    boolean addHarmonic = harmonic.isSelected();

    int intSR = ((Integer)sampleRate.getSelectedItem()).intValue();
    int intFPW = framesPerWavelength.getValue();

    float sampleRate = (float)intSR;

    // oddly, the sound does not loop well for less than
    // around 5 or so, wavelengths
    int wavelengths = 20;
    byte[] buf = new byte[2*intFPW*wavelengths];
    AudioFormat af = new AudioFormat(
        sampleRate,
        8,  // sample size in bits
        2,  // channels
        true,  // signed
        false  // bigendian
        );

    int maxVol = 127;
    for(int i=0; i<intFPW*wavelengths; i++){
        double angle = ((float)(i*2)/((float)intFPW))*(Math.PI);
        buf[i*2]=getByteValue(angle);
        if(addHarmonic) {
            buf[(i*2)+1]=getByteValue(2*angle);
        } else {
            buf[(i*2)+1] = buf[i*2];
        }
    }

    try {
        byte[] b = buf;
        AudioInputStream ais = new AudioInputStream(
            new ByteArrayInputStream(b),
            af,
            buf.length/2 );

        clip.open( ais );
    } catch(Exception e) {
        e.printStackTrace();
    }
}
Categories
discuss

How to create a new activity and then display text in it with android programming

I managed to have a button and then when I click on it, I go to a new activity that is called “TUTORIALONE”

and then I want to display some text in this new activity

so I have something like this

   Button b = (Button) findViewById(R.id.tutorial1);
        b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            startActivity(new Intent("my.android.TUTORIALONE"));
            TextView tv = (TextView)findViewById(R.id.tutorial1);
                    tv.setText("this is some text);

        }
    });

the problem is that it first displays the text on my button, and then it shows me the new activity, how would I achieve displaying the text on the new activity?

thanks in advance

Answer

In your TUTORIALONE activity you probably have an associated xml file for displaying content. Perhaps it iss set something like this

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.id.TUTORIALONE);
}

In the layout xml file for TUTORIALONE just add something like this

<TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />

EDIT

To change the text of this TextView, do something like this in your TUTORIALONE activity.

protected void onStart()
{
    super.onStart();
    TextView tv = (TextView)findViewById(R.id.text);
    tv.setText("this string is set dynamically from java code");
}

Note that the id here (R.id.text) is the same as in the xml file (“@+id/text”)

Categories
discuss

How to debug shaders on Android

Is there any way to debug shaders on Android (OpenGL-ES 2.0)?
I`m new to OpenGL and shaders at all, messed up something with coordinates – how do you usually solve such issues?

Thanks in advance.

Answer

Basicly Max is right – there are no good tools for debuggin shaders on mobile devices, that`s why hand-made flags (change color in case of bug, etc) currently is the best solution.

Categories
discuss

Convert a string of HTML into DOM objects with jQuery

I have HTML in a JavaScript string (containing usual, nested HTML). Using jQuery, can I convert that into a valid HTML element in a single stroke using any of the document.create* functions? My requirement is to use document.getElementById on the created DOM object.

Answer

Take simple nested example.

var dom_string = '<div>xxx<div>yyy</div></div>';

create HTML DOM elements using $() function of jquery and append wherever you want. i have taken ‘body’ but you can append anywhere.

$(dom_string).appendTo('body');

Alternatively you can implement this with pure javascript:

var dom_target = document.getElementById("target");
dom_target.innerHTML = dom_string;
Categories
discuss

Theme dependent colors of selected widgets

I’m pretty sure that this question already has been answered somewhere. It just seems too common. But I can’t find the answer. I can’t also figure out the solution.

Here’s the problem:

I want one of my TableRow’s to have different background color. It’s simple, I just need to add

android:background="#123456" 

In TableRow’s XML Declaration. But, I also want my application to have two themes. In the other theme, the TableRow should have different background color. I just can’t find a way to define a color value inside a theme and use it. I would like to type something like this:

<style name="Theme.MyApp" parent="@style/Theme.Light">
   <color "my_cool_color">#123456</color>
</style>

<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
   <color "my_cool_color">#654321</color>
</style>

And, in TableRow’s declaration:

android:background="@color/my_cool_color"

So, when I change the theme, the color of that one TableRow’s background also changes. I’ve tried in many ways for many hours and didn’t succeed… One thing I didn’t try, was creating my own widget basing on TableRow and declaring a separate style for it – I think this should work, but it’s just too heavy solution for so simple problem.

Answer

You can do this using attributes. First define your attribute in attrs.xml (this file goes under the ‘values’ folder):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myCoolColor" format="color" />
</resources>

Then in your styles.xml, define myCoolColor for each theme:

<style name="Theme.MyApp" parent="@style/Theme.Light">
   <item name="myCoolColor">#123456</item>
</style>

<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
   <item name="myCoolColor">#654321</item>
</style>

Now, specify myCoolColor as the background of your view:

android:background="?myCoolColor"

You can go further and use a reference to a color so you can keep your colors defined in one place. Change the attribute to include a reference (note that we can use a color OR a reference):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myCoolColor" format="color|reference" />
</resources>

Change your styles.xml to reference a color for each theme:

<style name="Theme.MyApp" parent="@style/Theme.Light">
   <item name="myCoolColor">@color/blue</item>
</style>

<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
   <item name="myCoolColor">@color/green</item>
</style>

Finally define the colors in your colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#0000FF</color>
    <color name="green">#00FF00</color>
</resources>

That’s it!

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..