{"id":804,"date":"2021-10-03T05:43:18","date_gmt":"2021-10-03T05:43:18","guid":{"rendered":"https:\/\/es-andreabianchini.it\/andrewsblog\/?p=804"},"modified":"2021-10-24T01:34:33","modified_gmt":"2021-10-24T01:34:33","slug":"catch-the-note","status":"publish","type":"post","link":"https:\/\/es-andreabianchini.it\/andrewsblog\/?p=804","title":{"rendered":"Catch the note"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In questo articolo rendo pubblico, a titolo di esempio, il codice java principale della mia app CatchTheNote, disponibile in versione completa alla voce di men\u00f9 Android.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.andreabianchini.catchthenote;\n\nimport androidx.appcompat.app.AppCompatActivity;\n\nimport android.content.Intent;\nimport android.media.AudioManager;\nimport android.media.SoundPool;\nimport android.os.Bundle;\nimport android.os.Handler;\nimport android.text.InputFilter;\nimport android.text.Spanned;\nimport android.view.View;\nimport android.widget.Button;\nimport android.widget.EditText;\nimport android.widget.TextView;\nimport android.widget.Toast;\n\nimport com.google.android.gms.ads.AdRequest;\nimport com.google.android.gms.ads.AdView;\nimport com.google.android.gms.ads.MobileAds;\n\nimport java.util.Random;\n\npublic class MainActivity extends AppCompatActivity {\n    private SoundPool soundPool;\n    private int soundID,tone;\n    private int a,as,b,c,cs,d,ds,e,f,fs,g,gs;\n    boolean plays = false, loaded = false;\n    float actVolume, maxVolume, volume;\n    AudioManager audioManager;\n    int counter;\n    int &#91;] notes=new int&#91;8];\n    int &#91;] notesId=new int&#91;8];\n    int nnotes=1;\n\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        \/\/ AudioManager audio settings for adjusting the volume\n        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);\n        actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);\n        maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);\n        volume = actVolume \/ maxVolume;\n\n        \/\/Hardware buttons setting to adjust the media sound\n        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);\n\n        \/\/ the counter will help us recognize the stream id of the sound played  now\n        counter = 0;\n\n        \/\/ Load the sounds\n        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);\n        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {\n            @Override\n            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {\n                loaded = true;\n                plays=false;\n            }\n        });\n\n        a = soundPool.load(this, R.raw.a, 1);\n        as = soundPool.load(this, R.raw.as, 1);\n        b = soundPool.load(this, R.raw.b, 1);\n        c = soundPool.load(this, R.raw.c, 1);\n        cs = soundPool.load(this, R.raw.cs, 1);\n        d = soundPool.load(this, R.raw.d, 1);\n        ds = soundPool.load(this, R.raw.ds, 1);\n        e = soundPool.load(this, R.raw.e, 1);\n        f = soundPool.load(this, R.raw.f, 1);\n        fs = soundPool.load(this, R.raw.fs, 1);\n        g = soundPool.load(this, R.raw.g, 1);\n        gs = soundPool.load(this, R.raw.gs, 1);\n\n        soundID = a;\n        nnotes=1;\n        notes&#91;0]=0;\n        notesId&#91;0]=a;\n\n        Button button =  findViewById(R.id.btPlay);\n        Button button1 =  findViewById(R.id.btA);\n        Button btChange =  findViewById(R.id.btChange);\n        Button btShow =  findViewById(R.id.btShow);\n        Button btInfo =  findViewById(R.id.btInfo);\n        EditText etNNotes = findViewById(R.id.etNNotes);\n\n        button.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                playSound(v);\n           }\n        });\n\n        button1.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                playA(v);\n            }\n        });\n\n        btChange.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                changeNote(v);\n            }\n        });\n\n        btShow.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                showNote(v);\n            }\n        });\n\n        btInfo.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                showInfo(v);\n            }\n        });\n\n        etNNotes.setFilters(new InputFilter&#91;]{ new InputFilterMinMax(\"1\", \"8\")});\n\n        \/\/ Initialize the Mobile Ads SDK\n        MobileAds.initialize(this, getString(R.string.admob_app_id));\n\n        AdView mBannerAd;\n\n        mBannerAd = findViewById(R.id.adView);\n\n        AdRequest adRequest = new AdRequest.Builder()\n                .build();\n        mBannerAd.loadAd(adRequest);\n\n    }\n\n    public void showInfo(View v) {\n        Intent intent = new Intent(MainActivity.this, Info.class);\n        startActivity(intent);\n\n    }\n\n    public void showNote(View v) {\n        TextView tv  =  findViewById(R.id.tvNote);\n        EditText etNNotes = findViewById(R.id.etNNotes);\n        nnotes = Integer.parseInt(etNNotes.getText().toString());\n        String s;\n        s=\"\";\n        for(int i=0;i&lt;nnotes;i++) {\n            switch (notes&#91;i]) {\n                case 0:\n                    s+=\"A \";\n                    break;\n                case 1:\n                    s+=\"A# \";\n                    break;\n                case 2:\n                    s+=\"B \";\n                    break;\n                case 3:\n                    s+=\"C \";\n                    break;\n                case 4:\n                    s+=\"C# \";\n                    break;\n                case 5:\n                    s+=\"D \";\n                    break;\n                case 6:\n                    s+=\"D# \";\n                    break;\n                case 7:\n                    s+=\"E \";\n                    break;\n                case 8:\n                    s+=\"F \";\n                    break;\n                case 9:\n                    s+=\"F# \";\n                    break;\n                case 10:\n                    s+=\"G \";\n                    break;\n                case 11:\n                    s+=\"G# \";\n                    break;\n\n            }\n        }\n        tv.setText(s);\n    }\n\n    public void changeNote(View v) {\n        EditText etNNotes = findViewById(R.id.etNNotes);\n        nnotes = Integer.parseInt(etNNotes.getText().toString());\n        int last,last1;\n        last1=2;\n        last=1;\n        for(int i=0;i&lt;nnotes;i++) {\n            tone=0;\n            if (i&gt;1)\n            while(tone==last || (Math.abs(tone-last)==1 &amp;&amp; Math.abs(last1-last)==1) || (Math.abs(tone-last)==3 &amp;&amp; Math.abs(last1-last)==3) || (Math.abs(tone-last)==2 &amp;&amp; Math.abs(last1-last)==3) || (Math.abs(tone-last)==3 &amp;&amp; Math.abs(last1-last)==2)) {\n                tone = new Random().nextInt(12);\n            }\n            else\n                tone = new Random().nextInt(12);\n            last1 = last;\n            last = tone;\n            notes&#91;i]=tone;\n            switch (tone) {\n                case 0:\n                    soundID = a;\n                    break;\n                case 1:\n                    soundID = as;\n                    break;\n                case 2:\n                    soundID = b;\n                    break;\n                case 3:\n                    soundID = c;\n                    break;\n                case 4:\n                    soundID = cs;\n                    break;\n                case 5:\n                    soundID = d;\n                    break;\n                case 6:\n                    soundID = ds;\n                    break;\n                case 7:\n                    soundID = e;\n                    break;\n                case 8:\n                    soundID = f;\n                    break;\n                case 9:\n                    soundID = fs;\n                    break;\n                case 10:\n                    soundID = g;\n                    break;\n                case 11:\n                    soundID = gs;\n                    break;\n\n            }\n            notesId&#91;i]=soundID;\n        }\n    }\n\n    public void playSound(View v) {\n        \/\/ Is the sound loaded does it already play?\n        TextView tv  =  findViewById(R.id.tvNote);\n        tv.setText(getString(R.string.note));\n\n        for (int i=0;i&lt;nnotes;i++){\n        if (loaded &amp;&amp; !plays) {\n            soundPool.play(notesId&#91;i], 1, 1, 0, 0, 1);\n            counter = counter++;\n            \/\/Toast.makeText(this, \"Played sound\", Toast.LENGTH_SHORT).show();\n            \/\/plays = true;\n        }\n            try { Thread.sleep(1600); }\n            catch (InterruptedException ex) { android.util.Log.d(\"Catch the Note\", ex.toString()); }        }\n    }\n\n    public void playA(View v) {\n        \/\/ Is the sound loaded does it already play?\n        TextView tv  =  findViewById(R.id.tvNote);\n        tv.setText(getString(R.string.note));\n\n            if (loaded &amp;&amp; !plays) {\n                soundPool.play(a, 1, 1, 0, 0, 1);\n                counter = counter++;\n                \/\/Toast.makeText(this, \"Played sound\", Toast.LENGTH_SHORT).show();\n                \/\/plays = true;\n            }\n            try { Thread.sleep(1600); }\n            catch (InterruptedException ex) { android.util.Log.d(\"Catch the Note\", ex.toString()); }\n    }\n\n    public void playLoop(View v) {\n        \/\/ Is the sound loaded does it already play?\n        if (loaded &amp;&amp; !plays) {\n\n            \/\/ the sound will play for ever if we put the loop parameter -1\n            soundPool.play(soundID, volume, volume, 1, -1, 1f);\n            counter = counter++;\n            Toast.makeText(this, \"Plays loop\", Toast.LENGTH_SHORT).show();\n            plays = true;\n        }\n    }\n\n    public void pauseSound(View v) {\n        if (plays) {\n            soundPool.pause(soundID);\n\/\/            soundID = soundPool.load(this, R.raw.beep, counter);\n            Toast.makeText(this, \"Pause sound\", Toast.LENGTH_SHORT).show();\n            plays = false;\n        }\n    }\n\n    public void stopSound(View v) {\n        if (plays) {\n            soundPool.stop(soundID);\n\/\/            soundID = soundPool.load(this, R.raw.beep, counter);\n            Toast.makeText(this, \"Stop sound\", Toast.LENGTH_SHORT).show();\n            plays = false;\n        }\n    }\n\n    public class InputFilterMinMax implements InputFilter {\n\n        private int min, max;\n\n        public InputFilterMinMax(int min, int max) {\n            this.min = min;\n            this.max = max;\n        }\n\n        public InputFilterMinMax(String min, String max) {\n            this.min = Integer.parseInt(min);\n            this.max = Integer.parseInt(max);\n        }\n\n        @Override\n        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {\n            try {\n                int input = Integer.parseInt(dest.toString() + source.toString());\n                if (isInRange(min, max, input))\n                    return null;\n            } catch (NumberFormatException nfe) { }\n            return \"\";\n        }\n\n        private boolean isInRange(int a, int b, int c) {\n            return b &gt; a ? c &gt;= a &amp;&amp; c &lt;= b : c &gt;= b &amp;&amp; c &lt;= a;\n        }\n    }\n    }\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In questo articolo rendo pubblico, a titolo di esempio, il codice java principale della mia app CatchTheNote, disponibile in versione completa alla voce di men\u00f9 Android.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,7],"tags":[],"class_list":["post-804","post","type-post","status-publish","format-standard","hentry","category-android","category-stem"],"_links":{"self":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=804"}],"version-history":[{"count":2,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":857,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions\/857"}],"wp:attachment":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}