NAudio 4000Hz WAV? - arrays

I am attempting to use the NAudio lib like the below. When I have a WAV file saved as Mono, 4KHz, the AudioBytesOriginal array has all zeroes. The file does play when double-clicked in Windows, so the data is there. It also plays in Audacity.
using ( var waveFileReader = new WaveFileReader( FileNameIn ) )
{
var thisIsWhat = waveFileReader.WaveFormat; // reports as 8KHz
AudioBytesOriginal = new byte[waveFileReader.Length];
int read = waveFileReader.Read( AudioBytesOriginal , 0 , AudioBytesOriginal.Length );
short[] sampleBuffer = new short[read/2];
Buffer.BlockCopy( AudioBytesOriginal , 0 , sampleBuffer , 0 , read );
}
I need the extremely low sample rate for playback on a limited device, but am using .NET Framework 4.6.1 with NAudio to handle the byte work.
Thanks.

a couple of things to check
1) what is the value of read? Is it 0?
2) how far into sampleBuffer did you check? Even half a second of silence at the start of an audio file will result in several thousand samples with 0 value

Related

How do I loop ROBLOX audio at a specific point?

Here's my local script placed into the starter gui. I need the sound to loop after 62 seconds.
game.Workspace.Sound.Play()
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://145294677"
sound.TimePosition = 0
sound:Play()
wait(62)
sound:Stop()
sound:Play()
Sound:Play() will reset the position to 0 or the last set value
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://145294677"
while true do
sound:Play()
wait(62)
end

Reading and Writing Arrays from Multiple HDF Files in IDL

I am fairly new to IDL, and I am trying to write a code that will take a MODIS HDF file (level three data MOD14A1 and MYD14A1 to be specific), read the array, and then write the data from the array preferably to a csv file, but ASCII would work, too. I have code that will allow me to do this for one file, but I want to be able to do this for multiple files. Essentially, I want it to read one HDF array, write it to a csv, move to the next HDF file, and then write that array to the same csv file in the next row. Any help here would be greatly appreciated. I've supplied the code I have so far to do this with one file.
filename = dialog_pickfile(filter = filter, path = path, title = title)
csv_file = 'Data.csv'
sd_id = HDF_SD_START(filename, /READ)
; read "FirePix", "MaxT21"
attr_index = HDF_SD_ATTRFIND(sd_id, 'FirePix')
HDF_SD_ATTRINFO, sd_id, attr_index, DATA = FirePix
attr_index = HDF_SD_ATTRFIND(sd_id, 'MaxT21')
HDF_SD_ATTRINFO, sd_id, attr_index, DATA = MaxT21
index = HDF_SD_NAMETOINDEX(sd_id, 'FireMask')
sds_id = HDF_SD_SELECT(sd_id, index)
HDF_SD_GETDATA, sds_id, FireMask
HDF_SD_ENDACCESS, sds_id
index = HDF_SD_NAMETOINDEX(sd_id, 'MaxFRP')
sds_id = HDF_SD_SELECT(sd_id, index)
HDF_SD_GETDATA, sds_id, MaxFRP
HDF_SD_ENDACCESS, sds_id
HDF_SD_END, sd_id
help, FirePix
print, FirePix, format = '(8I8)'
print, MaxT21, format = '("MaxT21:", F6.1, " K")'
help, FireMask, MaxFRP
WRITE_CSV, csv_file, FirePix
After I run this, and choose the correct file, this is the output I am getting:
FIREPIX LONG = Array[8]
0 4 0 0 3 12 3 0
MaxT21: 402.1 K
FIREMASK BYTE = Array[1200, 1200, 8]
MAXFRP LONG = Array[1200, 1200, 8]
The "FIREPIX" array is the one I want stored into a csv.
Thanks in advance for any help!
Instead of using WRITE_CSV, it is fairly simple to use the primitive IO routines to write a comma-separated array, i.e.:
openw, lun, csv_file, /get_lun
; the following line produces a single line the output CSV file
printf, lun, strjoin(strtrim(firepix, 2), ', ')
; TODO: do the above line as many times as necessary
free_lun, sun

Python Simple PiggyBank Program

This is my Python Program that I have been having some issues with:
-- coding: cp1252 --
from time import gmtime, strftime
print("Welcome to the PiggyBank version 1.")
num_write = int(input("How much money would you like to store in your PiggyBank?"))
f = open("PiggyBanks_Records.txt", "w")
current_time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
convert_1 = str(current_time)
convert_2 = str(int(num_write))
add_1 = ("\n" + convert_1 + " £" + convert_2)
add_2 = ("\n" + add_1) #Tried to make it so new line is added every time the program is run
final_record = str(add_2)
print("Final file written to the PiggyBank: " + final_record)
#Write to File
f.write(final_record)
f.close()
Right now whenever the program writes to the file it over-writes. I would preferably would like to keep, like a history of the amounts added. If anyone can help so the string that needs to be written to the .txt file goes down by one line and essentially keeps going for ever. I am also open to any suggestion on how I can shorten this code.
You need to open your file with append mode :
f = open("PiggyBanks_Records.txt", "a")
Using the 'w' write option with open automatically looks for the specified file, and deletes its contents if it already exists (which you can read about here) or creates it if it doesn't. Use 'a' instead to add / append to the file.

Send/Receive data via USB between Vex Cortex and .net

I'm coding in vb.net (I will accept answers in c# as I can approach this either way) and I need to send data to and receive data from a Vex Cortex. It has a USB port on it which I will be connecting to a computer to send it data via a program.
I have researched this and the .net side seems fairly straightforward. Here is the code I currently have:
Dim sensor As New SerialPort("COM1")
sensor.BaudRate = 9600
sensor.Parity = Parity.None
sensor.StopBits = StopBits.One
sensor.DataBits = 8
sensor.ReadTimeout = 300
sensor.WriteTimeout = 300
sensor.Handshake = Handshake.None
sensor.Open()
I was looking around and i found some steps to take to send data from the program, which is below:
Dim byteOut(5) As Byte
Dim byteIn(6) As Byte
Dim Voltage, i As Integer
Try
byteOut(0) = &H2 '2 bytes in output message
byteOut(1) = &H0 'should be 0 for NXT
byteOut(2) = &H0 '&H0 = reply expected &H80 = no reply expected
byteOut(3) = &HB '$HB = read battery command
sensor.Write(byteOut, 0, 4) '0 = offset into byteOut and 4 = number of bytes
'now read the reply
byteIn(0) = sensor.ReadByte ' number of bytes in message
byteIn(1) = sensor.ReadByte ' should be 0 for NXT
For i = 2 To 1 + byteIn(0) ' read rest of message
byteIn(i) = sensor.ReadByte()
Next
Voltage = byteIn(5) + byteIn(6) * 256 ' the voltage has low byte in 5 and high in 6
Console.Write(Voltage) 'display voltage
Catch ex As Exception
MsgBox(ex.ToString)
End Try
The example above is used to return battery data for an irrelevant device for this question. Basically I need to be able to read the data I send, and send data back to the computer, on the Vex Cortex.
The Cortex is coded in c and I'm using RobotC to compile and download the code to the Cortex. I can't figure out how to read the data for the life of me (example code said to do Serial.begin(9600); but this raises a compilation error in RobotC).
Can anyone assist me on how to read (and write back) data on the Cortex end of this transfer? I may need to ditch RobotC and I'm perfectly fine with that if a solution is proposed without it, so long as it's still c code.

Conversion of MetaTrader4 to NinjaTrader

I am trying to write an indicator originally from MT4 into NT7.
I have the following calculations in MT4:
dayi = iBarShift(Symbol(), myPeriod, Time[i], false);
Q = (iHigh(Symbol(), myPeriod,dayi+1) - iLow(Symbol(),myPeriod,dayi+1));
L = iLow(NULL,myPeriod,dayi+1);
H = iHigh(NULL,myPeriod,dayi+1);
O = iOpen(NULL,myPeriod,dayi+1);
C = iClose(NULL,myPeriod,dayi+1);
myperiod is a variable where I place the period in minutes (1440 = 1day).
What are the equivalent functions in NT7 to iBarShift, iHigh and so on?
Thanks in advance
For NinjaTrader:
iLow = Low or Lows for multi-time frame
iHigh = High or Highs
iOpen = Open or Opens
iClose = Close or Closes
So an example would be
double low = Low[0]; // Gets the low of the bar at index 0, or the last fully formed bar (If CalculateOnBarClose = true)
In order to make sure you are working on the 1440 minute time frame, you will need to add the following in the Initialize() method:
Add(PeriodType.Minute, 1440);
If there are no Add statements prior to this one, it will place it at index 1 (O being the chart default index) in a 2 dimensional array. So to access the low of the 1440 minute bar at index 0 would be:
double low = Lows[1][0];
For iBarShift look at
int barIndex = Bars.GetBar(time);
which will give you the index of the bar with the matching time. If you need to use this function on the 1440 bars (or other ones), use the BarsArray property to access the correct Bar object and then use the GetBar method on it. For example:
int barIndex = BarsArray[1].GetBar(time);
Hope that helps.

Resources