Playing a music file in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Playing a music file in R

Vahid
Hello my R friends,

How can we call a music file from our laptop (for example from desktop) and
play it in R? Could you please give me an explicit example by writing
commands in R. Is it necessary for our music file to be in a specific
format? If yes, which format it has to be?

Thank you in advance

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

Yousri Fanous
Google search "playing music with r"
for example:
https://stackoverflow.com/questions/31782580/how-can-i-play-birthday-music-using-r

It can be a starting point

Yousri

On Tue, Jul 21, 2020 at 2:02 PM Vahid Borji <[hidden email]> wrote:

>
> Hello my R friends,
>
> How can we call a music file from our laptop (for example from desktop) and
> play it in R? Could you please give me an explicit example by writing
> commands in R. Is it necessary for our music file to be in a specific
> format? If yes, which format it has to be?
>
> Thank you in advance
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

Jim Lemon-4
In reply to this post by Vahid
Hi Vahid,
The following command:

system("mplayer /home/jim/songs/bimbo_soul.mp3",
 wait=FALSE,ignore.stdout=TRUE)

works fine for me. Of course you'll have to specify a music player and
music file that you have...

Jim

On Wed, Jul 22, 2020 at 4:02 AM Vahid Borji <[hidden email]> wrote:

>
> Hello my R friends,
>
> How can we call a music file from our laptop (for example from desktop) and
> play it in R? Could you please give me an explicit example by writing
> commands in R. Is it necessary for our music file to be in a specific
> format? If yes, which format it has to be?
>
> Thank you in advance
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

bretschr
In reply to this post by Vahid
Hello vahid.borji,


Re:

> How can we call a music file from our laptop (for example from desktop) and
> play it in R? Could you please give me an explicit example by writing
> commands in R. Is it necessary for our music file to be in a specific
> format? If yes, which format it has to be?

Wave files, and probably a few other formata, can be loaded, saved and manupulated with (a.o.)  these packages:

library(signal)  
library(seewave)
library(tuneR)


In addition, to play a wave file, a small Unix file is necessary, called "play".
It must be attached by the following line:

        setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")   # path depends on your R version etc.


Then, a simple script like the following will do:


        inname = file.choose()
        ywave=readWave(inname)
        ster=ywave@stereo
        srate=[hidden email]
        play(ywave)


Hoping this helps, I remain
With best regards,


Franklin Bretschneider
Dept of Biology
Utrecht University
[hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

David Winsemius
In reply to this post by Vahid
 From the package's DESCRIPTION file found as the first hit to a search
with rseek.org using strategy "play mp3 files":

Package:     ‘tuneR’ July 8, 2018

Version:     1.3.3 Date 2018-07-03

Title:     Analysis of Music and Speech

Author:     Uwe Ligges with contributions from Sebastian Krey, Olaf
Mersmann, Sarah Schnackenberg, Guillaume Guenard, Andrea Preusser, Anita
Thieler, Johanna Mielke and Claus Weihs, as well as code fragments and
ideas from the former package 'sound' by Matthias Heymann and functions
from 'rastamat' by Daniel P. W. Ellis. The included parts of the libmad
MPEG audio decoder library are authored by Underbit Technologies

Description:     Analyze music and speech, extract features like MFCCs,
handle wave files and their representation in various ways, read mp3,
read midi, perform steps of a transcription, ... Also contains functions
ported from the 'rastamat' 'Matlab' package.

--

David

On 7/21/20 11:00 AM, Vahid Borji wrote:

> Hello my R friends,
>
> How can we call a music file from our laptop (for example from desktop) and
> play it in R? Could you please give me an explicit example by writing
> commands in R. Is it necessary for our music file to be in a specific
> format? If yes, which format it has to be?
>
> Thank you in advance
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

Jim Lemon-4
In reply to this post by Jim Lemon-4
Thie "system" command in R takes the first argument and passes that to
the shell in which R is running.
1) I run R in the bash shell under Fedora Linux version 30
2) I have installed "mplayer" as an audio/visual player that can be
called from the bash shell
3) The command I pass says "Start the mplayer program using the first
argument as the file to process.

One method is to find the easiest way to play an audio/visual file on
your system and then call it from R using "system".
Other ways using R packages have been suggested by other respondents.

Jim

On Thu, Jul 23, 2020 at 5:26 PM Vahid Borji <[hidden email]> wrote:

>
> Dear Jim,
>
> Thank you for your answer.
>
> I have a question for you.
>
> This is your code: system("mplayer /home/jim/songs/bimbo_soul.mp3",
>  wait=FALSE,ignore.stdout=TRUE)
>
> 1. Which part of your code is related to call a music player?
>
> 2. Which part is related to call a music file?
>
> I want to do this on my laptop, so I need to understand your code (the answer to the above questions).
>
> Regards,
> Vahid
>
>
> On Wed, Jul 22, 2020 at 1:45 AM Jim Lemon <[hidden email]> wrote:
>>
>> Hi Vahid,
>> The following command:
>>
>> system("mplayer /home/jim/songs/bimbo_soul.mp3",
>>  wait=FALSE,ignore.stdout=TRUE)
>>
>> works fine for me. Of course you'll have to specify a music player and
>> music file that you have...
>>
>> Jim
>>
>> On Wed, Jul 22, 2020 at 4:02 AM Vahid Borji <[hidden email]> wrote:
>> >
>> > Hello my R friends,
>> >
>> > How can we call a music file from our laptop (for example from desktop) and
>> > play it in R? Could you please give me an explicit example by writing
>> > commands in R. Is it necessary for our music file to be in a specific
>> > format? If yes, which format it has to be?
>> >
>> > Thank you in advance
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

bretschr
In reply to this post by bretschr
Dear Vahid,


Re:

> I have a question regarding the following code:
> setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")   # path depends on your R version etc.
>
> How can I find the corresponding path on my laptop?


This line ...

setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")  

... is the path on my MacBook Air running Mac OS Mojave.
The structure of R on similar computers and OS versions will be the same, but I don't know if you use Windows or Linux.
Maybe an R-user working with Windows or Linux can help.
Success,

Franklin
----



Franklin Bretschneider
Dept of Biology
Utrecht University
[hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Playing a music file in R

bretschr
In reply to this post by bretschr
Dear Vahid,


Re:

> I have a question regarding the following code:
> setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")   # path depends on your R version etc.
>
> How can I find the corresponding path on my laptop?


This line ...

setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")  

... is the path on my MacBook Air running Mac OS Mojave.
The structure of R on similar computers and OS versions will be the same, but I don't know if you use Windows or Linux.
Maybe an R-user working with Windows or Linux can help.
Success,

Franklin
----



Franklin Bretschneider
Dept of Biology
Utrecht University
[hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Re: Playing a music file in R

Richard M. Heiberger
The R command
system.file()

tells you where the currently running version of R is located on your
machine and your operating system.

On the Macintosh it shows
> system.file()
[1] "/Library/Frameworks/R.framework/Resources/library/base"

You don't need to worry about the R version number, as R knows where
it is and gives you the location
of the currently running version.  You can now open a system directory
file (Finder on Mac, WindowsExplorer on Windows, etc)
and navigate up and down to what you are looking for.

Or, within R, you can navigate with .. and more directory statements,
thus the example from the original email

setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")
  # path depends on your R version etc.

would be written
tmp <- system.file("../../bin/play")
and the result is the correct location (if it is there).
You will get an empty string if it is not ther, but this will get you
started on where to look.
then you write

setWavPlayer(tmp)   # path depends on your R version etc.

On Thu, Jul 23, 2020 at 7:21 AM bretschr <[hidden email]> wrote:

>
> Dear Vahid,
>
>
> Re:
>
> > I have a question regarding the following code:
> > setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")   # path depends on your R version etc.
> >
> > How can I find the corresponding path on my laptop?
>
>
> This line ...
>
> setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")
>
> ... is the path on my MacBook Air running Mac OS Mojave.
> The structure of R on similar computers and OS versions will be the same, but I don't know if you use Windows or Linux.
> Maybe an R-user working with Windows or Linux can help.
> Success,
>
> Franklin
> ----
>
>
>
> Franklin Bretschneider
> Dept of Biology
> Utrecht University
> [hidden email]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Re: Playing a music file in R

Rasmus Liland-3
On 2020-07-23 14:19 -0400, Richard M. Heiberger wrote:

> On Thu, Jul 23, 2020 at 7:21 AM bretschr <[hidden email]> wrote:
> >
> > Dear Vahid,
> >
> >
> > Re:
> >
> > > I have a question regarding the
> > > following code:
> > > setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")   # path depends on your R version etc.
> > >
> > > How can I find the corresponding
> > > path on my laptop?
> >
> >
> > This line ...
> >
> > setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")
> >
> > ... is the path on my MacBook Air
> > running Mac OS Mojave.  The
> > structure of R on similar computers
> > and OS versions will be the same,
> > but I don't know if you use Windows
> > or Linux.  Maybe an R-user working
> > with Windows or Linux can help.
>
> The R command
> system.file()
>
> tells you where the currently running
> version of R is located on your
> machine and your operating system.
>
> On the Macintosh it shows
> > system.file()
> [1] "/Library/Frameworks/R.framework/Resources/library/base"
>
> You don't need to worry about the R
> version number, as R knows where it is
> and gives you the location of the
> currently running version.  You can
> now open a system directory file
> (Finder on Mac, WindowsExplorer on
> Windows, etc) and navigate up and down
> to what you are looking for.
>
> Or, within R, you can navigate with ..
> and more directory statements, thus
> the example from the original email
>
> setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")
>   # path depends on your R version etc.
>
> would be written
> tmp <- system.file("../../bin/play")
> and the result is the correct location
> (if it is there).
> You will get an empty string if it is
> not ther, but this will get you
> started on where to look.  then you
> write
>
> setWavPlayer(tmp)   # path depends on your R version etc.

Hello, I found you can use mpv (and
mplayer) as well, instead of sox[1].  
There are many ways to install it[1]
including various ways on mac.

        > system("whereis mpv")
        mpv: /usr/bin/mpv /etc/mpv /usr/include/mpv /usr/share/mpv /usr/share/man/man1/mpv.1.gz
        > tuneR::setWavPlayer("/usr/bin/mpv")
        > w <- tuneR::readWave(filename="rf_07.wav", from=0, to=1e6)
        > w
       
        Wave Object
                Number of Samples:      1000001
                Duration (seconds):     22.68
                Samplingrate (Hertz):   44100
                Channels (Mono/Stereo): Stereo
                PCM (integer format):   TRUE
                Bit (8/16/24/32/64):    16
       
        > tuneR::play(w)
         (+) Audio --aid=1 (pcm_s16le 2ch 44100Hz)
        AO: [pulse] 44100Hz stereo 2ch s16
        A: 00:00:22 / 00:00:22 (98%)
       
       
        Exiting... (End of file)
        >
        > tuneR::setWavPlayer("/usr/bin/play")
        > tuneR::play(w)
       
        /tmp/RtmpadTOu6/tuneRtemp.wav:
       
         File Size: 4.00M     Bit Rate: 1.41M
          Encoding: Signed PCM
          Channels: 2 @ 16-bit
        Samplerate: 44100Hz
        Replaygain: off
          Duration: 00:00:22.68
       
        In:100%  00:00:22.68 [00:00:00.00] Out:1.00M [ -====|====- ] Hd:4.2 Clip:0
        Done.
        >


Best,
Rasmus

[1] http://sox.sourceforge.net/
[1] https://mpv.io/installation/

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [External] Re: Playing a music file in R

Henrik Bengtsson-5
FWIW, see also the 'midi' R package
(https://github.com/moodymudskipper/midi).  It's not on CRAN.  /Henrik

On Thu, Jul 23, 2020 at 12:19 PM Rasmus Liland <[hidden email]> wrote:

>
> On 2020-07-23 14:19 -0400, Richard M. Heiberger wrote:
> > On Thu, Jul 23, 2020 at 7:21 AM bretschr <[hidden email]> wrote:
> > >
> > > Dear Vahid,
> > >
> > >
> > > Re:
> > >
> > > > I have a question regarding the
> > > > following code:
> > > > setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")   # path depends on your R version etc.
> > > >
> > > > How can I find the corresponding
> > > > path on my laptop?
> > >
> > >
> > > This line ...
> > >
> > > setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")
> > >
> > > ... is the path on my MacBook Air
> > > running Mac OS Mojave.  The
> > > structure of R on similar computers
> > > and OS versions will be the same,
> > > but I don't know if you use Windows
> > > or Linux.  Maybe an R-user working
> > > with Windows or Linux can help.
> >
> > The R command
> > system.file()
> >
> > tells you where the currently running
> > version of R is located on your
> > machine and your operating system.
> >
> > On the Macintosh it shows
> > > system.file()
> > [1] "/Library/Frameworks/R.framework/Resources/library/base"
> >
> > You don't need to worry about the R
> > version number, as R knows where it is
> > and gives you the location of the
> > currently running version.  You can
> > now open a system directory file
> > (Finder on Mac, WindowsExplorer on
> > Windows, etc) and navigate up and down
> > to what you are looking for.
> >
> > Or, within R, you can navigate with ..
> > and more directory statements, thus
> > the example from the original email
> >
> > setWavPlayer("/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/play")
> >   # path depends on your R version etc.
> >
> > would be written
> > tmp <- system.file("../../bin/play")
> > and the result is the correct location
> > (if it is there).
> > You will get an empty string if it is
> > not ther, but this will get you
> > started on where to look.  then you
> > write
> >
> > setWavPlayer(tmp)   # path depends on your R version etc.
>
>
> Hello, I found you can use mpv (and
> mplayer) as well, instead of sox[1].
> There are many ways to install it[1]
> including various ways on mac.
>
>         > system("whereis mpv")
>         mpv: /usr/bin/mpv /etc/mpv /usr/include/mpv /usr/share/mpv /usr/share/man/man1/mpv.1.gz
>         > tuneR::setWavPlayer("/usr/bin/mpv")
>         > w <- tuneR::readWave(filename="rf_07.wav", from=0, to=1e6)
>         > w
>
>         Wave Object
>                 Number of Samples:      1000001
>                 Duration (seconds):     22.68
>                 Samplingrate (Hertz):   44100
>                 Channels (Mono/Stereo): Stereo
>                 PCM (integer format):   TRUE
>                 Bit (8/16/24/32/64):    16
>
>         > tuneR::play(w)
>          (+) Audio --aid=1 (pcm_s16le 2ch 44100Hz)
>         AO: [pulse] 44100Hz stereo 2ch s16
>         A: 00:00:22 / 00:00:22 (98%)
>
>
>         Exiting... (End of file)
>         >
>         > tuneR::setWavPlayer("/usr/bin/play")
>         > tuneR::play(w)
>
>         /tmp/RtmpadTOu6/tuneRtemp.wav:
>
>          File Size: 4.00M     Bit Rate: 1.41M
>           Encoding: Signed PCM
>           Channels: 2 @ 16-bit
>         Samplerate: 44100Hz
>         Replaygain: off
>           Duration: 00:00:22.68
>
>         In:100%  00:00:22.68 [00:00:00.00] Out:1.00M [ -====|====- ] Hd:4.2 Clip:0
>         Done.
>         >
>
>
> Best,
> Rasmus
>
> [1] http://sox.sourceforge.net/
> [1] https://mpv.io/installation/
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [External] Re: Playing a music file in R

Rasmus Liland-3
On 2020-07-23 16:25 -0700, Henrik Bengtsson wrote:
> FWIW, see also the 'midi' R package
> (https://github.com/moodymudskipper/midi).  
> It's not on CRAN.  /Henrik

Interesting!  I tried to play the midi
running these lines, but failed:

        > mid <- midi::midi$new(file.path(find.package("midi"), "extdata/vivaldi_spring.mid"))
        > mid$play("Violin")
        sh: /tmp/RtmpTGH8Qu/tuneRtemp.wav: Permission denied

This blogpost[1] is good for francophile
music theorists.

/Rasmus

[1] https://www.davidsolito.com/post/midiplayer-avec-r-part-2/

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (849 bytes) Download Attachment