Jython Sound and Image Processing

Moderator: Moderators

Jython Sound and Image Processing

Postby alvin.murphy@navy.mil » Wed Apr 05, 2017 9:29 am

Does the Cameo Jython implementation support the use of sound and video methods? Do I need to import a module or add something to my MagicDraw directory?
alvin.murphy@navy.mil
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:0
Joined: Thu Mar 23, 2017 7:16 am

Re: Jython Sound and Image Processing

Postby tomas.lukosius@nomagic.com » Thu Apr 06, 2017 2:28 am

Hello Alvin Murphy,

It is possible to play basic sound using java api. Following example code shows how to play wave sound:

Code: Select all
from javax.sound.sampled import AudioInputStream, AudioFormat, DataLine, AudioSystem, Clip
from java.io import InputStream, FileInputStream, File

yourFile = File("path to file *.wav")
stream = AudioSystem.getAudioInputStream(yourFile)
format = stream.getFormat()
info = DataLine.Info(Clip, format)
clip = AudioSystem.getLine(info)
clip.open(stream)
clip.start()


If you need to play compressed audio formats, like mp3, or video formats, then you will need to rely on some 3rd party library. We did not test any of them, so we cannot provide any recomendations, but we think, that technically it is possible.

Kind regards,
No Magic, Inc. Customer Support
tomas.lukosius@nomagic.com
Forum Newbie
Forum Newbie
 
Posts: 22
Posts Rating:3
Joined: Tue Aug 30, 2016 12:22 am

Re: Jython Sound and Image Processing

Postby alvin.murphy@navy.mil » Thu Apr 06, 2017 3:15 pm

Thank you! Thank worked perfectly.

Is there a way to access the file from the current Project Directory without specifying the full path name?

I used the following:

Code: Select all
yourFile = File("C:\\Users\\alvin.murphy\\Music\MySound.wav")


I need an easy way to share the model without having the user update the path within the script.

Thanks.
alvin.murphy@navy.mil
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:0
Joined: Thu Mar 23, 2017 7:16 am

Re: Jython Sound and Image Processing

Postby tomas.lukosius@nomagic.com » Fri Apr 07, 2017 2:03 am

You can get project directory with following code:

Code: Select all
from com.nomagic.magicdraw.core import Application
from java.io import File

projectFilePath = Application.getInstance().getProject().getFileName()
projectFile = File(projectFilePath)
projectDir = projectFile.getParent()
# append your media file name


Kind regards,
No Magic, Inc. Customer Support
tomas.lukosius@nomagic.com
Forum Newbie
Forum Newbie
 
Posts: 22
Posts Rating:3
Joined: Tue Aug 30, 2016 12:22 am


Return to Cameo Simulation Toolkits

Who is online

Users browsing this forum: No registered users and 0 guests