I would like to know an easy way to implement a function playWav[filename_String] that plays a .wav file (as a side effect).
playSound[filename_String] := EmitSound # Import[filename, "WAV"]
Changing the "wav" to "Sound" allows it to play without GUI
playSound[filename_String] := EmitSound # Import[filename, "Sound"]
Related
I need to get waveform data from the wav file,but my code returns not right waveform (i compare my results with waveform from fl studio)
This is my code:
path = "/storage/emulated/0/FLM User
Files/My Samples/808 (16).wav";
waveb = FileUtil.readFile(path);
waveb = waveb.substring((int) (waveb.indexOf("data") + 4), (int)(waveb.length()));
byte[] b = waveb.getBytes();
for(int i= 0; i < (int)(b.length/4); i++) {
map = new HashMap<>();
map.put("value", String.valueOf((long)((b[i*4] & 0xFF) +
((b[i*4+1] & 0xFF) << 8))));
map.put("byte", String.valueOf((long)(b[i*4])));
l.add(map);
}
listview1.setAdapter(new
Listview1Adapter(l));
( (BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();
My results:
Fl studio mobile results:
I'm not sure I can help, given what I know off of the top of my head, but perhaps this will trigger some ideas in your search for a solution.
It looks to me like you are assuming the sound file is 16-bit stereo, little-endian, and that you are only attempting to inspect one track of the stereo frame. Can you confirm this?
There's at least one way this plan could go awry: the .wav header may be an odd number of bytes in length, and you might not be properly parsing frame boundaries as a result. As an experiment, maybe try adding a different increment when you reference the b[] array? For example b[i4 + 1] and b[i4 + 2] instead of b[i4] and b[i4 + 1]. This won't solve the general problem of parsing .wav headers, but it could at least get you closer to understanding the situation.
It sure looks like Java's AudioInputStream is not accessible in Android, and all searches that I have that ask if there is an Android equivalent are turning up unanswered.
I've used AudioTrack for the playback of raw PCM, but I don't know an Android equivalent for reading wav files. The AudioRecord class and read() methods look interesting as the read methods store PCM data in a short array, but I've never used them, and they seem to be hard-coded to the microphone for input.
There used to be a Google Group: andraudio#googlegroups.com. IDK if it is still around. I used to go there and occasionally ask about things.
Maybe there is code you can use from Oboe or libGDX? The latter makes use of OpenAL and is for cross-platform development, with Android as one of the target platforms. I have not looked into either for this question.
If you do find the answer, it would be great to post it as a solution. This seems to be a matter that many have tried to solve and given up on.
I intend on making a cli audio player for racket, as an exercise to learn Racket, and everything else that would entail this project. I am stuck though how to begin. I can't find any package to play sound files, so I am guessing I may have to make one. How would I go about it?
What you probably want is #lang video (website). It provides a high level interface into audio playback. Allowing you to do something like:
#lang video
(clip "file.mp3")
Since you want to make a little command line player you might also want to take a look at its small preview tool.
I ended doing this the hackish way by calling a shell script via racket, not ideal at all. For reference, putting the code here.
; This creates the initail rsound
; for a song, this rsound is passed around
; so the whole song doesn't have to be
; decoded from the file everytime.
(define (play filepath)
(cond [(string=? "mp3" (last (regexp-split #rx"\\." filepath)))
(system* "./mp3-hack" filepath)
(set! filepath "curr.wav")])
(define input-pstream (make-pstream))
(define input-rsound (rs-read filepath))
(pstream-play input-pstream input-rsound)
(values input-pstream input-rsound filepath))
And the mp3-hack file just uses ffmpeg
#!/bin/sh
ffmpeg -i $1 -acodec pcm_s16le -ac 1 -ar 44100 curr.wav
Yeah, I know. Inelegant, but at least I got it working. I needed it for my hackathon project MPEGMafia
When the user trigger a condition,I need to play a sound once.It is in major cycle of while,so it always play many times.Please solve the little problem.Thanks.
Include at the start of your loop the code playsound = True. Then in your loop put the following:
if playsound:
# Play the sound
playsound = False
Put your current command for playing in place of # Play the sound, it probably looks something like sound.play().
I've recently started Coding a program that will replace sound effects from a default directory, in the Source-Engine Game, Garry's Mod.
This is the current code:
function GM:PlayerFootstep( ply, pos, foot, sound, volume, rf )
ply:EmitSound("gear1")
return true
end
I want to emit multiple .wav Sound effects, without them overlapping, and being selected at random.
I have not found any Source helpful enough on the Internet to assist, so i resorted to Stack Overflow.
I would appreciate assistance with the topic.
You'll want to look at the file.Find function for this.
I'd recommend having a custom folder such as sound/customsteps/ where you can put all your custom sounds. I would also recommend using the .wav format for the sound files, but some others do work (.mp3 and .ogg if I recall correctly).
In your code, simply call local snds=file.Find( "sound/customsteps/*", "GAME" ) which gives you a table, then you can simply choose a random one from the list using local snd=snds[math.random(1,#snds)] and play it as you do in your above code - ply:EmitSound(snd).
Make sure you create the table of sounds outside of the GM:PlayerFootstep function, so that it only runs once. I would also recommend precaching all the sounds. You can do this by looping through the table and calling util.PrecacheSound(path) on them, like so:
for k,v in pairs(snds) do
util.PrecacheSound(v)
end
So, with all that in mind - your final code should look something like this:
local snds=file.Find( "sound/customsteps/*", "GAME" )
for k,v in pairs(snds) do
util.PrecacheSound(v)
end
function GM:PlayerFootstep( ply, pos, foot, sound, volume, rf )
ply:EmitSound(snds[math.random(1,#snds)])
return true
end
Source: personal experience
I draw a screen with OpenGL commands. And I must save this screen to .bmp or .png format. But I can't do it. I am using glReadpixels but I can't do continue. How can I save this drawing in c++ with OpenGL?
Here it comes! you must include WinGDI.h (which i think the GL will do it!)
void SaveAsBMP(const char *fileName)
{
FILE *file;
unsigned long imageSize;
GLbyte *data=NULL;
GLint viewPort[4];
GLenum lastBuffer;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
bmfh.bfType='MB';
bmfh.bfReserved1=0;
bmfh.bfReserved2=0;
bmfh.bfOffBits=54;
glGetIntegerv(GL_VIEWPORT,viewPort);
imageSize=((viewPort[2]+((4-(viewPort[2]%4))%4))*viewPort[3]*3)+2;
bmfh.bfSize=imageSize+sizeof(bmfh)+sizeof(bmih);
data=(GLbyte*)malloc(imageSize);
glPixelStorei(GL_PACK_ALIGNMENT,4);
glPixelStorei(GL_PACK_ROW_LENGTH,0);
glPixelStorei(GL_PACK_SKIP_ROWS,0);
glPixelStorei(GL_PACK_SKIP_PIXELS,0);
glPixelStorei(GL_PACK_SWAP_BYTES,1);
glGetIntegerv(GL_READ_BUFFER,(GLint*)&lastBuffer);
glReadBuffer(GL_FRONT);
glReadPixels(0,0,viewPort[2],viewPort[3],GL_BGR,GL_UNSIGNED_BYTE,data);
data[imageSize-1]=0;
data[imageSize-2]=0;
glReadBuffer(lastBuffer);
file=fopen(fileName,"wb");
bmih.biSize=40;
bmih.biWidth=viewPort[2];
bmih.biHeight=viewPort[3];
bmih.biPlanes=1;
bmih.biBitCount=24;
bmih.biCompression=0;
bmih.biSizeImage=imageSize;
bmih.biXPelsPerMeter=45089;
bmih.biYPelsPerMeter=45089;
bmih.biClrUsed=0;
bmih.biClrImportant=0;
fwrite(&bmfh,sizeof(bmfh),1,file);
fwrite(&bmih,sizeof(bmih),1,file);
fwrite(data,imageSize,1,file);
free(data);
fclose(file);
}
Unless you're feeling particularly ambitious (or perhaps masochistic) you probably want to use a library like DevIL that already supports this. The current version can load and/or save in both PNG and BMP formats, along with a few dozen others.
Compared to something like IJG, this is oriented much more heavily toward working with OpenGL or DirectX (e.g., it can load a file fairly directly into an texture or vice versa).
I know you're asking for raster formats, but an indirect way would be to first output vector graphics through gl2ps (http://www.geuz.org/gl2ps/). Examples of usage are provided with the package and on the site (http://www.geuz.org/gl2ps/#tth_sEc3).
Then, the vector output can be converted to the format of your choice using another tool (Inkscape, Image/GraphicsMagick, etc.) or library. An added benefit is you can convert to bitmaps of any resolution in the future.
One thing need to be fixed at:
bmih.biXPelsPerMeter = bmih.biYPelsPerMeter = 0;
Otherwise, some picture edit can not open correctly.