[Csnd] Soundfile in android package
Date | 2014-02-16 19:17 |
From | Tarmo Johannes |
Subject | [Csnd] Soundfile in android package |
Hi, |
Date | 2014-02-17 19:13 |
From | Jacques Leplat |
Subject | Re: [Csnd] Soundfile in android package |
Hello Tarmo, Files in the raw directory have to be copied before you can use them in a csd. Not very elegant. Have a look at the cad player sample, it has files in an assets folder. It looks very easy: Using the res/raw takes more effort: getResources().openRawResource(R.raw.filename) to get the input stream, then the code below to write it to a file of your choice. Note that the code doesn’t check correctly for errors: the file can remain open. Needs fixing. /** * Extract a file resource into a file * * @param in typically given as getResources().openRawResource(R.raw.something) * @param name of the resulting file * @param directory target directory * @return the resulting file * @throws IOException */ public static File extractFileResource(InputStream in, String filename, File directory) throws IOException { File file = new File(directory, filename); FileOutputStream out = new FileOutputStream(file); BufferedInputStream bin = new BufferedInputStream(in); OutputStream bout = new BufferedOutputStream(new FileOutputStream(file)); byte[] buffer = new byte[1024 * 16]; int bytesRead = 0; while ((bytesRead = bin.read(buffer)) > 0) { bout.write(buffer, 0, bytesRead); } bin.close(); bout.flush(); bout.close(); return file; } All the best, Jacques On 16 Feb 2014, at 19:17, Tarmo Johannes
|
Date | 2014-02-17 21:28 |
From | Tarmo Johannes |
Subject | Re: [Csnd] Soundfile in android package |
Dear Jacques,
thanks a million! Java is still hard for me and finding the solution without your help and the function would have wasted me probably a lot of time reading forums, making trials and failures.
What I did in my code was to use your function :
extractFileResource(getResources().openRawResource(R.raw.test), "test.ogg", this.getCacheDir()); (+ the try /catch lines)
- the sound file got copied to the same directory where BaseCsoundActivity.createTempFile writes the csd and
asig soundin "test.ogg", iskiptime
in csound code worked flawlessly!
Something new from this day, thanks!
tarmo
On Monday 17 February 2014 19:13:19 Jacques Leplat wrote: Hello Tarmo, Files in the raw directory have to be copied before you can use them in a csd. Not very elegant. Have a look at the cad player sample, it has files in an assets folder. It looks very easy: webview.loadUrl("file:///android_asset/Csound6_User_Guide.html”); Using the res/raw takes more effort: getResources().openRawResource(R.raw.filename) to get the input stream, then the code below to write it to a file of your choice. Note that the code doesn’t check correctly for errors: the file can remain open. Needs fixing. /** * Extract a file resource into a file * * @param in typically given as getResources().openRawResource(R.raw.something) * @param name of the resulting file * @param directory target directory * @return the resulting file * @throws IOException */ public static File extractFileResource(InputStream in, String filename, File directory) throws IOException { File file = new File(directory, filename); FileOutputStream out = new FileOutputStream(file); BufferedInputStream bin = new BufferedInputStream(in); OutputStream bout = new BufferedOutputStream(new FileOutputStream(file)); byte[] buffer = new byte[1024 * 16]; int bytesRead = 0; while ((bytesRead = bin.read(buffer)) > 0) { bout.write(buffer, 0, bytesRead); } bin.close(); bout.flush(); bout.close(); return file; } All the best, Jacques On 16 Feb 2014, at 19:17, Tarmo Johannes Hi, |
Date | 2014-02-17 21:31 |
From | Victor Lazzarini |
Subject | Re: [Csnd] Soundfile in android package |
That's very good. We should add a new example using this. Victor On 17 Feb 2014, at 21:28, Tarmo Johannes wrote: > Dear Jacques, > > thanks a million! Java is still hard for me and finding the solution without your help and the function would have wasted me probably a lot of time reading forums, making trials and failures. > > What I did in my code was to use your function : > > extractFileResource(getResources().openRawResource(R.raw.test), "test.ogg", this.getCacheDir()); > (+ the try /catch lines) > > - the sound file got copied to the same directory where BaseCsoundActivity.createTempFile writes the csd and > > asig soundin "test.ogg", iskiptime > > in csound code worked flawlessly! > > Something new from this day, > thanks! > > tarmo > > > > On Monday 17 February 2014 19:13:19 Jacques Leplat wrote: > Hello Tarmo, > > > Files in the raw directory have to be copied before you can use them in a csd. Not very elegant. Have a look at the cad player sample, it has files in an assets folder. It looks very easy: > > webview.loadUrl("file:///android_asset/Csound6_User_Guide.html”); > > Using the res/raw takes more effort: > > > getResources().openRawResource(R.raw.filename) to get the input stream, then the code below to write it to a file of your choice. Note that the code doesn’t check correctly for errors: the file can remain open. Needs fixing. > > /** > * Extract a file resource into a file > * > * @param in typically given as getResources().openRawResource(R.raw.something) > * @param name of the resulting file > * @param directory target directory > * @return the resulting file > * @throws IOException > */ > public static File extractFileResource(InputStream in, String filename, File directory) throws IOException { > File file = new File(directory, filename); > FileOutputStream out = new FileOutputStream(file); > > BufferedInputStream bin = new BufferedInputStream(in); > OutputStream bout = new BufferedOutputStream(new FileOutputStream(file)); > > byte[] buffer = new byte[1024 * 16]; > int bytesRead = 0; > while ((bytesRead = bin.read(buffer)) > 0) { > bout.write(buffer, 0, bytesRead); > } > bin.close(); > bout.flush(); > bout.close(); > return file; > } > > > All the best, > > Jacques > > On 16 Feb 2014, at 19:17, Tarmo Johannes > Hi, > If i want to put a soundfile to res/raw in an android csound project and use it in csd, how should I do it? > Simply diskin2 "test.ogg" does not find it. > Thanks! > Tarmo > > > > Dr Victor Lazzarini Senior Lecturer Dept. of Music NUI Maynooth Ireland tel.: +353 1 708 3545 Victor dot Lazzarini AT nuim dot ie |
Date | 2014-02-18 23:21 |
From | Andres Cabrera |
Subject | Re: [Csnd] Soundfile in android package |
Would using the <CsFileB> tag work in Android? Cheers,http://www.csounds.com/manual/html/CommandUnifile.html Andrés On Mon, Feb 17, 2014 at 1:31 PM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote: That's very good. We should add a new example using this. |