Forum Rule: Always post complete source code & details to reproduce any issue!
Page 4 of 4 FirstFirst ... 2 3 4
Results 76 to 91 of 91

Thread: Sound for model railroading scale 1/32

  1. #76
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    Thanks defragster for the reply......but I am still clutching at straws in the background....????
    I normally I think ? keep my Tools, USB type set to USB HID......and this seems to always work....but for CopyFromSerial it needs to be USB Type Serial...and Copy From Serial works......but the to play the file I load the FlashRawPlayer and USB type was still set to Serial......so I put it to USB HID and the while(!Serial); bit works now and the files play without opening the monitor. I tried USB HID with CopyFromSerial and Python does not open COM5......So........I probably had USB HID when I used CopyfromSD and had no problems. The monitor still printed the file name to the monitor with either setting....

    So USB Serial for CopyFromSerial and USB HID for the Player etc.......

    Maybe defragster you would have a technical answer as to why.....thanks....

  2. #77
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    IWith the setting indicated in last post, it plays OK when Powered by USB port of the PC but does not play if plugged into a USB power bank that is fully charged and I have tried on several other Teensy devices to make sure the power pack is working and all work. If I set the USB type to "No USB" I can still upload the player sketch to teensy with the "No USB" setting and it reboots and plays normal and OK. And it plays when Powered by the USB Power bank. I have never come across this before and I have had lots of Teensys and programmed all sorts of sketches. So what has changed in the program or settings...???
    What is the meaning of USB type in tools settings.....??
    What does "No USB" mean if I can still program it with a USB port....??
    all my other teensy devices work from a power bank and were definitely not setup as "No USB"...?

  3. #78
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,069
    programmed as NO_USB should only work with a BUTTON push to program?

    USB battery bank will work - with programmed delay using the provided && millis() cod.

    My use is generally USB_Type Serial - - HID usage was rare - and connected at those times. And usually with the millis() timeout to prevent it hanging when not connected.

  4. #79
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    Yes, No USB means Press Button to upload sketch....Ok thats fine....I never gave all this much thought in the past as it never gave me a problem, the only time I think I ever changed these settings was when doing MIDI sketches OR making teensy work as a USBtoSerial port for talking serial to something.
    So I went back to basics, closed down Arduino IDE, closed down computer, rebooted, run CCleaner, rebooted computer, no teensy plugged in, checked system devices, shows COM1 only and no HID devices......All OK and normal.

    Got a brand new Teensy3.2 probably bought about 6 mths ago never used still in packet, plugged it into USB port on PC, PCsays Found new hardware etc etc, new HID device found, Device now ready, or words similar....all appears normal and OK.

    Opened system devices, what now listed are...2x HID compliant device and 2x USB Human Interface Device and still COM1 only listed.......all Normal and OK.....and preinstalled Blink sketch is flashing the LEDs.

    Opened the Arduino IDE, it opens at the last sketch used which was FlashRawPlayer, Did not do anything with it, clicked file new, opened the blank default that opens with new, closed the FlashRawPlayer sketch, all normal, now looking at the new blank sketch, clicked menu tools, list shows USB type HID and Port lists COM1 but not ticked.
    And I think all this is normal and I would not have changed these settings so thats why I assumed I always used HID in the past......???

    It is possible I have uploaded sketches with USB set to Serial but Serial not actually used or needed, OR maybe they have a Serial(begin) with a few Serial.print lines but wouldn't have any while(!Serial) waiting to make sure it was there......so all my devices work plugged into PC or on Power Bank or on just 2 wires.

    some of these checks are ok when writing or debugging sketches but maybe not for the final running program....????

    When I plug in my teensy with the player sketch set to Serial, system devices immediately shows Teensy COM5 listed and Tools says USB type Serial and Port COM5 ticked.

    The sketch works OK without the while(!Serial) check and works with Defragster mod as well....but if I read it correctly it waits for Serial and if it doesn't happen go on anyway.??....the Serial.print lines are working so serial must be working OR is it that Serial only starts working when we open the monitor window....we need the monitor open anyway to see what it prints.....so if it Listfiles sketck...no problem we are expecting text so we open the monitor.....but if Flash Player we are expecting sound so don't bother to open the monitor.....????

  5. #80
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,069
    It seems your post 79 dialog is normal and good. To support USB the Teensy needs to be running USB code, unlike UNO it doesn't have a USB hardware interface chip that survives reboot and is always present.

    When any USB stack is loaded in Teensy sketch - and active without fault from BAD code - it can respond to the Computer request to upload Program and button press not required. The Button press is when USB NONE { as shipped } or if the loaded sketch takes USB stack offline.

    When there is a while (!Serial){} that will wait forever for a USB monitor to connect, adding some timeout like &&millis() allows that to be conditional as desired.

    Without some wait - early prints in setup() or loop() can be lost as Teensy may be online and ready to run before the computer software can be connected.

    Using while (!Serial && millis()<500 ); would result in a 100 ms ( 150 ms on TD 1.42 ) wait at most and USB would generally be present when desired for immediate use, and minimal delay when not connected. Also there is typically some buffering in USB code if minimal prints are sent before a Monitor is connected.

  6. #81
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    OK, thanks for that, it has given me a better understanding as to what happens for the different settings.

    I wonder what is happening when I try to copy a 1.6 MB or a 700 KB to spiflash using Copyfromserial.....My PC crashes and restarts.....I can copy them using CopyfromSD...no problems.......I can copy small files with CopyFromSerial OK and it works as discussed in the earlier posts.

    What is the reaction between Python and the Teensy serial port and SPI......With Teensy setup as a Serial port is it acting like a terminal on COM5 and then Python opens COM5 and sends data to Teensy over the serial port and then Teensy sends the data to spiflash over SPI.....??
    What bit of this is likely to be the problem....????

  7. #82
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,069
    Not having read anything other than what led to what I posted ... speculation ...

    Could be comingling the SPI writes and COM5 transfers? They likely don't run at the same speed - and the overhead of incoming USB may be interfering with the SPI writes?

    Without handshaking on the USB to control Python output the incoming data may be faster than the data dumping to the SPI?

    Quick test might be to have the Python send usable/manageable chunk - some number of flash pages that fit in Teensy RAM - then pause Python until the Teensy asks for more?

    Not sure if that is already done in any fashion - or is easily done ...

    <edit> just re-read p#81 - I've not used these 'copyfrom...' things - it is the PC that crashes when the Teensy spews OUT data? SPI read will run much faster than SD - the receiver code on the PC would have to be efficient to keep up with Teensy data rate. Take what is written above and reverse it ... have the Teensy send at a slower rate - like that from SD - send in usable blocks and pause ?

  8. #83
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    The CopyfromSerial sketch I am using is posted on post #71. Basically it is loaded and running on Teensy. The audio.raw files to be copied are on PC in same folder as the Python script....which is the "rawfile-uploader.py" found in extras folder in the SerialFlash teensy library.

    PC is connected to teensy via the USB/Serial port and spiflash is connected to Teensy via SPI port.

    Direction of Data transfer is from PC via Python to USB/Serial and on to Teensy and from Teensy sketch via SPI port to spiFlash. So from your last post SPI runs faster but would be fed by the slower Serial data..???? so would there be that type of problem...??

    Python apparantly buffers the data on PC waiting on Teensy saying it is ready, The crash seems to happen as soon as transfer seems to start, I thought maybe a conflict when Teensy is trying to talk to the port when Python has it open as well...???.......Thanks for your consideration.....

  9. #84
    Have you try on another PC?
    Because, we call some Windows ressources and I am not sure of the compatibility with certain Python procedures.
    I thought that the PC USB (seen from Windows) link protocols were differents when :
    1) uploading with Python rawfile-uploader.py
    2) uploading SPI Flash with CopyFromSerial
    That's why it was necessary to unplug /plug in order to re-initialize the protocol parameters.

  10. #85
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,069
    Okay so that the <edit> was the wrong read ... indeed SPI write should be fast - though at 12 Mbit USB won't be slow - and that incoming data won't be stopped unless speed is controlled by sender? The incoming data reception will be causing data buffering and interrupts to receive the data. Would not be surprising to have it affect the SPI interface at full speed.

    If it works for small file - it is odd that it hangs so quickly on the start of a larger file without some error - or that the small file was buffered and done as the writes completed.

  11. #86
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    Yes, I think there is something about needing to unplug/plug in again. Although I have got small files copied without unplugging at all between loading and running the 2 sketches Copy and List. But some times the python cannot find the com5 port and if I unplug/plugin and run script before blinking LEDs it works.

    I put a blank new sketch (just file new) on teensy, with USB serial and port Com5 (COM5 needed because Python cant open COM5 if it not plugged in and in System Devices) and then ran the cCMD line python script trying to copy the 700kb file....on the command screen it got as far as listing the file and then crashed.....????
    PC just does not "Hang" it crashes completely and reboots PC.
    I will pu Python on another Win10 laptop ad see what happens....and post back again.

  12. #87
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    Yes, that worked, I put Python 2.7 x86 version on Windows 10 64 bit laptop and pyserial 2.6 version as per earlier posts

    I had put Python 2.6 64 bit version on but couldnt find pyserial2.6 for 64bit....googled alot of info saying there was problems with 64bit version but that x86 version of both worked OK on 64 bit.... so installed that.

    I have the Arduino environment on an Windows XP so I still loaded the same sketches on XP and unplugged and plugged in Win10 and ran the Python Script from there starting it at the first double blink of the LEDS. then when LEDS out unplugged and back to XP to upload the Listfiles sketch...compiled it first with teensy unplugged the plugged in and press button right away before any LED flashing so files were not deleted.....Files listed OK.....Then uploaded the Player sketch and it played OK. a bit of extra noise an end of sound...need to check that out...???

    A thing I hadnt noticed before the enumeration of the ports starts from 0 upwards.....so serial(4) is COM5....????? same on Win10 PC
    >>> ser5 = serial.Serial(4)
    >>> print ser5
    Serial<id=0x1365bd0, open=True>(port='COM5', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)

  13. #88
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    I copied 5 files from Serial....2 of size 2MB approx each and 3 other small files about 15 Kb each. All done listed on one command line. All copied OK, and played OK, except for a small about 2 ms glitch in one of the 2MB files. I don't see the glitch if I import this RAW file into Audacity so must be added during copying ...I then copied the same files again along with 1 aboy 700kb and another small one....The files listed OK but when played one of the large was just loud noise and one not picked up by player......I ran Erase all and then copyfromserial again and copied the 7 files about 4.8 MB again....all copied and played OK. I did the Arduino bit on the XP maching and the Python bit on the Win10 laptop. I did the same both times of copying.


    Below this is the procedure I was following.......

    1......CopyFromSerial sketch needs to be set USB/Serial when compiling/uploading so that when Teensy is plugged in to the Python PC it will appear as a USB/Serial COMMS device.

    2. The Python PC maybe the same PC as the Arduino IDE so Teensy needs to be unplugged/pluggedin again to re-initialise the device and make it ready for Python.

    3. The Python PC needs to have the COMMS port ready in System devices so that it will see/find it when the Script is started to run.

    4. Compile the CopyFromSerial sketch in Arduino IDE with either ....

    ... Teensy Plugged in and Auto Upload to Teensy......

    ... OR Teensy Unplugged and wait until asked to Press Button....Plugin Teensy and Press button to upload.

    ... If "Reboot OK" ...let Teensy run until LED is solid on......Then unplug USB.


    5. ... Open CMD Prompt Window for Python and type in command line...

    ... When ready to send Python command line ....Plugin Teensy and Hit Return to send command line as soon as
    LEDs starts flashing....

    6 ... Filenames will be listed on command window....when copying finished the LEDs go OFF.

    7. ... Unplug Teensy. Do not plug in again while CopyFromSerial sketch is in Teensy or it will re-run
    and erase the files just copied.

    8. ... Open Arduino and load and compile the Listfiles sketch with Teensy USB unplugged.

    9. ... When asked to Press Button.....Plugin Teensy and within a few seconds Press button to upload sketch.

    10. ... Open IDE monitor to see list of files that were copied. Unplug/plugin to re-run the list.

    11. ... Compile and upload the RawFlashPlayer sketch to hear the files or load any other sketch as normal.


    It seems to be working....but not all that reliable....???
    Last edited by Teenfor3; Yesterday at 04:33 PM.

  14. #89
    What is the best soundfile extension (easy to manage in sketch, no glitch, no stop when reading soundfiles)
    to deal with Audio.h (for switching, for mixing, . . .) via Audio GUI tools ?

    1) SOUNDFILE.RAW (direct from Audacity)
    or
    2) SOUNDFILE.TRW ( Audacity - 44000 Hz/16 bit) + (wav2trw -16)

  15. #90
    Senior Member
    Join Date
    Apr 2014
    Posts
    232
    For Binary files...RAW would be the most popular and Audacity would probably be the best editor/converter. With Audacity open/import various sound files and you can hear it and see its waveform and edit/convert and save/export it in a variety of formats......and then import what you have done and see/hear it again. Audacity gives a RAW binary file with no header.

    SOX will convert a WAV to RAW but no visual...???.....a WAV binary with no header

    Wav2Trw will convert it to TRW ....a binary file...actually a RAW file with TRW extension and a small header info.

    I found sometimes there are Zeros at end of the TRW files and maybe the others as well at time which will give long gap between when playing
    To play continuous loop the file need clean start and end whis is easier to do in Audacity

    You can loop the sound file in Audacity and hear how it joins up...and edit the start and end to get best then export......the challenge will the to get it to loop as well in Teensy.

    What type of sound file are you playing...??? long or short...or continuous sounding long playing sirens or whistles .....or intermittent sounds as in engine tick over or steam train puffing...
    Last edited by Teenfor3; Today at 05:06 PM.

  16. #91
    There are two kinds of soundfiles to play:
    Type 1)
    Basic soundfiles related to the engine (steam, electric or diesel) which depends on the speed (stop, start -> full speed), but also on the deceleration (soft braking, strong braking to stop). These files can last each last 1 minute.

    Type 2)
    Event sounfiles related to manual commands (whistle, horn, lift synchronized pantographs (with servos), coupling/ uncoupling (servo). These files can last 30 s each.
    The total soundfiles could be 10 minutes.

    Both types of soundfiles Type 1) and Type 2) can be played together (Type 1 + Type 2) --> eg :engine + whistle).
    So I need to be able to mix these sounds.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •